web項目中加入struts二、spring的支持,並整合二者

Web項目中加入struts2 的支持web

  1. 在lib下加入strut2的jar包

                       

    2. 在web.xml中添加配置spring

<filter>apache

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

       <filter-class>app

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterjsp

       </filter-class>url

    </filter>spa

    <filter-mapping>插件

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

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

</filter-mapping>

  3. 在src下添加 struts2的配置文件

        

 Web項目中添加spring的支持

  1. 在lib下加入spring.jar

      

     2. 在web.xml中添加spring的配置信息

<context-param>

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

       <param-value>

           /WEB-INF/classes/applicationContext.xml

       </param-value>

    </context-param>

     <listener>

       <listener-class>

           org.springframework.web.context.ContextLoaderListener

       </listener-class>

</listener>  

    3. 在src下加入spring的配置文件

       

 在web項目中整合struts2和spring(我的認爲是代碼最優的一種方式)

l  Action由struts2建立

<action name="showname"

class="net.wanggd.mobile_scm.test.action.TestAction">

l  struts2中用到的spring中的bean有spring自動注入

1. struts的配置文件default.properties文件由以下配置struts.objectFactory.spring.autoWire = name

2. spring的配置文件中有

<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

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  default-autowire="byName"

 整合步驟:

  1. 在lib下加入插件

        

     2. Struts.xml中配置的內容的寫法和沒有引入spring以前的寫法同樣,不用變

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

    <constant name="struts.action.extension" value="do" />

    <constant name="struts.devMode" value="false" />

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

       <action name="welcome">

           <result>/WEB-INF/jsp/MyJsp.jsp</result>

       </action>

       <action name="showname"

           class="net.wanggd.mobile_scm.test.action.TestAction">

           <result name="ret">/WEB-INF/jsp/index2.jsp</result>

       </action>

    </package>

</struts>

  3. 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"

    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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  default-autowire="byName" >

    <bean id="testService"

       class="net.wanggd.mobile_scm.test.service.TestServiceImpl" >

    </bean>

</beans>

Web項目中配置文件在src下面

                       

 當部署到tomcat下後,src下面的東西會自動出如今classpath下面

相關文章
相關標籤/搜索