Struts 2.0 solution of problems

Struts 2.0 <s:select> solution of problems

Today, the use of Struts 2.0 <s:select> encountered an error when the label:

tag 'select', field 'list', name 'university': The requested list key 'universities' could not be resolved as a collection / array / map / enumeration / iterator type.

google, baidu to find a lot of ways, all can not be solved, then finally found where the problem lies, in fact, is a small problem, to sum up here, so that we will appear similar mistakes in the future.
<s:select> is a drop-down list of labels, there are a variety of uses, such as direct specify a list of its assignment:

<s:select name="university" headerKey="-1" headerValue=" Please select the " list="{' South China University of technology ',' Huazhong University ',' Fuzhou University '}"/>

Sometimes, we need to get from the database inside the so-called "data dictionary", fill the drop-down list,

Let's look at a specific code it:

register.jsp part of the code snippet:

[Copy to clipboard] [-] CODE:
<form action="register.action" method="POST">
<s:select name="university" headerKey="-1" headerValue=" Please select the " list="universities" listKey="uniId" listValue="uniName"/>
</ form>
list attribute specifies a variable universities, this variable is a register of this action a member of the property, its type is List <University>:

[Copy to clipboard] [-] CODE:
... ... ... ...

public class Register extends ActionSupport (
private List <University> universities;

public List <University> getUniversities () (
return universities;

public void setUniversity (String university) ( = university;

@ Override
public String execute () throws Exception (
universities = uniService.getUniversityList ();
... ... ... ...
Omitted here, a lot of things, but to illustrate the usage of select tag, uniService.getUniversityList () can be obtained from the database a list of a University, where omitted uniService statements and AutoWire. Of course, in addition to use List, you can also use Map.

struts.xml and Spring configuration file to register the action is mapped to Specifically how the integration of Struts 2.0, Spring and Hibernate, a lot of online tutorials, you can also refer to Netbeans under:

Omitted here for details.

If we run directly register.jsp, will receive the error described in the beginning we have, in fact, the reason is very simple, because for the first time when running the page, Register of this action has not been instantiated, so there is no universities in this variable. Then how should we do? Is very simple, first of all, in the register.jsp preceded by a page, for example index.jsp:

[Copy to clipboard] [-] CODE:
<s:url action="register">
<s:param name="type"> getForm </ s: param>
</ s: url>
<s:a href="%{regForm}"> Register </ s: a>
This page which is very simple, defines a url, the url will be submitted to register the action, and then define a hyperlink pointing to this url. Thus, when the user clicks the hyperlink time, action will be instantiated, universities can also get this variable. What type of this attribute is the use? Let's look at the action we need to do some changes to the execute method:

[Copy to clipboard] [-] CODE:
public String execute () throws Exception (
if (type! = null & & type.equals ( "getForm")) (
universities = uniService.getUniversityList ();
return INPUT;

if (type! = null & & type.equals ( "submit")) (
... ... ... ...
As we will once again of the form may be submitted (such as registration), it is important to distinguish is to initialize the page, or to submit the page. Using an attribute to differentiate on it. Of course, we also need to register.jsp which declared such a property, or when the form is submitted once again this variable did not get the:

[Copy to clipboard] [-] CODE:
<input type="hidden" name="type" value="submit"/>
Finally, run index.jsp, click on the hyperlink, the page will be able to show properly.
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Struts 2.0 solution of problems

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

  • Based on Spring's Hibernate Search full-text search function of sample

    Database: Oracle 9i JDBC Driver: OJDBC14 Development Environment: Eclipse-JEE Spring version: Spring 2.0.6 Hibernate version: Hibernate Core 3.2.5/Hibernate Annotation 3.3.0/Hibernate Validator 3.0.0/Hibernate Search 3.0.0 Beta4 / / (JDBC

  • Some interview questions java

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug

  • spring + hibernate + oracle9i to read and write CLOB

    Database-driven update classes12-9i.jar Hibernate modify the configuration of the following code <bean/> <bean Lazy-init="true"> <property name="nativeJdbcExtractor"> <ref local="nativejdbcExtractor"/>

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • hibernate generic generic DAO

    package org.lzpeng.dao; import; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries