`
b_l_east
  • 浏览: 636860 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring2.5 + Struts2 + Hibernate3 整合 [1/2]

阅读更多

Spring2.5 + Struts2 + Hibernate3 整合

Struts2,Spring2.5,Hibernate3整合

1.     /WEB-INF/lib目录下放入已下jar包:

 

commons-logging-1.0.4.jar

freemarker-2.3.15.jar

struts2-core-2.1.8.1.jar

xwork-core-2.1.6.jar

commons-fileupload-1.2.1.jar

struts2-convention-plugin-2.1.8.1.jar

struts2-spring-plugin-2.1.8.1.jar

spring.jar

commons-dbcp.jar

commons-pool.jar

hibernate3.jar

dom4j-1.6.1.jar

slf4j-log4j12-1.5.0.jar

slf4j-api-1.5.0.jar

log4j-1.2.15.jar

2.     配置/WEB-INF/web.xml文件:

 

  <!-- Struts2配置 -->

  <filter>

    <filter-name>struts2</filter-name>     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    <init-param>

        <param-name>config</param-name>

        <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>

    </init-param>

  </filter>

  <filter-mapping>

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

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

  </filter-mapping>

  <!-- Spring2.5配置 -->

  <context-param>

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

    <param-value>classpath:spring.xml</param-value>

  </context-param>

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

  </listener>

*至此,Struts2Spring2.5已经整合完毕,但要看到效果和使用整合框架,需要继续以下步骤。

3.     创建Struts2Spring2.5的配置文件

1.     /WEB-INF/classes目录下建struts.xml文件(web.xml中配置的文件名相同)

web.xml中的配置项为:

 

    <init-param>

        <param-name>config</param-name>

        <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>

    </init-param>

文件内容如下:

 

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

    <!-- 注意namespace"/"表应用根目录,extends"struts-default"不要写成"struts-defaults" -->

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

       <!-- action正常配置,只是将class配置成spring配置文件中的beanid -->

       <action name="example" class="spring.example">

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

        </action>

    </package>

</struts>

2.     /WEB-INF/classes目录下建spring.xml文件(web.xml中配置的文件名相同)

web.xml中的配置项为:

 

  <context-param>

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

    <param-value>classpath:spring.xml</param-value>

  </context-param>

文件内容如下:

 

<?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:context="http://www.springframework.org/schema/context"

        xmlns:aop=http://www.springframework.org/schema/aop

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

        xsi:schemaLocation="http://www.springframework.org/schema/beans

                       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

                      http://www.springframework.org/schema/context

                       http://www.springframework.org/schema/context/spring-context-2.5.xsd

                       http://www.springframework.org/schema/aop

                       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

                       http://www.springframework.org/schema/tx

                       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 以下id值与struts配置文件中的class值对应 -->

    <bean id="spring.example" class="com.erison.action.Example"/>

</beans>

4.     创建类及测试

建如下类com.erison.action.Example

 

package com.erison.action;

 

public class Example {

    public String execute() throws Exception {

       return "success";

    }

}

应用根目录下建如下文件index.jspbogy部分如下:

 

  <body>

    <h1>struts spring integraded! </h1>

  </body>

部署应用到tomcat5.5或其它容器。

打开浏览器,在地址栏输入:http://localhost:8080/ssh/example.action,结果如下:

 

*至此,struts2spring2.5的整合使用已经完成,以下整合Hibernate3

下一篇 Spring2.5 + Struts2 + Hibernate3 整合 [2/2]

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics