开发者

Updating the value of a iterator tag in struts2

I have a jsp in which I have a drop down as

<s:select name="newQuestion.CertificationId" list="certificationList"
          listKey="certificationId" listValue="certificationName" 
          headerKey="" headerValue="Select Certification" 
          label="Certification Name"
          onchange="getQuesti开发者_如何学运维onsList(this.value)" />

When the dropdown value changes I can getQuestionsList. In the javascript function I submit to an action class where I modify the value of a questionList which is displayed in my JSP via an iterator.

The values of the questionList contain all questions and when I select a value from the above drop down I need to populate only those questions which belong to the id selected in the drop down. (I query the DB to load the questions in action class.)

Initially when the page is loaded I have all questions in questionList but after selecting a value from drop down I have the updated questions in the action class.

For displaying the values of question list I use a iterator tag

<div id="questionDetails" class="registrationDetails" style="display: none;">
  <span><b>Question List</b></span>
  <br>
  <table class="registrationDetailsTable">
    <tr class="tabledataheader">
      <td>Question Id</td>
      <td>Question Description</td>
    </tr>

    <s:iterator value="questionList">
      <tr class="tabledatarow">
        <td><s:property value="questionId" /></td>
        <td><s:property value="questionDesc" /></td>
      </tr>
</s:iterator>
  </table>
</div>

The div is initially hidden and on select of a value in drop down I need to display the values of questionList which is taking old values as the page is not reloaded.

When I again come back to this jsp I am not seeing the new value as it is not getting updated.

Any heads up please


Why don't you try Struts2 Jquery Plugin. Here is the showcase with code.

What you need is

<sj:select ...> tag.

Let me know if you still need an example.


You would need to make few changes to the code. Let me list them down:

  1. Move the following part in your initial JSP to another jsp, say questionList.jsp. The below part should be the only thing which should be inside the newly created jsp.

      <span><b>Question List</b></span>
      <br>
      <table class="registrationDetailsTable">
        <tr class="tabledataheader">
          <td>Question Id</td>
          <td>Question Description</td>
        </tr>
    
        <s:iterator value="questionList">
          <tr class="tabledatarow">
            <td><s:property value="questionId" /></td>
            <td><s:property value="questionDesc" /></td>
          </tr>
    </s:iterator>
      </table>
    
  2. In your main page, you would need to replace the above code part with

    <jsp:include page="questionList.jsp" flush="true"/>

  3. Now deploy and see if everything is in place as before.

  4. In your getQuestionsList() javascript function, make an ajax call to another action mapping say showQuestionList.action?certification=12. Here, 12 will be the id of the certification, which you will handle in the action. If you use certification as an action property, add a variable with the same name with getter and setter methods.
  5. In your action method say showQuestionList(), retrieve the questionList and assign to the action variable.
  6. On return of SUCCESS of the above method, the result should send only questionList.jsp. This will have the part to list just the necessary fields.
  7. In the getQuestionsList() javascript function, after the ajax call being success, get the data and put it into the div with id questionDetails.
  8. Show the div.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜