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

Struts 1.3.8 学习笔记(八)

 
阅读更多

第八个版本,完成学生的增删改查操作。

 

如果我们的Action直接继承struts的Action,即org.apache.struts.action.Action,则我们需要为每个操作创建一个Action,一共需要创建六个Action:

进入增加学生页面的Action:

/MyStruts08/src/com/coderdream/action/student/AddStudentAction.java

删除学生的Action:
/MyStruts08/src/com/coderdream/action/student/DeleteStudentAction.java

保存学生的Action:
/MyStruts08/src/com/coderdream/action/student/SaveStudentAction.java

显示学生列表的Action:
/MyStruts08/src/com/coderdream/action/student/StudentAction.java

更新已存在学生信息的Action:
/MyStruts08/src/com/coderdream/action/student/UpdateStudentAction.java

进入查看学生信息页面的Action:
/MyStruts08/src/com/coderdream/action/student/ViewStudentAction.java

 

AddStudentAction.java

 

package com.coderdream.action.student;

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

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

public class AddStudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// StudentDao studentDao = new StudentDao();
		System.out.println("AddStudentAction");

		return mapping.findForward("success");
	}

}

 
DeleteStudentAction.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.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

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

public class DeleteStudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 得到学生的学号
		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);

		return mapping.findForward("success");
	}

}

 
SaveStudentAction.java

 

 

package com.coderdream.action.student;

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

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

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

public class SaveStudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 得到客户的的提交数据 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!");
		return mapping.findForward("success");
	}

}

 
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.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

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

public class StudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		StudentDao studentDao = new StudentDao();
		List<StudentForm> studentFormList = studentDao.quertAllStudent();

		request.setAttribute("studentFormList", studentFormList);

		return mapping.findForward("success");
	}

}

 
UpdateStudentAction.java

 

 

package com.coderdream.action.student;

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

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

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

public class UpdateStudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 得到客户的的提交数据 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);
		return mapping.findForward("success");
	}

}

 
ViewStudentAction.java

 

 

package com.coderdream.action.student;

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

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

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

public class ViewStudentAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 得到客户的的提交数据 no,name,sex,age,dept
		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("success");
	}

}

 

 

显示学生列表的页面

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="/viewStudent.do" paramId="no"
						paramName="studentForm" paramProperty="no">修改</html:link></td>
				<td><html:link page="/deleteStudent.do" paramId="no"
						paramName="studentForm" paramProperty="no">删除</html:link></td>
			</tr>
		</logic:iterate>
	</table>
	<br>
	<html:link page="/addStudent.do">新增学生</html:link>
</body>
</html:html>

 

 

增加学生的页面

 

<%@ 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.do">返回学生信息页面</html:link>
	| 操作员:
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
	<hr>
	<br> 新增学生
	<hr>
	<html:form action="/saveStudent.do">
		<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>
						<bean:message key="login.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.do">返回学生信息页面</html:link>
	| 操作员:
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
	<hr>
	<br> 修改学生详细信息
	<hr>
	<html:form action="/updateStudent.do">
		<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>
						<bean:message key="login.submit" />
					</html:submit></td>
			</tr>
		</table>
	</html:form>
</body>
</html:html>

 

同时,增加一个首页,用于导航不同的模块,如:学生列表、课程课程等等,对应的Action为IndexAction。

IndexAction.java

package com.coderdream.action;

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

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

public class IndexAction extends Action {

	/**
	 * 处理客户端请求
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("success");
	}

}

 

首页

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.do">学生列表</html:link>
	|课程列表
</body>
</html:html>

 

配置文件

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" type="com.coderdream.action.student.StudentAction"
			input="/index.jsp" scope="request">
			<forward name="success" path="/student.jsp" />
		</action>
		<action path="/deleteStudent"
			type="com.coderdream.action.student.DeleteStudentAction" name="studentForm"
			input="/index.jsp" scope="request">
			<forward name="success" path="/student.jsp" />
		</action>

		<action path="/addStudent" type="com.coderdream.action.student.AddStudentAction"
			input="/student.jsp" scope="request">
			<forward name="success" path="/addStudent.jsp" />
		</action>

		<action path="/saveStudent" type="com.coderdream.action.student.SaveStudentAction"
			name="studentForm" input="/addStudent.jsp" scope="request">
			<forward name="success" path="/addStudent.jsp" />
		</action>

		<action path="/viewStudent" type="com.coderdream.action.student.ViewStudentAction"
			name="studentForm" input="/student.jsp" scope="request">
			<forward name="success" path="/updateStudent.jsp" />
		</action>

		<action path="/updateStudent"
			type="com.coderdream.action.student.UpdateStudentAction" name="studentForm"
			input="/updateStudent.jsp" scope="request">
			<forward name="success" path="/updateStudent.jsp" />
		</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>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>MyStruts08</display-name>
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml
		</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

运行效果:

1、成功登录

 

2、新增学生

 

3、修改学生信息

 

4、删除学生

点击【删除】链接,即可删除学生信息。

 

源代码:

 

  • 大小: 109.6 KB
  • 大小: 185.3 KB
  • 大小: 191.5 KB
0
1
分享到:
评论

相关推荐

    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 学习笔记涵盖的内容广泛,包括框架的组件使用、配置、源码解读以及工具应用。通过这样的学习,开发者能够熟练掌握 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 框架的核心概念、架构和实际应用。在这个版本中,Struts 已经相当成熟,提供了一套完整的 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