<html:options>和<html:optionsCollection>都要与<html:select>标签搭配使用
1.<html:options>标签
a.先写一个bean
package com.hsp.bean;
public class Student {
private String key;//学号
private String value;//姓名
public Student(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
b.在jsp页面中添加如下内容
<%
ArrayList list = new ArrayList();
list.add(new Student("201004070117","show value1"));
list.add(new Student("201004070118","show value2"));
list.add(new Student("201004070119","show value3"));
list.add(new Student("201004070120","show value4"));
pageContext.setAttribute("valuelist",list);
%>
<html:form action="/test.do">
<html:select property="studentCode"> //property要与ActionForm中的属性匹配
<%-- collection是集合的名字,存在于pageContext, request, session,application中
key和value与student Bean 中的属性名字一样--%>
<html:options collection="valueslist" property="key" labelProperty="value"/>
</html:select>
</html:form>
2.<html:optionsCollection>
a.编写一个ActionForm
package com.hsp.form;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.hsp.bean.Student;
public class RegisterForm extends ActionForm{
private String username;
private String password;
private List<Student> areas;
private String area;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//简单的数据验证
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
//ActionErrors继承了ActionMessages
if("".equals(username)){
actionErrors.add("errorUsername", new ActionError("error.username.blank"));
}
if("".equals(password)){
actionErrors.add("errorPassword", new ActionError("error.password.blank"));
}
if(0 == age){
actionErrors.add("errorAge", new ActionError("error.age.blank"));
}
return actionErrors;
}
public List<Student> getAreas() {
return areas;
}
public void setAreas(List<Student> areas) {
this.areas = areas;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
b.在action中添加
public class RegisterAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
RegisterForm registerForm = (RegisterForm)form;
List<Student> areas = new ArrayList<Student>();
areas.add(new Student("hubei", "1" ));
areas.add(new Student("wuhan", "2"));
areas.add(new Student("zhejiang","3"));
registerForm.setAreas(areas);
return mapping.findForward("success");
}
}
c.在jsp页面中添加
<%-- <html:select name="registerForm" property="areatest" value="area">这样可以使areatest的默认值为registerForm对象中的area属性值 --%>
<html:select name="registerForm" property="area">
<%--property是name所指定的对象registerForm中属性。如果name是一个集合,那么可以省略property的配置 --%>
<html:optionsCollection name="registerForm" property="areas" value="value" label="key"/>
</html:select>