1.搭建环境
eclipase:Luna Release (4.4.0)
struts2 :2.3.28 下载地址:http://download.csdn.net/detail/chuck_kui/9513090
2. 创建类
① 创建类
Role类
package com.baidu.struts2.application.onlineusers; public class Role { private Integer roleId; private String roleName; public Integer getRoleId() { return roleId; } public void setRoleId(Integer roleId) { this.roleId = roleId; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public Role(Integer roleId, String roleName) { super(); this.roleId = roleId; this.roleName = roleName; } public Role() { super(); } }Department类
package com.baidu.struts2.application.onlineusers; public class Department { private Integer deptId; private String deptName; public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public Department() { super(); } public Department(Integer deptId, String deptName) { super(); this.deptId = deptId; this.deptName = deptName; } }
package com.baidu.struts2.application.onlineusers; import java.util.ArrayList; import java.util.List; public class Dao { private List<Department> departments; public List<Department> getDepartments(){ departments = new ArrayList<Department>(); departments.add(new Department(1001,"销售部")); departments.add(new Department(1002,"营运部")); departments.add(new Department(1003,"教学部")); return departments; } private List<Role> roles; public List<Role> getRoles(){ roles =new ArrayList<Role>(); roles.add(new Role(2001, "教师") ); roles.add(new Role(2002, "教授") ); roles.add(new Role(2003, "博士生导师") ); return roles; } }
参加一个Action
package com.baidu.struts2.application.onlineusers; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; public class Employee implements RequestAware { Dao dao = new Dao(); private Map<String ,Object> requestMap; @Override public void setRequest(Map<String, Object> arg0) { this.requestMap = arg0; } public String input(){ requestMap.put("depts", dao.getDepartments()); requestMap.put("roles", dao.getRoles()); return "input"; } }
创建一个index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="emp-input">emp-input </a> </body> </html>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,do,,"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="emp-*" class="com.baidu.struts2.application.onlineusers.Employee" method="{1}"> <result name="{1}">/pages/emp-{1}.jsp</result> </action> </package> </struts>
创建emp-input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <br><br> <s:form action="emp-save"> <s:hidden></s:hidden> <s:textfield name="name" label="Name"></s:textfield> <s:password name="password" label="Password"></s:password> <!-- 多选一 也就是单选框 --> <s:radio name="gender" label="Gender" list="#{'1':'Male','0':'Female'}" ></s:radio> <!-- 下拉框 --> <s:select name="dept" label="Department" list="#request.depts" listKey="deptId" listValue="deptName"> </s:select> <!-- 一组复选框 --> <s:checkboxlist name="roles" label="Role" list="#request.roles" listKey="roleId" listValue="roleName"> </s:checkboxlist> <!-- 文本域 --> <s:textarea name="desc" label="Desc"></s:textarea> <s:submit></s:submit> </s:form> </center> </body> </html>
完善 Action
package com.baidu.struts2.application.onlineusers; import java.util.List; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; public class Employee implements RequestAware { Dao dao = new Dao(); private String name; private String password; private String gender; private String dept; private List<String> roles; private String desc; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public List<String> getRoles() { return roles; } public void setRoles(List<String> roles) { this.roles = roles; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } private Map<String ,Object> requestMap; @Override public void setRequest(Map<String, Object> arg0) { this.requestMap = arg0; } public String input(){ requestMap.put("depts", dao.getDepartments()); requestMap.put("roles", dao.getRoles()); return "input"; } public String save(){ System.out.println("save: " + this); return "save"; } @Override public String toString() { return "Employee [name=" + name + ", password=" + password + ", dept=" + dept + ", gender=" + gender + "]"; } }
创建接收页面JSP
emp_save.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <br><br> Name:${name } <br><br> Password:${password } <br><br> Gender:${gender } <br><br> Dept:${dept } <br><br> Roles:${roles } <br><br> DESC:${desc } <br><br> </center> </body> </html>
输入:
显示结果:
注意:构造Map 的格式
<s:radio name="gender" label="Gender" list="#{'1':'Male','0':'Female'}" ></s:radio>