继续上面的一篇文章
package org.mo.action; import java.util.List; import org.mo.DAO.UserJDBCDAO; import org.mo.model.UserModel; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private List<UserModel> list; private String[] sexs = new String[] { "男", "女" }; public UserModel userModel; 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 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; } }
//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>
//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> </tr> <s:iterator value="list"> <tr> <td><s:property value="id" /> </td> <td><s:property value="name" /> </td> <td><s:property value="age" /> </td> </tr> </s:iterator> </table> <div> <a href="userToAdd">添加用户</a><br /> <a href="#">查询用户</a> </div> </body> </html>
//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="userToList" class="org.mo.action.UserAction" method="toList"> <result name="toList">/list.jsp</result> </action> <action name="userAdd" class="org.mo.action.UserAction" method="add"> <result name="toList">/list.jsp</result> </action></package> </struts>
时间: 2024-10-11 07:49:01