Struts2的select使用
(1)struts2中的select 标签中,必须设置的属性只有一个,即是list。
(2)select标签的list中必须有值,不然会报错。如果没有值,可以加上headerKey, headerValue,这样就可以通过了
其中,即使list中有值,我们也通常会加上headerKey, headerValue,即列表中显示的第一个选项。注意,headerKey不可以为空,不可以为-1
(3)list属性:通常是在action中定义的,必须是一个可以迭代的源,如为一个List, Map, Set等。如果是一个Map,那么map的key就对应于select标签中的value,map中的value就对应于select标签中的option。而如果是一个List或者是一个Set,那么可以通过listKey和listValue指定。
(4)listKey和listValue:listKey即对应于select标签中的value, listValue即对应于select标签中的option。
(5)name属性:即是表单中select的名称。
<!----------------------转载结束---------------------------------------------------->
1.页面程序
<%@ 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="选择学生"
list="liststu"
name="selectStudent"
listKey="userid"
listValue="username"
emptyOption="false"
value="3" //这里的value值对应于listKey中的userid,即为默认值,从数据库中取到某学生的值,赋予value即可
/>
<s:submit/>
</s:form>
</body>
</html>
2. 学生信息基础类 (StudentListTest.java),填充至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.后台程序赋值给list传回前台页面 (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("萧峰");
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("段誉");
liststu.add(stutest);
setListstu(liststu);
return SUCCESS;
}
}
4.取值测试的程序(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()); //打印出选择的值
return SUCCESS;
}
}
5. 配置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>