• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Struts passing a List to and from a Form

My application allows the user to add, update and delete courses. Each course have zero or more modules associated with it. I am having a problem updating the modules for a course.

Currently I can display the "Update Course" page, which displays course title, course code and all of the modules for the course.

I am using a list to store all the module details, the list is made up of module objects.
List<Module> modulesInList = new ArrayList<Module>();

I can change any of the module information but when I hit the Save button on the "Update Course" page the module details are not passed to the Update Course action. The setModulesInList function is working fine, in that it is populating the table with module information  in the Update Course page. The getModulesInList function in the CourseForm doesn't return anything when I call it from the UpdaetCourseAction...
I don't get any error but I can see the getModulesInList function is being called successfully but there is no module information there being passed back (see Course Form below)

One thing that is strange, in the jsp when I am referencing the modulesInList List i have to add the form name, courseForm.modulesInList. I dont have to do this with other items from the form, such as Course Name, Course Code, etc...

My question is why is the Course Form not passing back the module information when called from the action?
Any help really appreciated!

Below is the jsp page "Dispaly a course for Update"
<body>
<br/>
<h3>Update Course</h3>
<br/>
 <font color="Red"><b><html:errors/></b></font>
 
<html:form action="/updateCourse">

<h4>Fill in the details below to Update Course</h4><hr>
             
<table>
<tr>
  <td>Course Id</td>
  <td><html:text property="courseId" readonly="true"/></td>
</tr>
<tr>
  <td>Course Title</td>
  <td><html:text property="courseTitle"/></td>
</tr>
<tr>
  <td>Course Code</td>
  <td><html:text property="courseCode"/></td>
</tr>
</table>

<br/>
<h4>Update the details of the Modules</h4><hr>
<html:hidden property="count" styleId="count" value="0"/>
<input type="button" value="Add Module (+)" onclick="addRow(moduleslist)"/>
Remove module button
<br/>
<table border="1" id ="moduleslist" border="1">
<tr>
 
  <td>Module ID</td> <td>Module Number</td>
  <td>ModuleName</td>
  <td>Module Code</td>
  <td>Module Date (dd/mm/yyyy)</td>
  <td>Module Expiry Date  (dd/mm/yyyy)</td>
</tr>

<c:forEach var="m" items="${courseForm.modulesInList}" >
<tr>
<td><input name="moduleId" disabled="disabled" style="padding: 0"value="<c:out value="${m.moduleId}" />"/></td>
<td><c:out value="${m.moduleNumber}" /></td>
<td><input type="text" name="moduleName" style="padding: 0"value="<c:out value="${m.moduleName}"/>" /></td>
<td><input type="text" name="moduleCode" style="padding: 0"value="<c:out value="${m.moduleCode}"/>" /></td>
<td><input type="text" name="moduleDate" style="padding: 0"value="<c:out value="${m.moduleDate}"/>" /></td>
<td><input type="text" name="moduleExpiry" style="padding: 0"value="<c:out value="${m.moduleExpiry}" />"/></td>
</tr>
</c:forEach>
</table>
<span id="textBoxes"></span>

<table>
<tr><td><html:submit onclick="return confirm('Are you SURE you want to Update this Course?')">Update Course</html:submit></td></tr>
</table>
</html:form>
</body>

//////////////////////////////////////////////////////////////////
This is the relevant section from Update Course Action:

List<Module> modulesInList = new ArrayList<Module>();
modulesInList = courseForm.getModulesInList();
System.out.println("Update Course Action: Testing~: "+courseForm.getModulesInList());
int i;
System.out.println("myListSize: " + modulesInList.size());
for(i=0;i<modulesInList.size();i++){
      System.out.println("Update Course Action: MOdule Name: "+modulesInList.get(i).getModuleName());  
}


//////////////////////////////////////////////////////////////////////
relevant section from Course Form:

      private List<Module> modulesInList = new ArrayList<Module>();
      
      
        public List<Module> getModulesInList(){
              System.out.println("Here we are in CourseForm: Get Modules in List");
              int i;
              for(i=0;i<modulesInList.size();i++){
                        System.out.println("Course Form: Get modules in List: MOdule Name: "+modulesInList.get(i).getModuleName());  
                     }
              return modulesInList;
        }
        public void setModulesInList(List<Module> modulesInList){
              int i;
              for(i=0;i<modulesInList.size();i++){
                        System.out.println("Course Form: Set modules in List: MOdule Name: "+modulesInList.get(i).getModuleName());  
                     }
             this.modulesInList = modulesInList;
        }
0
kieranocallaghan
Asked:
kieranocallaghan
  • 5
  • 4
1 Solution
 
Gurvinder Pal SinghCommented:
you need to set the list in the session and then use the same in the jsp to use it in the form.

for passing it from the form, you need to set the values to the input fields (hidden or textboxes), and do the getParameter in the servlet
0
 
kieranocallaghanAuthor Commented:
Hi gurvinder,
I just tried that, however, the changes I made in the modules table were not reflected in the List when I pulled it from the session in the UpdateCourseAction.
Any idea why this would happen?





Now I have the following in the getCourse Action:
//////////////////////////////////////////////////////////
session.setAttribute("modulesInListInSession", modulesInList);

//////////////////////////////////////////////////////////
Now what I have in JSP:
//////////////////////////////////////////////////////////
<c:forEach var="m" items="${modulesInListInSession}" >
<tr>
<td><input name="moduleId" disabled="disabled" style="padding: 0"value="<c:out value="${m.moduleId}" />"/></td>
<td><c:out value="${m.moduleNumber}" /></td>
<td><input type="text" name="moduleName" style="padding: 0"value="<c:out value="${m.moduleName}"/>" /></td>
<td><input type="text" name="moduleCode" style="padding: 0"value="<c:out value="${m.moduleCode}"/>" /></td>
<td><input type="text" name="moduleDate" style="padding: 0"value="<c:out value="${m.moduleDate}"/>" /></td>
<td><input type="text" name="moduleExpiry" style="padding: 0"value="<c:out value="${m.moduleExpiry}" />"/></td>
</tr>
</c:forEach>

//////////////////////////////////////////////////////////
And in the the UpdateCourseAction:
//////////////////////////////////////////////////////////
 HttpSession session = request.getSession();
                  modulesInList = (List<Module>) session.getAttribute("modulesInListInSession");
                  System.out.println("Update Course Action: Testing~: "+courseForm.getModulesInList());
                      int i;
                      System.out.println("myListSize: " + modulesInList.size());
                           for(i=0;i<modulesInList.size();i++){
                                    System.out.println("Update Course Action: MOdule Name: "+modulesInList.get(i).getModuleName());  
                                 }
0
 
Gurvinder Pal SinghCommented:
as i have told above, if you want to pass from form to server, you need to submit the same to the servlet, and fetch it via getParameter method, since the session value cannot be updated at the client side
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kieranocallaghanAuthor Commented:
Hmm, ok, as far as I can tell I have done following: "you need to set the list in the session" and "then use the same in the jsp". This is how I am populating my table.

I don't understand how to "use it in the form", I though that was what I was doing with: <input type="text" name="moduleName" style="padding: 0"value="<c:out value="${m.moduleName}"/>" />

I don't know how to  "submit it in the servlet" using getParameter in the Servlet. Can you give me an example please?
Thanks,
Kieran
0
 
Gurvinder Pal SinghCommented:
if you are submitting the form, then it is getting submitted to the servlet already
0
 
kieranocallaghanAuthor Commented:
Hmm, I am still confused.
I have tried pulling one of the module names from the servlet:
                           String test = (String) request.getAttribute("moduleName");
                           System.out.println("Update Course Action: Modules: "+test);

but i get a null pointer exception...
Is this the right approach?
0
 
kieranocallaghanAuthor Commented:
Opps, Im using getParameter now and I am seeing the updated module name.

String test = (String) request.getParameter("moduleName");
System.out.println("Update Course Action: Modules: "+test);

However I only see one set of module details when I check all the parameter Names in the request.
Hmm, how do I cycle through all the modules in the original list?


 Enumeration paramNames = request.getParameterNames();
                            while(paramNames.hasMoreElements()) {
                              String paramName = (String)paramNames.nextElement();
                              System.out.println("Param Name:" + paramName);
                            }
0
 
Gurvinder Pal SinghCommented:
you need to name them in the form appropriately. Like, module1, module2 etc, and then iterate through them
0
 
kieranocallaghanAuthor Commented:
Yep, works, thanks for the help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now