Question

I am trying to use a hashmap to populate textfields in a jsp and bring back the modified values in the action class through the same hashmap i used to populate. In spite of many different approaches, i am failing to do so. I tried sending back an object list but i failed in that too(see this thread). Its not necessary that i use the following approach, but i just want to take a modified list back to action class through struts tags. Please help me in this i already wasted a lot of time in this.

Action class code

private HashMap<String,Integer> coursesMarks;
//getters and setters

public String editCoursePage() {
        this.student = studentService.getStudent(rollNo);
        this.courseList = courseService.getStudentCourses(rollNo);
        coursesMarks = new HashMap<String, Integer>();

        for(Course c: courseList) {
            coursesMarks.put(c.getCourse(), c.getMarks());
        }
        return ActionSupport.SUCCESS;
    }

    public String editCourseAction() {

        System.out.print("inside of student action "
                + coursesMarks.size() + "\n\n\n");

        return ActionSupport.SUCCESS;
    }

Inside the jsp

<s:form action="editCourseAction" validate="true">
 Courses: 

<s:iterator value="coursesMarks" status="st">
<s:label ><s:property value="course" /></s:label>
<s:textfield name="coursesMarks[%{#st.index}].value" theme="simple"/>
<br />

</s:iterator>
<s:submit action="editCourseAction" value="submit" />
 </s:form>

Running this code throws this error...

    Mar 13, 2013 12:09:23 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
    at java.util.HashMap$EntryIterator.next(HashMap.java:834)
    at java.util.HashMap$EntryIterator.next(HashMap.java:832)
    at org.apache.struts2.components.IteratorComponent.end(IteratorComponent.java:334)
    at org.apache.struts2.views.jsp.IteratorTag.doAfterBody(IteratorTag.java:87)
    at org.apache.jsp.pages.editCourse_jsp._jspx_meth_s_005fiterator_005f0(editCourse_jsp.java:251)
    at org.apache.jsp.pages.editCourse_jsp._jspx_meth_s_005fform_005f0(editCourse_jsp.java:174)
    at org.apache.jsp.pages.editCourse_jsp._jspService(editCourse_jsp.java:104)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        .....and a long long list of error lines below this

If i use the following code

    <s:iterator value="coursesMarks" >
<s:label ><s:property value="course" /></s:label>
<s:textfield name="value" theme="simple"/>

It displays the values correctly but does not pass them back to the action class. Could you please tell me how to accomplish my task

Était-ce utile?

La solution

In JSP iterate over keys of your map and in <s:textfield> tag use key to get values. You JSP should look like that:

<s:form action="editCourseAction" validate="true">
 Courses: 

  <s:iterator value="coursesMarks.keys" var="coursekey">
    <s:label ><s:property value="course" /></s:label>
    <s:textfield name="coursesMarks['%{#coursekey}']" theme="simple"/>
    <br />

  </s:iterator>
  <s:submit action="editCourseAction" value="submit" />
</s:form>

Or iterate over map like you did and use key keyword to get values.

<s:iterator value="coursesMarks">
  <s:textfield name="coursesMarks['%{key}']" theme="simple"/>
</s:iterator>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top