首页 > 代码库 > 东软培训-003

东软培训-003

继续上面的一篇文章

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="http://www.mamicode.com/">

<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="http://www.mamicode.com/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="http://www.mamicode.com/添加"></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="http://www.mamicode.com/">

<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="http://www.mamicode.com/styles.css">
	-->

</head>

<body>
	<table>
		<tr>
			<td>编号</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>操作</td>
		</tr>
		<s:iterator value="http://www.mamicode.com/list">
			<tr>
				<td><s:property value="http://www.mamicode.com/id" />
				</td>
				<td><s:property value="http://www.mamicode.com/name" />
				</td>
				<td><s:property value="http://www.mamicode.com/age" />
				</td>
			</tr>
		</s:iterator>
	</table>
	<div>
		<a href="http://www.mamicode.com/userToAdd">添加用户</a><br />
		<a href="http://www.mamicode.com/#">查询用户</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="http://www.mamicode.com/true" />
	<constant name="struts.locale" value="http://www.mamicode.com/zh_CN" />
	<constant name="struts.i18n.encoding" value="http://www.mamicode.com/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>


东软培训-003