zoukankan      html  css  js  c++  java
  • 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后,也许有了很好的解决吧。

    --------------------
    做一个精神上的素食主义者。
  • 相关阅读:
    UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置
    RSA算法及其在iOS中的使用
    利用UICollectionViewFlowLayout的隐式动画实现UICollectionView的layout的动画调整(外加放大指定cell效果)
    给iOS开发新手送点福利,简述UIPageControl的属性和用法
    给iOS开发新手送点福利,简述UISegment的属性和用法
    给iOS开发新手送点福利,简述UIView的属性和用法
    给iOS开发新手送点福利,简述UIControl事件的用法
    给iOS开发新手送点福利,简述UIAlertView的属性和用法
    给iOS开发新手送点福利,简述UIActivityIndicatorView的属性和用法
    给iOS开发新手送点福利,简述UITextField的属性和用法
  • 原文地址:https://www.cnblogs.com/xfile/p/4093552.html
Copyright © 2011-2022 走看看