I learned the struts2 component yesterday. An error is reported when the select label is used.
1. Page code
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
<Body>
<Div id = "Glo" style = "width: 200px">
<H3>
Select2
</H3>
<S: Form Action = "../select2.action">
<S: select list = "userlist" listkey = "ID" listvalue = "firstname"> </S: Select>
</S: Form>
</Div>
</Body>
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
2. pojo class
Private integer ID;
Private string firstname;
Private string lastname;
Private string password;
Private string description;
Public user (){}
Public user (integer ID, string lastname ){
This. ID = ID;
This. lastname = lastname;
}
Public String getdescription (){
Return description;
}
Public void setdescription (string description ){
This. Description = description;
}
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}
Private int age;
Public integer GETID (){
Return ID;
}
Public void setid (integer ID ){
This. ID = ID;
}
Public String getfirstname (){
Return firstname;
}
Public void setfirstname (string firstname ){
This. firstname = firstname;
}
Public String getlastname (){
Return lastname;
}
Public void setlastname (string lastname ){
This. lastname = lastname;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
3. Action-class Java code
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
/***
*
*
* Component Learning
* @ Return
*/
Private user;
Private userservice service;
Private string usertype;
Private string username;
Private list <user> userlist;
Private Final Static logger log = logger. getlogger (component. Class );
Public list <user> getuserlist (){
Return userlist;
}
Public void setuserlist (list <user> userlist ){
This. userlist = userlist;
}
Public static logger getlog (){
Return log;
}
Public user getuser (){
Return user;
}
Public void setuser (User user ){
This. User = user;
}
// Select
Public String select2 (){
List <user> userlist = new arraylist <user> ();
// Identify all users from Dao
Userlist = service. findall ();
Log.info ("userlist's value:" + userlist );
Return "back ";
}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
4. struts2.xml
<Action name = "select2" class = "select2action" method = "select2">
<Result name = "back">/components/select2.jsp </result>
</Action>
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Page Error
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Tag 'select', field 'LIST': the requested list key 'userlist' could not be resolved as a collection/array/MAP/enumeration/iterator type. example: people or people. {name}-[unknown location]
At org. Apache. struts2.components. component. fielderror (component. Java: 231)
At org. Apache. struts2.components. component. findvalue (component. Java: 293)
At org. Apache. struts2.components. listuibean. evaluateextraparams (listuibean. Java: 79)
At org. Apache. struts2.components. Select. evaluateextraparams (select. Java: 99)
At org. Apache. struts2.components. uibean. evaluateparams (uibean. Java: 780)
At org. Apache. struts2.components. uibean. End (uibean. Java: 481)
At org. Apache. struts2.views. jsp. componenttagsupport. doendtag (componenttagsupport. Java: 43)
At org. Apache. jsp. components. select2_jsp. _ jspx_meth_s_005fselect_005f0 (select2_jsp.java: 155)
At org. Apache. jsp. components. select2_jsp. _ jspx_meth_s_005fform_005f0 (select2_jsp.java: 120)
At org. Apache. jsp. components. select2_jsp. _ jspservice (select2_jsp.java: 79)
At org. Apache. Jasper. runtime. httpjspbase. Service (httpjspbase. Java: 70)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 717)
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Solution: Cause of analysis. It may be that the value of list is null and userlist = service. findall (); this line of code does not have setuserlist (), resulting in the attribute userlist and the value on the page being blank. Modify the action class as follows:
// Select
Public String select2 (){
List <user> userlist = new arraylist <user> ();
// Identify all users from Dao
Userlist = service. findall ();
Log.info ("userlist's value:" + userlist );
// Set the attribute value
This. setuserlist (userlist );
Return "back ";
}
Solve the problem ...................
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------