Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Struts2 Java Function Usage

 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys-

I am starting on a new web project here at work that I've decided to use Struts2 for. I wrote my last project in straight JSP with servlets, and I wanted something a bit more structured this time, with easier form management. I ordered a Struts2 book, but it hasn't come in yet, and in the meantime I've been trying to tinker around with the framework to try to get a feel for it.

I've been trying to make a checkboxlist but I can't seem to get the tag to read my array variable right.

What do I all need to have in my Java classes to make a variable "readable" by the Struts tags in JSP?

Right now, I have 2 classes, one (Register) which extends ActionSupport and provides the interface to modifying the Person class from the JSP (or so I gather). In the person class, I have a String array and a getter for it (no setter, since I just want it to display a few options). When I try to create a checkboxlist, I call:


I get an exception saying that:
tag 'checkboxlist', field 'list', name 'Test List': The requested list key 'personBean.testList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location].

Am I missing something that's required to access my testList array? It's declared as a String[] and filled in the normal manner.

Here's my Person code (I'm using http://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Framework as my basis, and modifying the code):



Thanks for any help you can give me!
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts2 uses the name attribute to specify the property that you want to use to display which fields in the list have been selected. So its cheching your action class for the property and it isn't finding anything. Change the name attribute to reference a property and I think this will work better.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Firstof all your list name comes from Action which is not contains space.

in your jsp write

<s:checkboxlist name="testList" list="testList" />


Your Action class looks like this.
private String[] testList;

then getter and setter method


/**
* @return the testList
*/
public String[] getTestList() {
return testList;
}

/**
* @param testList the testList to set
*/
public void setTestList(String[] testList) {
this.testList = testList;
}

one method which gives Struts.xml to which jsp include

public String excecute(){

return "success";
}



Thanks,
Nishan Patel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic