web项目中加入struts2、spring的支持,并整合两者

Web项目中加入struts2 的支持

  1. 在lib下加入strut2的jar包

                       

    2. 在web.xml中添加配置

<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>*.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下面

 

原文地址:https://www.cnblogs.com/wanggd/p/3216731.html