[英]How to put variable in OGNL tag
<input type="radio" name="${questions.qid}" value="${questions.ans1}" <s:if test="%{(#session.map.get(1)).equals(\'ans1\')}">checked</s:if>><s:property value="#attr.questions.ans1"/>
在此代碼中,'questions' 是一個列表,其中包含帶有字符串 question,ans1,ans2,ans3 的問題對象。 在我的程序中,我會讓它出現在瀏覽器中
Question 1
(RadioButton) Answer 1
(RadioButton) Answer 2
(RadioButton) Answer 3
Question 2
(RadioButton) Answer 1
(RadioButton) Answer 2
(RadioButton) Answer 3
.
.
.
該列表可能包含多個問題對象,所以我讓它每頁顯示 5 個問題。 我的問題是(例如)用戶可能從第 4 頁轉到第 2 頁,我想重新填寫用戶在第 2 頁中單擊的答案。因此,在操作類中,我創建了一個 HashMap 並放置了問題 id (qid) 和將回答的問題(例如 ans2)放入地圖,然后將此地圖放入名為“地圖”的會話中。
在上面的代碼中,我使用
<s:if test="%{(#session.map.get(1)).equals(\'ans1\')}">checked</s:if>
在 html 單選標簽中。 我將問題 id (qid) 硬編碼為“1”,它按計划工作。 但是 get() 中的數字必須是可變的。 那一定是我用過的真實問題ID
name="$(questions.qid)"
我試着把參數作為
#session.map.get(#attr.questions.qid)
但它不起作用。 請指導我如何制作參數變量。
要填充您的問題,您需要使用s:iterator
標簽。
<s:iterator value = "myQuestions" status="key">
<s:textfield name = "myQuestions[%{#key.index}].name" /><br>
<input type="radio" name="myQuestions[<s:property value="%{#key.index}"/>].ans1" value="<s:property value="%{myQuestions[#key.index].ans1}"/>" <s:if test="%{(#session.map.get(myQuestions[#key.index].name)).equals(myQuestions[#key.index].ans1)}">checked</s:if>><s:property value="%{myQuestions[#key.index].ans1}"/><br>
</s:iterator>
在行動中按名稱使用地圖問題(相當於您的qid
)
Map<String, String> map = new HashMap<String, String>();
根據您的描述創建的問題類。
public class Question {
private String name;
private String ans1;
private String ans2;
private String ans3;
//getters setters here
}
private List<Question> myQuestions;
//getters setters here for questions
確保在返回結果之前初始化問題。
public String execute(){
myQuestions = new ArrayList<Question>();
myQuestions.add(new Question("Question1", "ans1", "ans2","ans3"));
myQuestions.add(new Question("Question2","ans1", "ans2","ans3"));
//test results, map should not be empty
map.put("Question1", "ans1");
map.put("Question2", "ans2");
session.put("map", map);
在這個例子中,第一個無線電將被選中,第二個無線電將由於會話映射值而被取消選中。
表單的輸入元素通過它們的名稱綁定到動作。 如果您在提交表單時需要獲取值,則需要使用索引屬性名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.