Struts2标签之<s:select>
根据MVC设计模式,JSP页面是不能直接访问的,而是需要通过Servlet或者Struts2中的action进行跳转访问;
<s:select>标签的内容并不能像HTML一样,直接在页面中设置,而是需要通过读取Action的属性来进行设置;
在HTML中,代码如下:
<form> <select name="name"> <option value="v1">v1</option> <option value="v2">v2</option> </select> </form>
而在Struts2中,<s:select>的流程如下:
Action:
public class ConfigCustomerInfoAction extends ActionSupport { private List<CustomerInfo> typeList = new ArrayList<CustomerInfo>(); public String execute()throws Exception{ CustomerInfo info1 = new CustomerInfo(); info1.setTypeName("normal"); info1.setTypeValue("普通用户"); typeList.add(info1); CustomerInfo info2 = new CustomerInfo(); info2.setTypeName("special"); info2.setTypeValue("会员用户"); typeList.add(info2); return SUCCESS; } public List<CustomerInfo> getTypeList() { return typeList; } public void setTypeList(List<CustomerInfo> typeList) { this.typeList = typeList; } }
struts.xml
<action name="configCustomerInfo" class="org.xiazdong.action.ConfigCustomerInfoAction"> <result>/fillCustomerInfo.jsp</result> </action>vo
package org.xiazdong.vo; public class CustomerInfo { private String typeName; private String typeValue; public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public String getTypeValue() { return typeValue; } public void setTypeValue(String typeValue) { this.typeValue = typeValue; } }JSP
<s:select list="typeList" name="type" listKey="typeName" listValue="typeValue" label="用户类型"></s:select>
注意点:
(1)各种getter和setter方法都要有,毕竟Struts2框架就是依靠反射的。