How to put variable in OGNL tag

試著忘記壹切 提交于 2020-01-03 00:01:18

问题


<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"/>

In this code 'questions' is a list which contain questions object with String question,ans1,ans2,ans3. In my program, I will make it appears in browser like

Question 1
(RadioButton) Answer 1
(RadioButton) Answer 2
(RadioButton) Answer 3

Question 2
(RadioButton) Answer 1
(RadioButton) Answer 2
(RadioButton) Answer 3

.
.
.

The list may contain multiple question objects so I made it to show 5 question per page. My problem is (for example) the user may go from page 4 to page 2 and I want to refill the answers the user have clicked in page 2. So in the action class, I create a HashMap and put question id (qid) and answered question (eg. ans2) into the map, then put this map into the session called "map".

In above code, I use

<s:if test="%{(#session.map.get(1)).equals(\'ans1\')}">checked</s:if>

in the html radio tag. I hard coded the question id (qid) as '1' and it works as planned. But the number in the get() must be variable. That must be real question id like I used in

name="$(questions.qid)"

I tried put the parameter as

#session.map.get(#attr.questions.qid)

but it don't work. Please kindly guide me how can I make the parameter variable.


回答1:


To populate your question you need to use the iterator tag.

<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>

in the action use map questions by name (equivalent your qid)

Map<String, String> map = new HashMap<String, String>();

the question class created from your description.

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

make sure you initialize the questions before return result.

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);

In this example the first radio will be checked and second unchecked due to the session map values.

The input elements of the form are bound to the action by their names. If you need to get values when you submit the form you need to use indexed property names.



来源:https://stackoverflow.com/questions/16252528/how-to-put-variable-in-ognl-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!