`
CoderDream
  • 浏览: 478458 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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>

 

源代码:

0
0
分享到:
评论

相关推荐

    Struts 1.3.8 学习笔记(一)

    在实际开发中,学习 Struts 1.3.8 包括理解其核心概念、配置文件的编写、Action 类的设计、Form Beans 的使用以及 JSP 页面的开发。同时,熟悉其提供的标签库和错误处理机制也是必不可少的。对于那些对源码感兴趣的...

    Struts 1.3.8 学习笔记(二)

    总的来说,Struts 1.3.8 学习笔记会引导你掌握如何利用这个框架构建健壮、可维护的 Web 应用。通过学习 Struts 的核心概念、配置方式以及实践案例,你将能够有效地组织和管理应用程序的结构,提升开发效率。

    Struts 1.3.8 学习笔记(八)

    在博客 "Struts 1.3.8 学习笔记(八)" 中,可能涉及到了更深入的主题,如自定义拦截器(PlugIn)、异常处理、文件上传下载、数据库操作集成等。博主可能还分享了如何使用 Struts 工具进行调试、性能优化以及与 ...

    Struts 1.3.8 学习笔记(五)

    总结来说,Struts 1.3.8 学习笔记涵盖的内容广泛,包括框架的组件使用、配置、源码解读以及工具应用。通过这样的学习,开发者能够熟练掌握 Struts 的核心功能,为构建高效、可维护的 Web 应用奠定基础。

    Struts 1.3.8 学习笔记(四)

    Struts 1.3.8 是 Apache ...总之,Struts 1.3.8 学习笔记会涵盖以上诸多方面,旨在帮助开发者掌握这个框架,提升 Web 应用的开发能力。对于初学者来说,理解并熟练运用这些知识点是迈进 Struts 开发世界的关键步骤。

    Struts 1.3.8 学习笔记(九)

    这篇学习笔记主要针对 Struts 1.3.8 版本进行深入探讨,通过讲解其核心概念、工作原理以及实际应用,帮助开发者更好地理解和使用这一框架。 首先,Struts 1.3.8 的主要特点包括: 1. **MVC架构**:Struts 将应用...

    Struts 1.3.8 学习笔记(三)

    这个版本的学习笔记主要集中在 Struts 框架的核心概念、架构和实际应用上。Struts 1 提供了一个MVC(Model-View-Controller)设计模式的实现,帮助开发者组织应用程序的逻辑,提高代码的可维护性和可扩展性。 在...

    Struts 1.3.8 学习笔记(七)

    本学习笔记将聚焦于 Struts 1.3.8 版本中的核心概念、组件以及在实际开发中的应用。 Struts 框架基于 Model-View-Controller (MVC) 设计模式,它简化了应用程序的结构,使得开发者可以更专注于业务逻辑,而不是底层...

    Struts 1.3.8 学习笔记(六)

    在本学习笔记中,我们将深入探讨 Struts 1.3.8 的核心概念、功能以及实际应用。Struts 为开发者提供了构建动态 Web 应用程序的结构框架,简化了开发流程,增强了代码的可维护性和可扩展性。 首先,我们来了解 MVC ...

    spring学习笔记

    目录 1.1 Spring 框架学习路线:...........................................................................................................................4 1.2 Spring 框架的概述:...........................

Global site tag (gtag.js) - Google Analytics