Struts 1.3.8 学习笔记(十)

版本十,这个版本我们将Action类型改为DispatchAction类型:

由于这个Action可以接受参数,所以我们前面的多个Action可以合并为一个Action:

新增一个MyBaseAction

MyBaseAction.java

package com.coderdream.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class MyBaseAction extends DispatchAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (request.getSession().getAttribute("userView") == null) {
			return mapping.findForward("login");
		}

		return super.execute(mapping, form, request, response);
	}

}

将多个Action合并为一个Action

StudentAction.java

package com.coderdream.action.student;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.coderdream.action.MyBaseAction;
import com.coderdream.db.StudentDao;
import com.coderdream.form.StudentForm;

public class StudentAction extends MyBaseAction {

	/**
	 * 如果没有传递任何标识参数(如command参数),则默认调用unspecified方法
	 */
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("StudentAction=>>unspecified()");
		// 调用业务逻辑操作
		StudentDao studentDao = new StudentDao();
		List<StudentForm> studentFormList = studentDao.quertAllStudent();
		request.setAttribute("studentFormList", studentFormList);

		return mapping.findForward("listSuccess");
	}

	/**
	 * 添加学生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("StudentAction=>>add()");

		// 得到客户的的提交数据 no,name,sex,age,dept
		StudentForm studentForm = (StudentForm) form;
		studentForm.setNo(studentForm.getNo());
		studentForm.setName(studentForm.getName());
		studentForm.setSex(studentForm.getSex());
		studentForm.setAge(studentForm.getAge());
		studentForm.setDept(studentForm.getDept());

		System.out.println(studentForm);
		StudentDao studentDao = new StudentDao();
		int result = studentDao.saveStudent(studentForm);

		System.out.println("Result: " + result);
		// request.setAttribute("studentFormList", studentFormList);
		System.out.println("Save Student!");

		// 调用业务逻辑操作
		// List studentList = StudentManager.getInstance().findAllStudent();
		// request.setAttribute("studentList", studentList);
		List<StudentForm> studentFormList = studentDao.quertAllStudent();
		request.setAttribute("studentFormList", studentFormList);

		ActionForward af = new ActionForward("student.do", true);
		return af;
	}

	/**
	 * 删除学生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("StudentAction=>>delete()");

		// 得到学生的学号
		StudentForm studentForm = (StudentForm) form;
		String sno = studentForm.getNo();
		System.out.println("sno: " + sno);

		// 删除学生
		StudentDao studentDao = new StudentDao();
		int result = studentDao.deleteStudent(sno);
		System.out.println("Delete Result: " + result);

		// 查询最新的学生列表
		List<StudentForm> studentFormList = studentDao.quertAllStudent();
		request.setAttribute("studentFormList", studentFormList);

		ActionForward af = new ActionForward("student.do", true);
		return af;
	}

	/**
	 * 根据学生代码查询需要修改的学生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward modifyStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("StudentAction=>>modifyStudent()");
		// 获取从页面表单中提交过来的值
		StudentForm studentForm = (StudentForm) form;

		String sno = studentForm.getNo();
		System.out.println("sno: " + sno);

		StudentDao studentDao = new StudentDao();
		StudentForm tempStudentForm = studentDao.quertStudent(sno);

		request.setAttribute("tempStudentForm", tempStudentForm);

		return mapping.findForward("modifyStudent");
	}

	/**
	 * 修改学生
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("StudentAction=>>modify()");
		// 得到客户的的提交数据 no,name,sex,age,dept
		StudentForm studentForm = (StudentForm) form;
		studentForm.setNo(studentForm.getNo());
		studentForm.setName(studentForm.getName());
		studentForm.setSex(studentForm.getSex());
		studentForm.setAge(studentForm.getAge());
		studentForm.setDept(studentForm.getDept());

		System.out.println(studentForm);
		StudentDao studentDao = new StudentDao();
		int result = studentDao.updateStudent(studentForm);

		System.out.println("Result: " + result);
		// request.setAttribute("studentFormList", studentFormList);
		System.out.println("Update Student!");
		request.setAttribute("tempStudentForm", studentForm);
		ActionForward af = new ActionForward("student.do", true);
		return af;
	}

}

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.coderdream.form.LoginForm" />
		<form-bean name="studentForm" type="com.coderdream.form.StudentForm" />
	</form-beans>

	<action-mappings>
		<action path="/login" type="com.coderdream.action.LoginAction"
			name="loginForm" validate="yes" input="/login.jsp" scope="request">
			<forward name="success" path="/index.jsp" />
			<forward name="failure" path="/failure.jsp" />
		</action>

		<action path="/student/student" type="com.coderdream.action.student.StudentAction"
			name="studentForm" scope="request" parameter="command">
			<forward name="listSuccess" path="/WEB-INF/jsp/student/student.jsp" />
			<forward name="modifyStudent" path="/WEB-INF/jsp/student/updateStudent.jsp" />
			<forward name="updateStudent" path="/WEB-INF/jsp/student/updateStudent.jsp" />
			<forward name="deleteStudent" path="/WEB-INF/jsp/student/student.jsp" />
			<forward name="saveStudent" path="/WEB-INF/jsp/student/student.jsp" />
		</action>

		<action path="/student/addStudent" forward="/WEB-INF/jsp/student/addStudent.jsp"
			name="studentForm">
		</action>

		<action path="/index" type="com.coderdream.action.IndexAction"
			scope="request">
			<forward name="success" path="/index.jsp" />
		</action>
	</action-mappings>

	<!-- 加载资源文件 -->
	<message-resources parameter="com.coderdream.resources.application" />
</struts-config>

同时,考虑到安全,我们将student相关的jsp放到WEB-INF文件夹下的jsp/student文件夹中:

更新后的student.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html lang="ture">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
	<html:link page="/index.do">返回首页</html:link>
	| 操作员:
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
	<hr>
	<br> 学生列表:
	<br>
	<table border="1" bgcolor="#11FFCC">
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
			<th>部门</th>
			<th colspan="2">操作</th>
		</tr>
		<logic:iterate id="studentForm" indexId="index" name="studentFormList">
			<tr>
				<td><bean:write name="studentForm" property="no" /></td>
				<td><bean:write name="studentForm" property="name" /></td>
				<td><bean:write name="studentForm" property="sex" /></td>
				<td><bean:write name="studentForm" property="age" /></td>
				<td><bean:write name="studentForm" property="dept" /></td>
				<td><html:link page="/student/student.do?command=modifyStudent"
						paramId="no" paramName="studentForm" paramProperty="no">修改</html:link></td>
				<td><html:link
						page="/student/student.do?command=delete" paramId="no"
						paramName="studentForm" paramProperty="no">删除</html:link></td>
			</tr>
		</logic:iterate>
	</table>
	<br>
	<html:link page="/student/addStudent.do">新增学生</html:link>
</body>
</html:html>

更新后的addStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.coderdream.form.StudentForm"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html lang="ture">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
	<html:link page="/index.do">返回首页</html:link>
	|
	<html:link page="/student/student.do">返回学生信息页面</html:link>
	| 操作员:
	<logic:present name="user" scope="session">
		<bean:write name="user" property="userName" />
	</logic:present>
	<hr>
	<br> 新增学生
	<hr>
	<html:form action="/student/student.do?command=add">
		<table width="100%" border="1" bgcolor="#11FFCC">
			<tr>
				<td>学号</td>
				<td><html:text property="no" value="2013001" /></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td><html:text property="name" value="Lisi" /></td>
			</tr>
			<tr>
				<td>性别</td>
				<td><html:text property="sex" value="female" /></td>
			</tr>
			<tr>
				<td>年龄</td>
				<td><html:text property="age" value="18" /></td>
			</tr>
			<tr>
				<td>部门</td>
				<td><html:text property="dept" value="law" /></td>
			</tr>
			<tr>
				<td></td>
				<td><html:submit>
						保存
					</html:submit></td>
			</tr>
		</table>
	</html:form>
</body>
</html:html>

updateStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.coderdream.form.StudentForm"%>
<%
	StudentForm tempStudentForm = (StudentForm) request.getAttribute("tempStudentForm");
%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html lang="ture">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
	<html:link page="/index.do">返回首页</html:link>
	|
	<html:link page="/student/student.do">返回学生信息页面</html:link>
	| 操作员:
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
	<hr>
	<br> 修改学生详细信息
	<hr>
	<html:form action="/student/student.do?command=modify">
		<table width="100%" border="1" bgcolor="#11FFCC">
			<tr>
				<td>学号</td>
				<td><html:text property="no"
						value="<%=tempStudentForm.getNo()%>" readonly="true"
						disabled="disabled" /></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td><html:text property="name"
						value="<%=tempStudentForm.getName()%>" /></td>
			</tr>
			<tr>
				<td>性别</td>
				<td><html:text property="sex"
						value="<%=tempStudentForm.getSex()%>" /></td>
			</tr>
			<tr>
				<td>年龄</td>
				<td><html:text property="age"
						value="<%=tempStudentForm.getAge().toString()%>" /></td>
			</tr>
			<tr>
				<td>部门</td>
				<td><html:text property="dept"
						value="<%=tempStudentForm.getDept()%>" /></td>
			</tr>
			<tr>
				<td></td>
				<td><html:submit>
						保存
					</html:submit></td>
			</tr>
		</table>
	</html:form>
</body>
</html:html>

更新后的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html:html lang="ture">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
	用户登录成功!操作员:
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
	<hr>
	<html:link page="/student/student.do">学生列表</html:link>
	|课程列表
</body>
</html:html>

源代码:

猜你喜欢

转载自coderdream.iteye.com/blog/1943070