Struts2 Spring hibernate 整合示例
步骤:
1.创建一个WEB工程,命名为BookShop(名字自己取,此处为示例工程名);
2.导入struts2的核心jar包,也就是在WEB-INF下的lib文件夹中放入jar包文件,
freemaker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
struts2-spring-plugin-2.0.14.jar
xwork-2.0.7.jar
3.加载spring,选中工程名,右击并选择【MyEclipse】选项下的【Add Spring Capabilities...】(或者在工具栏中选择【MyEclipse】下的【Add Spring Capabilities...】),在弹出的对话框中进行如下选择:
Spring 2.5 AOP Libraries
Spring 2.5 Core Libraries
Spring 2.5 Persistence Core Libraries
Spring 2.5 PersistenceJDBC Libraries
Spring 2.5 Web Libraries
点击【Next】,点击Folder文本框后的【Browse】,在弹出对话框中选择spring配置文件存储位置为WEB-INF文件夹:
4.加载Hibernate,选择【MyEclipse】下的【Add Hibernate Capabilities...】,在弹出对话框中进行如下所示操作:
全选 MyEclipse Libraries, 然后选择 Copy checked Library.......
5.创建struts.xml文件,在src包下新建一个struts.xml文件,该文件内容如下:
<!DOCTYPE struts PUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<!--下述语句是将struts2交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<!--导入struts-default.xml文件 -->
<include file="struts-default.xml"></include>
<!--将action内容放在package元素下,package元素的name值与extends值可以使用默认值,如下所示 -->
<package name="login" namespace="/login" extends="struts-default">
<!--例如与用户登录相关的LoginAction,配置如下,其中,class值与applicationContext.xml文件中该action的id值相对应,name值作为标识-->
<action name="loginAction" class="LoginAction">
<!--result元素设置页面跳转值,其中name值为目标页面的字符串标识,如下,登录失败时回到login.jsp页面,成功时,转向success.jsp页面 -->
<result name="fail">/login.jsp </result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
6.配置web.xml文件,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param><filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter><filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list><listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener></web-app>
7.反向生成实体类文件
在src包下新建一个entity包,将前面数据源xiaoqi所示数据库bookshop中的user表反向生成Hibernate实体类文件。
8.创建数据操作文件
在src包下创建dao包,在该包下新建类文件PersonManage.java,其内容如下所示:
package dao;
import java.util.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import entity.*;
public class PersonManage extends HibernateDaoSupport {
//普通用户登录验证
@SuppressWarnings("unchecked")
publicbooleancheckUser(StringuserName,StringuserPassword){
booleanflag=false;
Stringhql="fromUserasuserwhereuser.userName='"+userName+"'anduser.userPassword='"+userPassword+"'";
List<User>userList=this.getHibernateTemplate().find(hql);
if(userList.size()>0){
flag=true;
}
returnflag;
}}
9.创建Action
在src包下新建一个包,名为"com.xiaoqi.action"(名字仅为示例,只写一个com也可以,示例写法是采取struts中的action包名写法),在该包下新建一个action类LoginAction.java,其内容如下:
package com.xiaoqi.action;
import com.opensymphony.xwork2.ActionSupport;
import dao.PersonManage;
public class LoginAction extends ActionSupport{
private String userName;
privateStringuserPassword;
privatePersonManagepersonManage;
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetUserPassword(){
returnuserPassword;
}
publicvoidsetUserPassword(StringuserPassword){
this.userPassword=userPassword;
}
publicvoidsetPersonManage(PersonManagepersonManage){
this.personManage=personManage;
}
publicStringexecute(){
Stringpage="fail";
booleanflag=false;
flag=personManage.checkUser(userName,userPassword);
if(flag){
page="success";
}
returnpage;
}
}10.创建页面,创建登录页面login.jsp,内容如下:
<%@ page language="java" pageEncoding="gb2312"%>
<!-- 导入struts2标签库 --><%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<center><div id="login">
<s:formaction="loginAction">
<s:textfieldlabel="用户名"name="userName"></s:textfield>
<s:passwordlabel="密码"name="userPassword"></s:password>
<s:submitvalue="登录"></s:submit>
</s:form>
</div></center>
</body>
</html>页面中,先导入struts2标签库,用来创建form表单。form表单提交给与登录相关的LoginAction.java文件,而LoginAction.java在struts.xml文件中相应的name值为"loginAction”,所以这里的action值为"loginAction"。至于用户名文本框与密码文本框的name值则与LoginAction.java文件中的userName与userPassword完全匹配。
11.配置applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 以下为数据源配置,自动生成 -->
<bean id="candy" class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<propertyname="url"
value="jdbc:mysql://localhost:3306/bookstore">
</property>
<propertyname="username"value="root"></property>
<propertyname="password"value="sa"></property>
</bean><!-- 以下为sessionFactory配置,自动生成 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="candy"/>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<propertyname="mappingResources">
<list>
<value>entity/User.hbm.xml</value></list>
</property></bean><!-- 配置PersonManage.java类文件 -->
<bean id="personManage" class="dao.PersonManage" abstract="false"
lazy-init="default"autowire="default"dependency-check="default">
<propertyname="sessionFactory">
<reflocal="sessionFactory"/>
</property>
</bean><!-- 配置LoginAction.java类文件 -->
<bean id="LoginAction" class="com.xiaoqi.action.LoginAction" >
<property name="personManage">
<refbean="personManage"/>
</property></bean></beans>