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

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

  

转载于:https://www.cnblogs.com/tfl-511/p/6072983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值