Struts2 's Select Tag Usage example Analysis _jsp programming

Source: Internet
Author: User

The example in this article describes the Select label usage of STRUTS2. Share to everyone for your reference. Specifically as follows:

A small problem is encountered in the project, summed up.

About the use of the STRUTS2 select tag.

Struts2 the data from other tables to fill the Drop-down menu with the <s:select> tab display.

The struts2 version is 2.1.8

<s:select
List= ""
Name= ""
Value= ""
Headerkey= ""
Headervalue= ""
Listkey= ""
Listvalue= ""
/>
(1) list is a must-have property list can store the map list set type of data

List property: Usually defined in action, must be a source that can be iterated, such as a list, Map, set, and so on. If it is a map, then the key of the map corresponds to the value in the Value,map in the Select tab corresponding to option in the Select label. If it is a list or a set, it can be specified by Listkey and ListValue.

(2) If the list is empty Headkey and headvalue cannot be empty. Listkey and Listvalue:listkey correspond to the value in the Select label, and listvalue corresponds to option in the Select label

(3) Name property: That is, the names of the select in the form.

The following details explain the meaning of each property.

Headervalue: Default Value
Headerkey: Default name
List: source data (You can get the list from the database in the action method)
Name: location where the selected data is stored (here we < define the object > in the action in the attribute value username of object obj)
Listkey: Name of the dropdown option
ListValue: Value of the dropdown option
Value: Default value

1. First example:

Copy Code code as follows:
<s:select list= "{' AA ', ' BB ', ' cc '}" theme= "simple" headerkey= "headervalue=" ></s:select>

2. Second example:
Copy Code code as follows:
<s:select list= "#{1: ' AA ', 2: ' BB ', 3: ' CC '}" label= "abc" listkey= "Key" listvalue= "value" headerkey= "0" headervalue= " Aabb ">

3. A third example:

<%
Java.util.HashMap map = new Java.util.LinkedHashMap ();
Map.put (1, "AAA");
Map.put (2, "BBB");
Map.put (3, "CCC");
Request.setattribute ("map", map);
Request.setattribute ("AA", "2");
%>
<s:select list= "#request. Map" label= "abc" listkey= "Key" listvalue= "value"
 value= "#request. AA" headerkey= "0" headervalue= "aabb" ></
s:select
>

Headerkey Headervalue to set the default value
4. Fourth example

public class Program implements Serializable {
  /**  serialversionuid * *
  private static final long Serialversionuid = 1L;
  private int programid;
  Private String programname;
  public int Getprogramid () {return
    programid;
  }
  public void Setprogramid (int programid) {
    this.programid = ProgramID;
  }
    Public String Getprogramname () {return
    programname;
  }
  public void Setprogramname (String programname) {
    this.programname = programname;
  }
}

 Xxxextends extends Actionsupport {
  private list<program> programs;
    Public list<program> Getprograms () {return
    programs;
  }
  public void Setprograms (list<program> programs) {
      this.programs = programs;
  }
}

On the JSP page

<s:select list= "Programs" listvalue= "ProgramName" listkey= "ProgramID" name= "program" id= "program"
headerkey= "0l" headervalue= "" value= "Bean.programid" ></s:select> 

Among them: list= "Programs" is the List,listvalue= "ProgramName" for the action inside the <option value= "xxx" >value</option> Corresponding to the fields inside the bean programname
Listkey= "ProgramID" is <option value= "xxx", corresponding to the field in the Bean programid,value= "Bean.programid" for the set Select selected value, S:select will automatically select the value for key in the bean

I hope this article will help you with the design of struts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.