失败页面fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
操作失败!
</body>
</html>
页面列表jsp.
<%@page import="com.hanqi.entity.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Student> ls=(List<Student>)request.getAttribute("Studentlist");
for(Student st:ls)
{
out.print(st+"【<a href='deleteStudent?sno="+st.getSno()
+"'>删除</a>】【<a href='updateStudent?sno="+st.getSno()+"'>修改</a>】<br>");
}
%>
</body>
</html>
修改页面jsp
<%@page import="com.hanqi.entity.Student"%>
<%@page import="com.hanqi.service.StudentService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student st=(Student)request.getAttribute("student");
%>
修改界面
<form action="update" method="post">
学号:<input type="text" name="student.sno" value=<%=st.getSno() %> readonly="readonly">
<br><br>
年龄:<input type="text" name="student.age" value=<%=st.getAge() %>>
<br><br>
姓名:<input type="text" name="student.sname" value=<%=st.getSname() %>>
<br><br>
生日:<input type="text" name="student.birthday" value=<%=st.getBirthday() %>>
<br><br>
金额:<input type="text" name="student.money" value=<%=st.getMoney() %>>
<br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Dao包
//查询单条信息 public Student query(int sno) { init(); Student st=(Student)se.get(Student.class, sno); destroy(); return st; } //修改方法 public void update(Student student) { init(); se.saveOrUpdate(student); destroy(); }
service包
//查询单条信息 public Student query(int sno) { return new StudentDao().query(sno); } //修改 public void update(Student student) { new StudentDao().update(student); }
Action类
package com.hanqi.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; import com.hanqi.entity.Student; import com.hanqi.service.StudentService; public class StudentAction implements ServletRequestAware { private HttpServletRequest hsr; //欲模型方式 private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } //查询单挑信息 public String update() { String rtn="fail"; try{ String sno=hsr.getParameter("sno"); Student st=new StudentService().query(Integer.parseInt(sno)); hsr.setAttribute("student", st); rtn="ok"; }catch(Exception e) { e.printStackTrace(); } return rtn; } //修改 public String updateStudent() { String rtn="fail"; try{ new StudentService().update(student); rtn="ok"; }catch(Exception e) { e.printStackTrace(); } return rtn; } @Override public void setServletRequest(HttpServletRequest arg0) { hsr=arg0; }
Struts.xml文件
<!-- 查询单条信息 --> <action name="updateStudent" class="com.hanqi.action.StudentAction" method="update"> <result name="ok">/WEB-INF/pages/update.jsp</result> <result name="fail">/WEB-INF/pages/fail.jsp</result> </action> <!-- 修改 --> <action name="update" class="com.hanqi.action.StudentAction" method="updateStudent"> <result name="ok" type="redirectAction">selectStudent</result> <result name="fail">/WEB-INF/pages/fail.jsp</result> </action>
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" 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>Test21</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<!-- 连接数据库 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">test01</property>
<!-- 数据库方案 -->
<property name="hibernate.default_schema">TEST01</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 调试 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 自动建表方式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 注册映射文件 -->
<mapping resource="com/hanqi/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>