Struts: Read values from table

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>
kieranocallaghanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrcoffee365Commented:
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
kieranocallaghanAuthor Commented:
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
mrcoffee365Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Amitkumar PSr. ConsultantCommented:
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
kieranocallaghanAuthor Commented:
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
mrcoffee365Commented:
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
kieranocallaghanAuthor Commented:
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
mrcoffee365Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kieranocallaghanAuthor Commented:
Yep that's it, finally its working. String[] was the way to do it. Thanks mr. coffee!
0
mrcoffee365Commented:
Great -- good luck!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.