首页 > 代码库 > struts2&&Hibernate Demo1
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>
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。