Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Struts passing a List to and from a Form

Posted on 2011-09-20
9
Medium Priority
?
436 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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
Technology Partners: 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!

 

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:Gurvinder Pal Singh
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:
Gurvinder Pal Singh 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

610 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