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
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
</ 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) (
this.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 Register.java. 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
<s:param getForm </ s: param>
</ s: url>
<s:a 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.