gcw633

s2sh整合(二)

 

s2sh整合总结(struts2.1.8-spring2.5-hibernate3.2

1. 创建工程


2. 建好包架构
 

配置Struts2.0

3. 加入struts2
Commons-logging-1.0.4.jarFreemarker-2.3.13.jarOgnl-2.6.11.jarStruts2-core-2.1.6.jarXwork-2.1.2.jar,另外sqljdbc.jarcommons-fileupload-1.2.1.jar

4. 修改WEB-INF下的web.xml文件,增加struts2的配置
    <!-- struts2过滤器 -->

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

        </filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

</filter-mapping>

5.添加struts配置文件。WEB-INF/classes目录下,新建struts.xml,模版如下:

         <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>
</struts>

6,测试看是否配置成功

7.配置一个Action

      首先新建一个登陆页面login.jsp,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

 <head>

    <title>登录</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

 </head>

 <body>

   <s:form name="form1" action="login" >

       <s:textfield name="username" label="username" ></s:textfield>

       <s:password name="password" label="password" ></s:password>

       <s:submit label="submit"></s:submit>

   </s:form>

   <s:actionerror/>

 </body>

</html>

8.在我们已经建好的struts.xml中来配置登录的action。这里定义登录action的名字为login,配置代码如下:

<?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>

    <package name="struts2" extends="struts-default" namespace="/">

       <action name="login" class="com.test.actions.LoginAction">

           <result name="success" type="redirect">/index.jsp</result>

           <result name="input">/login.jsp</result>

           <result name="error">/login.jsp</result>

       </action>

    </package>

</struts>

9.下面就来编写具体的action类了。代码如下:

package com.test.actions;

import com.opensymphony.xwork2.ActionSupport;

publicclass LoginAction extends ActionSupport {

    public String username;

    public String password;

   

    public String excute(){

       if(!username.equals("admin")){

           super.addFieldError("username", "用户名错误!");

           returnERROR;

       }

       if(!password.endsWith("sa")){

           super.addFieldError("password","密码错误!");

           returnERROR;

       }

       returnSUCCESS;

    }

    //验证

    publicvoid validate(){

       if(username==null || username.length()==0){

           super.addActionError("请输入用户名!");

           }

       if(password==null || password.length()==0){

           super.addActionError("请输入密码!");

       }

    }

}

10.测试,看效果(成功)

配置Hibernate

11.导入hibernate包:

       hibernate3.jar-----------------------------核心类库
        
antlr-2.7.6.jar-----------------------------代码扫描器,用来翻译HQL语句
        
commons-collections-3.1.jar----------- Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大
        
dom4j-1.6.1.jar----------------------------是一个JavaXML API,类似于jdom,用来读写XML文件的
        
javassist-3.4.GA.jar----------------------- Javassist 字节码解释器
        
jta-1.1.jar------------------------------------标准的JTA API
      slf4j-api-1.5.2.jar
      slf4j-nop-1.5.2.jar

12.创建Hibernate配置文件。在WEB-INF"calsses目录下(工程的src包下)新建hibernate.cfg.xml。这是hibernate连接数据库的配置文件:

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

    <session-factory>

       <property name="connection.username">sa</property>

       <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=demo</property>

       <property name="dialect">

           org.hibernate.dialect.SQLServerDialect

       </property>

       <property name="myeclipse.connection.profile">s2shcon2</property>

       <property name="connection.password">sa</property>

       <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

       <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

       <property name="show_sql">true</property>

       <mapping resource="com/test/entity/Users.hbm.xml" />

    </session-factory>

</hibernate-configuration>

配置Spring2.5

14.导入包:

spring.jar,struts2-spring-plugin-2.1.8.1.jar (spring struts 整合映射用) ,cglib-2.1.3.jar, asm.jar

15.配置web.xml文件。Jar包引入完成后,就开始配置spring了,首先修改web.xml文件,增加如下代码:

       <!-- 配置spring的监听器 -->

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath*:applicationContext-*.xml</param-value>

    </context-param>

16.src下面新建applicationContext-common.xml文件。首先给这个文件加上spring的标头

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>

17.2个类测试spring

      package test.spring;

publicclass TUser implements java.io.Serializable {

    private String username;

    private String allname;

    private String address;

    public String getUsername() {

       returnthis.username;

    }

    publicvoid setUsername(String username) {

       this.username = username;

    }

    public String getAllname() {

       returnthis.allname;

    }

    publicvoid setAllname(String allname) {

       this.allname = allname;

    }

    public String getAddress() {

       returnthis.address;

    }

    publicvoid setAddress(String address) {

       this.address = address;

    }

}

package test.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

publicclass SpringTest {

    publicstaticvoid main(String[] args) {

       // 加载spring配置文件,初始化IoC容器

       ApplicationContext ac = new ClassPathXmlApplicationContext(

              "applicationContext-common.xml");

       // 从容器接管Bean

       TUser user = (TUser) ac.getBean("TUser");

       // 输出欢迎信息

       System.out.println("Hello:" + user.getUsername() + ";u is in "

              + user.getAddress() + " ; and u is " + user.getAllname());

    }

}

18.创建完毕后,就剩最后一步了,在applicationContext.xml中配置一个bean,在xml中增加如下代码:

    <bean id="TUser" class="test.spring.TUser">

       <property name="username" value="小张"></property>

       <property name="allname" value="张三"></property>

       <property name="address" value="青岛市"></property>

    </bean>

19.测试结果:

整合Struts
20.web.xml中添加:

    <!-- 配置spring的监听器 -->

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath*:applicationContext-*.xml</param-value>

    </context-param>

21.现在就来看如何把strutsaction交给spring。以struts示例中的login.action为例,首先创建一个LoginAction类的Bean。在applicationContext-common.xml中增加如下代码:

    <bean id="loginAction" class="com.test.actions.LoginAction"

       scope="prototype">

    </bean>

22.接下来修改struts.xml文件,把原来login.action的配置做如下修改:

原来:<action name="login" class="com.test.actions.LoginAction" >

修改为:<action name="login" class="loginAction">

class值设为了loginAction,即LoginAction类的beanID。这样我们就把LoginAction类交给了spring管理。至于具体是怎么处理的,秘密在struts2-spring-plugin-2.1.6.jar中,

整合Hibernate

22.配置sessionFactoryspring来创建Session。在applicationContext-common.xml中增加如下代码:

    <!-- 配置SessionFactory -->

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="configLocation"

           value="classpath:hibernate.cfg.xml">

       </property>

    </bean>

    <!-- 配置事务管理器 -->

    <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory" ref="sessionFactory" />

    </bean>

    <!-- 配置事务管理 -->

    <bean id="transactionBase"

    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

       lazy-init="true" abstract="true">

       <!-- 配置事务管理器 -->

       <property name="transactionManager" ref="transactionManager" />

       <!-- 配置事务属性 -->

       <property name="transactionAttributes">

           <props>

              <prop key="*">PROPAGATION_REQUIRED</prop>

           </props>

       </property>

    </bean>

23.web.xml添加过滤

<!-- hibernate过滤器 -->

    <filter>

       <filter-name>hibernateFilter</filter-name>

       <filter-class>

           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>hibernateFilter</filter-name>

       <url-pattern>*.action</url-pattern>

    </filter-mapping>

将数据访问层,逻辑层,action层的配置分开,applicationContext-common.xml只放公共配置

最后配置文件:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="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">

   

    <!-- 配置spring的监听器 -->

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath*:applicationContext-*.xml</param-value>

    </context-param>

    <!-- hibernate过滤器 -->

    <filter>

       <filter-name>hibernateFilter</filter-name>

       <filter-class>

           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>hibernateFilter</filter-name>

       <url-pattern>*.action</url-pattern>

    </filter-mapping>

   

    <!-- struts2过滤器 -->

    <display-name>Struts 2 Fileupload</display-name>

    <filter >

        <filter-name>struts-cleanup</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.ActionContextCleanUp

        </filter-class>

    </filter >

   

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

       </filter-class>

    </filter>

   

    <filter-mapping >

        <filter-name>struts-cleanup</filter-name>

        <url-pattern >/*</url-pattern >

    </filter-mapping >

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    <!-- 编码过滤器 -->

    <filter>

       <filter-name>encodingFilter</filter-name>

       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

       <init-param>

           <param-name>encoding</param-name>

           <param-value>utf-8</param-value>

       </init-param>

    </filter>

    <filter-mapping>

       <filter-name>encodingFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

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>

    <package name="struts2" extends="struts-default" namespace="/">

       <action name="login" class="loginAction">

           <result name="success">/main.jsp</result>

           <result name="input">/login.jsp</result>

           <result name="error">/login.jsp</result>

       </action>

      

       <action name="comReport" class="comReportAction">

           <result name="success">/index.jsp</result>

           <result name="input">/investreport.jsp</result>

       </action>

      

        <action name ="fileUpload" class ="com.gt.test.FileUploadAction" >

            <interceptor-ref name ="fileUploadStack" />

            <result name ="success" >/test/FileUpload.jsp </result >

        </action >

    </package>

</struts>   

Hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

    <session-factory>

       <property name="connection.username">IT03</property>

       <property name="connection.url">

           jdbc:sqlserver://localhost;databaseName=demo

       </property>

       <property name="dialect">

           org.hibernate.dialect.SQLServerDialect

       </property>

       <property name="myeclipse.connection.profile">s2shcon</property>

       <property name="connection.password">sa</property>

       <property name="connection.driver_class">

           com.microsoft.sqlserver.jdbc.SQLServerDriver

       </property>

       <property name="dialect">

           org.hibernate.dialect.SQLServerDialect

       </property>

       <property name="show_sql">true</property>

    </session-factory>

</hibernate-configuration>

posted on 2010-04-19 14:41 淡淡的回忆 阅读(966) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年4月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜