Solved

Struts: Read values from table

Posted on 2011-09-08
10
1,518 Views
Last Modified: 2013-11-24
I have been using struts framework to develop my application
My application stores information on courses, companies, students and results. Students can be associated with one or more companies.
On the update student page the user has the option to associate or disconsolate companies with a student.

So far I have the full list of companies listed on the update student page along with the correct  Boolean values (associated or not associated).
Also the user can associate or disassociate companies by checking or unchecking a tick box for each of the companies.
The problem I am having is in the Update Student Action, I am not able to read which company have been checked / unchecked from the table on the update student page.

I do not know how to save this student - company relationship in the Student Form, I have tried saving the values in the session and reading them from there but updates I make on the update student page are not picked up. Also I cannot figure out how to read directly the values on the update student jsp to the update student action.

HELP!!! I am out of ideas!
I hope my explanation is clear.
Here is a code snippet from the update student JSP. I am able to populate the table below from the DB using values I have read in the Get Student Action.

<table border ="1">
<tr><td><b>Company Name</b></td>
<td><b>Company Type</b></td>
<td><b>True or False</b></td>
<td><b>Selected</b></td>
</tr>

<c:forEach var="c" items="${companyList}">
<tr>
<td><c:out value="${c.companyName}"/></td>
<td><c:out value="${c.companyType}"/></td>
<td><c:out value="${c.associatedWithStudent}"/></td>
<td>
<c:if test="${c.associatedWithStudent==true}">
<input checked="checked" type="checkbox" name="${c.companyName}" value="${c.companyId}" />
</c:if>
<c:if test="${c.associatedWithStudent==false}">
<input type="checkbox" name="${c.companyName}" value="${c.companyId}"/>
</c:if>
</td>
</tr>

</c:forEach>
</table>
0
Comment
Question by:kieranocallaghan
  • 5
  • 4
10 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36507440
It looks as if your problem is that  values for checkboxes should be something like  true or on or 1.  But your values are company ids.  Struts people recommend using the multibox feature to do the type of thing you're doing:

http://www.jguru.com/faq/view.jsp?EID=925277
0
 

Author Comment

by:kieranocallaghan
ID: 36509100
Hi mrcoffee,
I am using true / false values for my check boxes, the company id is just a unique identifier.
The screen shot attached should help clarify what I have currently. This is the update student page. The companies the student is associated with are listed in the table. The last two columns are pretty much the same thing, just displayed differently. true = checked.
Thanks
Kieran Companies associated with student Companies associated with student Companies associated with student
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36509354
You input checkbox says:
value="${c.companyId}"

That means that the value is being set to the id.

Have you tried using the multibox feature of struts?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 36515821
Hi kieranocallaghan,

I feel you should not use the dynamic name to the check boxes

<input type="checkbox" name="${c.companyName}" value="${c.companyId}"/>

Use a common name to all the check box (i.e. selectedCompanies) and use the property of array type in the form (i.e. int[] selectedCompanies - assuming the companyId is numeric).

0
 

Author Comment

by:kieranocallaghan
ID: 36516120
Hi mrcoffee,
I've been trying to get a simple example of multi box working. The tickboxes are displayed along with their labels but I am getting an error when I tick the boxes and submit the form:
java.lang.IllegalArgumentException:

Cannot invoke form.StudentForm.setOptions on bean class 'class form.StudentForm' - argument type mismatch - had objects of type "java.lang.String" but expected signature "java.util.ArrayList"

My GetStudentAction is as follows, i just create a simple arraylist with 2 items:
ArrayList options = new ArrayList();
  options.add(new LabelValueBean("Texaco","true"));
  options.add(new LabelValueBean("Spar","true"));
request.setAttribute("TESTcompanies", options);

My displayStudentForUpdate.jsp is as follows:
<c:forEach  items="${TESTcompanies}" var="c">
<html:multibox property="options"><c:out value="${c.value}"/></html:multibox><c:out value="${c.label}"/>
</c:forEach>

My StudentForm has the following:
public ArrayList getOptions(){
        return options;
}
public void setOptions(ArrayList options){
         this.options = options;
}

I have tried but cannot figure out whats wrong with it!
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36525628
It's hard to tell from the code fragments, but where do you have the logic:iterate statement?

Also -- try to use something besides "c" as the variable name -- "c" is commonly used for the standard tag library, and you could easily hit conflicts with the naming.

And finally -- when you say it doesn't work -- what happens?  If you get exceptions, then post them.
0
 

Author Comment

by:kieranocallaghan
ID: 36526225
The logic:itterate statement is in the displayStudentForUpdate.jsp, the full file is pasted below:

I am now using tagL instead of c for the standard tag library and I have changed any other variables using "c".

When I submit the form to update the student I get an exception:
java.lang.IllegalArgumentException:

Cannot invoke form.StudentForm.setOptions on bean class 'class form.StudentForm' - argument type mismatch - had objects of type "java.lang.String" but expected signature "java.util.ArrayList"
The full stack trace is posted at the bottom of this comment.
Thanks,
Kieran

displayStudentForUpdate.jsp
///////////////////////////////////////////////////////////////////
<%@page import="Database1.Student"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib prefix="tagL" uri="http://java.sun.com/jstl/core" %>
<%@ page language="java" contentType="text/html" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@ page import="java.util.*" %>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>My Company</title>
</head>

<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/test?user=xxx;password=xxx";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
ResultSet rs1 = null;
%>

<body>
   <div id="wrapper">
         <div id="header">
<h1>Tim Wrafter Associates</h1>
<br/>
<h3>Update Student</h3>

<br/>
<h4>Fill in the details below to Update Student</h4><hr/>
             </div>
             <div id="footer">
<br/>
 <font color="Red"><b><html:errors/></b></font>

<html:form action="/updateStudent">

<table>

<tr>
  <td>Student Id</td>
  <td><html:text property="studentId" readonly="true"/>  </td>
</tr>
<tr>
  <td>First Name</td>
  <td><html:text property="firstName"/></td>
</tr>
<tr>
  <td>Surname</td>
  <td><html:text property="surname"/></td>
</tr>
<tr>
      <td>Position</td>
      <td><html:radio property="position" value="Operator"/>Operator</td>
      <td><html:radio property="position" value="Supervisor"/>Supervisor</td>
</tr>




<tr>
  <td>Date of Birth (dd/mm/yyyy)</td>
  <td><html:text property="dob"/></td>
</tr>
</table>
<br>
////////////////////////////////////////

<tagL:forEach  items="${TESTcompanies}" var="comp">
<html:multibox property="options"><tagL:out value="${comp.value}"/></html:multibox><tagL:out value="${comp.label}"/>
</tagL:forEach>

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


</html:form>

<form action="/Database1/displayMainMenuWithAlarms.do">
   <input type="submit" value="Main Menu">  
</form>
<br>
       </div>       
   </div>

</body>
</html>
////////////////////////////////////////////////////////

Exception stace trace:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.servlet.ServletException: BeanUtils.populate
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

javax.servlet.ServletException: BeanUtils.populate
      org.apache.struts.util.RequestUtils.populate(RequestUtils.java:475)
      org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
      org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalArgumentException: Cannot invoke form.StudentForm.setOptions on bean class 'class form.StudentForm' - argument type mismatch - had objects of type "java.lang.String" but expected signature "java.util.ArrayList"
      org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)
      org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2141)
      org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
      org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
      org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
      org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
      org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
      org.apache.struts.util.RequestUtils.populate(RequestUtils.java:473)
      org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
      org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalArgumentException: argument type mismatch
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155)
      org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2141)
      org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
      org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
      org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
      org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
      org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
      org.apache.struts.util.RequestUtils.populate(RequestUtils.java:473)
      org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
      org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

Apache Tomcat/6.0.29
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 36526600
Excellent -- you posted a lot of good information.  The exception tells you what the problem is -- you have to get the ArrayList, not a String.

However, the example I posted for you uses String[] not ArrayList -- I think that's the way you should go.

By the way, I meant change the variable name, not the "c" taglib label.  So this line:
<%@ taglib prefix="tagL" uri="http://java.sun.com/jstl/core" %>
is exactly what I meant when I said the standard tag library is usually called "c".  Change this back to prefix="c".   But don't name your variables "c" the way you did in this line in the original post:
<c:forEach var="c" items="${companyList}">

I see you've already changed the var name to comp, so that's better.

0
 

Author Closing Comment

by:kieranocallaghan
ID: 36539395
Yep that's it, finally its working. String[] was the way to do it. Thanks mr. coffee!
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36540450
Great -- good luck!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 48
session migration servlets 2 36
Eclipse with gitlab 1 37
maven module vs maven project 3 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now