struts2&&Hibernate Demo1

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   java   sp   

這篇文章和《struts1&&Hibernate Demo1》基本類似,我這裡只是拷貝代碼了。

最核心的代碼:LoginAction.java

package action;import org.hibernate.Session;import org.hibernate.Transaction;import hibernate.HibernateSessionFactory;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {    /**     *      */    private static final long serialVersionUID = 1L;    /* (non-Javadoc)     * @see com.opensymphony.xwork2.ActionSupport#execute()     */    @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        System.out.println("調用LoginAction.execute()");        System.out.println("Struts%% name:"+sname);        System.out.println(1);        Session session=HibernateSessionFactory.getSession();//        Transaction tx=session.beginTransaction();        LoginAction user=(LoginAction) session.load(this.getClass(), 1);        System.out.println(1.1);        System.out.println("hibernate%% user.getName = "+user.getSname());        System.out.println(2);        if(sname.equals(user.getSname())){            System.out.println(3);        return SUCCESS;        }else{            return ERROR;        }    }        // Fields        private Integer sid;        private String sname;        private String email;        // Constructors        /** default constructor */        public LoginAction() {        }        /** full constructor */        public LoginAction(String sname, String email) {            this.sname = sname;            this.email = email;        }        // Property accessors        public Integer getSid() {            return this.sid;        }        public void setSid(Integer sid) {            this.sid = sid;        }        public String getSname() {            System.out.println("調用LoginAction.getSname()");            return this.sname;        }        public void setSname(String sname) {            this.sname = sname;        }        public String getEmail() {            return this.email;        }        public void setEmail(String email) {            this.email = email;        }}

student.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="action.LoginAction" table="student" catalog="test">        <id name="sid" type="java.lang.Integer">            <column name="sid" />            <generator class="increment" />        </id>        <property name="sname" type="java.lang.String">            <column name="sname" length="64" />        </property>        <property name="email" type="java.lang.String">            <column name="email" length="32" />        </property>    </class></hibernate-mapping>
View Code

struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><include file="struts-default.xml"></include>    <package name="struts2" namespace="/" extends="struts-default">        <action name="login" class="action.LoginAction">            <result name="success">/WEB-INF/wel.jsp</result>            <result name="error">/WEB-INF/error.jsp</result></action>    </package></struts>    

其他代碼可以去《struts1&&Hibernate Demo1》中參考。

說說兩者的區別和好處吧,在struts2和Hibernate整合中,由於struts2的action包括了form,使得和Hibernate做邏輯關係簡單了一些,當然action內容太多,分工不是很明確,會比較亂。

學習了spring後,也許有了很好的解決吧。

 

struts2&&Hibernate Demo1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.