首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts 1.3.8 学习札记(八)

2013-10-25 
Struts 1.3.8 学习笔记(八)第八个版本,完成学生的增删改查操作。?如果我们的Action直接继承struts的Action,

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 {/** * 处理客户端请求 */@Overridepublic 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 {/** * 处理客户端请求 */@Overridepublic 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 {/** * 处理客户端请求 */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {// 得到客户的的提交数据 no,name,sex,age,deptStudentForm 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 {/** * 处理客户端请求 */@Overridepublic 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 {/** * 处理客户端请求 */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {// 得到客户的的提交数据 no,name,sex,age,deptStudentForm 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 {/** * 处理客户端请求 */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {// 得到客户的的提交数据 no,name,sex,age,deptStudentForm 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 {/** * 处理客户端请求 */@Overridepublic 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、成功登录
Struts 1.3.8 学习札记(八)
?

2、新增学生
Struts 1.3.8 学习札记(八)
?

3、修改学生信息
Struts 1.3.8 学习札记(八)
?

4、删除学生

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

?

源代码:

?

热点排行