struts2&&Hibernate Demo1

这篇文章和《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后,也许有了很好的解决吧。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。