继续上面的一篇文章
package org.mo.action; import java.util.List; import org.mo.DAO.UserJDBCDAO; import org.mo.model.UserModel; import org.mo.model.UserQueryModel; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private List<UserModel> list; private String[] sexs = new String[] { "男", "女" }; public UserModel userModel; public UserQueryModel uqm; public String toQuery() throws Exception{ return "toQuery"; } public String query() throws Exception{ UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); list = userJDBCDAO.getByCondition(uqm); return "toList"; } public String toList() throws Exception { UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); list = userJDBCDAO.getAll(); return "toList"; } public String toAdd() throws Exception { return "toAdd"; } public String add() throws Exception { UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); userJDBCDAO.create(userModel); return this.toList(); } public String toUpdate() throws Exception{ UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); userModel = userJDBCDAO.getSingle(userModel.getId()); return "toUpdate"; } public String update() throws Exception { UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); userJDBCDAO.update(userModel); return this.toList(); } public String delete() throws Exception{ UserJDBCDAO userJDBCDAO = new UserJDBCDAO(); userJDBCDAO.delete(userModel.getId()); return this.toList(); } public List<UserModel> getList() { return list; } public void setList(List<UserModel> list) { this.list = list; } public String[] getSexs() { return sexs; } public void setSexs(String[] sexs) { this.sexs = sexs; } public UserModel getUserModel() { return userModel; } public void setUserModel(UserModel userModel) { this.userModel = userModel; } public UserQueryModel getUqm() { return uqm; } public void setUqm(UserQueryModel uqm) { this.uqm = uqm; } }
//UserAction-userAdd-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- xwork校验器的DTD信息 --> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3dtd"> <validators> <!-- 字段校验器,校验userModel.id属性 --> <field name="userModel.id"> <!-- 配置必填字符串校验器 --> <field-validator type="int"> <param name="min">1</param> <message>请输入用户编号,类型为整数</message> </field-validator> </field> <!-- 字段校验器,校验userModel.name属性 --> <field name="userModel.name"> <!-- 配置必填字符串校验器 --> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请输入用户名</message> </field-validator> <!-- 长度必须是3到6位 --> <field-validator type="stringlength"> <param name="minLength">3</param> <param name="maxLength">6</param> <message>用户长度为${minLength}到${maxLength}位</message> </field-validator> </field> <field name="userModel.age"> <field-validator type="int"> <param name="min">1</param> <param name="max">200</param> <message>请输入的用户年龄在${min}到${max}之间</message> </field-validator> </field> </validators>
//add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div> <s:form action="userAdd" method="post"> <s:textfield name="userModel.id" label="编号"></s:textfield> <s:textfield name="userModel.name" label="姓名"></s:textfield> <s:select list="sexs" name="userModel.sex" label="性别"></s:select> <s:textfield name="userModel.age" label="年龄"></s:textfield> <s:submit value="添加"></s:submit> </s:form> </div> </body> </html>
//update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>更新</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div> <s:form action="userUpdate" method="post"> <s:textfield name="userModel.id" label="编号"></s:textfield> <s:textfield name="userModel.name" label="姓名"></s:textfield> <s:select list="sexs" name="userModel.sex" label="性别"></s:select> <s:textfield name="userModel.age" label="年龄"></s:textfield> <s:submit value="修改"></s:submit> </s:form> </div> </body> </html>
//list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table> <tr> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>操作</td> </tr> <s:iterator value="list"> <tr> <td><s:property value="id" /></td> <td><s:property value="name" /></td> <td><s:property value="sex" /></td> <td><s:property value="age" /></td> <td><a href="userToUpdate.action?userModel.id=<s:property value='id'/>">更新</a> |<a href="javascript:if(confirm('确定要删除吗?')) window.loaction.href='userDelete.action? userModel.id=<s:property value='id'/>'">删除</a></td> </tr> </s:iterator> </table> <div> <a href="userToAdd">添加用户</a><br /> <a href="userToQuery">查询用户</a>query.jsp </div> </body> </html>
//query.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'query.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:actionerror/> <s:form action="userQuery" method="post"> <s:textfield name="uqm.id" label="编号" value="0"/> <s:textfield name="uqm.name" label="姓名"/> <s:select list="sexs" name="uqm.sex" label="性别" headerKey="" headerValue="请选择"></s:select> <s:textfield name="uqm.age" label="年龄最小值" value="0"/> <s:textfield name="uqm.age2" label="年龄最大值" value="0"/> <s:submit value="查询"/> </s:form> </body> </html>
//UserAction-userQuery-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- xwork校验器的DTD信息 --> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3dtd"> <validators> <validator type="expression"> <param name="expression"> <![CDATA[(uqm.age2 > 0 && uqm.age <= uqm.age2) || uqm.age2 == 0]]> </param> <message>你输入的年龄最小值大于最大值</message> </validator> </validators>
//struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.locale" value="zh_CN" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="mo" extends="struts-default" namespace="/"> <action name="login" class="org.mo.action.Login"> <result name="towelcome">/welcome.jsp</result> <result name="input">/login.jsp</result> </action> <action name="userToAdd" class="org.mo.action.UserAction" method="toAdd"> <result name="toAdd">/add.jsp</result> </action> <action name="userToUpdate" class="org.mo.action.UserAction" method="toUpdate"> <result name="toUpdate">/update.jsp</result> </action> <action name="userToList" class="org.mo.action.UserAction" method="toList"> <result name="toList">/list.jsp</result> </action> <action name="userToQuery" class="org.mo.action.UserAction" method="toQuery"> <result name="toQuery">/query.jsp</result> </action> <action name="userAdd" class="org.mo.action.UserAction" method="add"> <result name="input">/add.jsp</result> <result name="toList">/list.jsp</result> </action> <action name="userUpdate" class="org.mo.action.UserAction" method="update"> <result name="input">/update.jsp</result> <result name="toList">/list.jsp</result> </action> <action name="userDelete" class="org.mo.action.UserAction" method="delete"> <result name="toList">/list.jsp</result> </action> <action name="userQuery" class="org.mo.action.UserAction" method="query"> <result name="input">/query.jsp</result> <result name="toList">/list.jsp</result> </action> </package> </struts>