?
Solved

Struts passing a List to and from a Form

Posted on 2011-09-20
9
Medium Priority
?
430 Views
Last Modified: 2012-05-12
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
Comment
Question by:kieranocallaghan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36568751
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
 

Author Comment

by:kieranocallaghan
ID: 36568833
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36568852
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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 

Author Comment

by:kieranocallaghan
ID: 36568932
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36568967
if you are submitting the form, then it is getting submitted to the servlet already
0
 

Author Comment

by:kieranocallaghan
ID: 36569122
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
 

Author Comment

by:kieranocallaghan
ID: 36569302
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
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 2000 total points
ID: 36571339
you need to name them in the form appropriately. Like, module1, module2 etc, and then iterate through them
0
 

Author Closing Comment

by:kieranocallaghan
ID: 36579742
Yep, works, thanks for the help!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question