In the select tag of struts2, the following attributes are commonly used:
(1) In the select tag of struts2, only one attribute must be set, that is, list.
(2) The list of select tags must contain values. Otherwise, an error is returned. If there is no value, you can add headerkey and headervalue, so that you can pass
Here, even if there is a value in the list, we usually add headerkey and headervalue, that is, the first option displayed in the list. Note: The headerkey cannot be empty or-1.
(3) List attribute: it is usually defined in action and must be a source that can be iterated, such as a list, MAP, set, etc. If it is a map, the map key corresponds to the value in the select tag, and the value in the map corresponds to the option in the select tag. If it is a list or a set, it can be specified through listkey and listvalue.
(4) listkey and listvalue: The listkey corresponds to the value in the select tag, and the listvalue corresponds to the option in the select tag.
(5) name attribute: Select name in the form.
<! ---------------------- End of reprinting ------------------------------------------------>
1. PageProgram
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> student select list </title>
</Head>
<Body>
<S: form name = "form1" Action = "stuvalueselectactiontest">
<S: select
Label = "select student"
List = "liststu"
Name = "selectstudent"
Listkey = "userid"
Listvalue = "username"
Emptyoption = "false"
Value = "3" // The value corresponds to the userid in the listkey, that is, the default value. You can obtain the value of a student from the database and assign it to the value.
/>
<S: Submit/>
</S: Form>
</Body>
</Html>
2. studentlisttest. Java, which is filled in to the list.
Package com. Shangyu. Bean;
Public class studentlisttest {
Private string username;
Private int userid;
Public int getuserid (){
Return userid;
}
Public void setuserid (INT userid ){
This. userid = userid;
}
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}
}
3. The background program assigns a value to list and returns the result to the foreground page (studentselectactiontest. Java)
Package com. Shangyu. Action;
Import com. opensymphony. xwork2.actionsupport;
Import com. Shangyu. Bean .*;
Import java. util .*;
Public class studentselectactiontest extends actionsupport {
Private list liststu;
Public list getliststu (){
Return liststu;
}
Public void setliststu (list liststu ){
This. liststu = liststu;
}
Public String execute ()
{
List liststu = new arraylist <studentlisttest> ();
Studentlisttest stutest = new studentlisttest ();
Stutest. setuserid (1 );
Stutest. setusername ("Xiao Feng ");
Liststu. Add (stutest );
Stutest = NULL;
Stutest = new studentlisttest ();
Stutest. setuserid (2 );
Stutest. setusername (" ");
Liststu. Add (stutest );
Stutest = NULL;
Stutest = new studentlisttest ();
Stutest. setuserid (3 );
Stutest. setusername ("Duan Yu ");
Liststu. Add (stutest );
Setliststu (liststu );
Return success;
}
}
4. Program for value testing (stuvalueselectactiontest. Java)
Package com. Shangyu. Action;
Import com. opensymphony. xwork2.actionsupport;
Public class stuvalueselectactiontest extends actionsupport {
Private string selectstudent;
Public String getselectstudent (){
Return selectstudent;
}
Public void setselectstudent (string selectstudent ){
This. selectstudent = selectstudent;
}
Public String execute ()
{
System. Out. println (getselectstudent (); // print the selected value.
Return success;
}
}
5. Configure XML
<Action name = "studentselecttest" class = "com. Shangyu. Action. studentselectactiontest">
<Result name = "error">/error. jsp </result>
<Result name = "success">/studentselecttest. jsp </result>
</Action>
<Action name = "stuvalueselectactiontest" class = "com. Shangyu. Action. stuvalueselectactiontest">
<Result name = "error">/error. jsp </result>
<Result name = "success">/XX. jsp </result>
</Action>