hibernate與Struts架構結合編寫簡單針對修改練習

來源:互聯網
上載者:User

標籤:cat   tag   attr   xml檔案   string   lan   result   submit   rri   

失敗頁面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>

  

hibernate與Struts架構結合編寫簡單針對修改練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.