You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by red phoenix <ro...@gmail.com> on 2008/03/02 12:03:39 UTC

struts2 question

I use struts2.0.11, I use Hibernate to get data and put it into attribute in
struts2 action,like follows:
public String show() throws Exception{
  request=ServletActionContext.getRequest();
  List myList=this.getHibernateTemplate().find("from test.MyModel");
  request.setAttribute("myList",myList);
  return SUCCESS;
 }

Hibernate mapping file is follows:
<hibernate-mapping>
 <class name="test.MyModel" table="Test">
  <id name="id" column="id" type="java.lang.String">
   <generator class="assigned"/>
  </id>
  <property name="cname" type="java.lang.String">
   <column name="name" length="50" not-null="false" />
  </property>

Then I want to show myList by using <s:select> tag,like follows:
<s:select label="Myname" headerKey="-1" headerValue="choose"
list="%{myList}" listKey="MyModel.id" listValue="MyModel.cname" />

When I run it,it raise following error:
org.apache.jasper.JasperException: tag 'select', field 'list': The requested
list key '%{myList}' could not be resolved as a
collection/array/map/enumeration/iterator type. Example: people or
people.{name} - [unknown location]

I have searched google for several days,but I don't find how to solve it. I
am puzzled with it! I want to know how to correct my <s:select> code?
Any idea will be appreciated!
Best regards
Phoenix

Re: struts2 question

Posted by Dave Newton <ne...@yahoo.com>.
--- red phoenix <ro...@gmail.com> wrote:
> I use struts2.0.11, I use Hibernate to get data and put it into attribute
> in
> struts2 action,like follows:
> public String show() throws Exception{
>   request=ServletActionContext.getRequest();
>   List myList=this.getHibernateTemplate().find("from test.MyModel");
>   request.setAttribute("myList",myList);
>   return SUCCESS;
>  }
> 
> Hibernate mapping file is follows:
> <hibernate-mapping>
>  <class name="test.MyModel" table="Test">
>   <id name="id" column="id" type="java.lang.String">
>    <generator class="assigned"/>
>   </id>
>   <property name="cname" type="java.lang.String">
>    <column name="name" length="50" not-null="false" />
>   </property>
> 
> Then I want to show myList by using <s:select> tag,like follows:
> <s:select label="Myname" headerKey="-1" headerValue="choose"
> list="%{myList}" listKey="MyModel.id" listValue="MyModel.cname" />

You're referring to the property as a value on the value stack, which it is
not. You'd need to either use JSP EL notation, ${myList}, or make myList a
property of the action class. I would recommend the latter, since that would
be canonical S2, and almost every single piece of documentation uses S2 in
that fashion.

> I have searched google for several days,but I don't find how to solve it.

You should also look at the S2 documentation wiki.

>   List myList=this.getHibernateTemplate().find("from test.MyModel");

I would also be wary of tying your action code so closely to Hibernate. In
general it's considered better implementation to put functionality like this
into a service object. This allows the injection of the service object into
the action via any of several IoC methodologies and makes it easier to test
the action in isolation.

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org