struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <>
Subject Re: struts2 <s:select> question
Date Sun, 02 Mar 2008 15:39:00 GMT
--- red phoenix <> 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="" 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.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message