首页 技术 正文
技术 2022年11月18日
0 收藏 367 点赞 4,805 浏览 5364 个字

失败页面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>

  hibernate与Struts框架结合编写简单针对修改练习

修改页面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>

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896