首页 > 代码库 > 配置struts2+spring,springmvc

配置struts2+spring,springmvc

  • Struts2+Spring整合

    一.spring负责注入,struts2负责它自己的工作。这样不是很符合spring作为ioc容器的全部功能,不推荐。

    二.spring负责全部bean和struts2的action的生成。作为ioc容易的最大共用。

    所需要jar包

    技术分享

    技术分享

    技术分享

    技术分享

      配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Struts2AndSpring</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <!-- 初始化 -->      <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>    <!-- 配置spring监听器 -->        <listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>        </listener>    <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></web-app>

 

    关键点:配置struts2核心过滤器;配置spring工厂的监听器;把applicationContext.xml的路径指明。

    配置struts2.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>        <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <constant name="struts.i18n.encoding" value="UTF-8"/>    <!--整合spring-->    <constant name="struts.objectFactory" value="spring" />    <!-- Add packages here -->    <package name="default" namespace="/" extends="struts-default">        <!-- 测试action-->        <!-- class对应applicationContext.xml的id,在applicationContext.xml上配置真是路径 -->        <action name="Hello" class="Hello">            <result name="success"></result>        </action>    </package></struts>

    配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"    "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans>    <bean id="Hello" class="action.TestAction" autowire="byName" scope="prototype">    </bean> </beans>

    action例子

package action;import java.io.IOException;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 测试action * @author MacBook * */public class TestAction extends ActionSupport{    public String execute(){        System.out.println("执行了!!!");        try {            ServletActionContext.getResponse().getWriter().print("hello");        } catch (IOException e) {            e.printStackTrace();        }        return SUCCESS;    }}

    访问

    技术分享

    结果

    技术分享

 

  • SpringMVC配置  

    我是新手,网上很多解析这个框架的。这个就集成在spring下载下来的jar包里。

    技术分享

    技术分享

    技术分享

    技术分享

    web.xml

 

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>Struts2AndSpring</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list> <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->  <servlet>      <servlet-name>springmvc</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc-servlet.xml</param-value>        </init-param>        <!-- <load-on-startup>1</load-on-startup> -->  </servlet>  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping></web-app>

 

    springmvc-servlet.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"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                        <!-- scan the package and the sub package -->    <context:component-scan base-package="mvc"/>    <!-- don‘t handle the static resource -->    <mvc:default-servlet-handler />    <!-- if you use annotation you must configure following setting -->    <mvc:annotation-driven />        <!-- configure the InternalResourceViewResolver -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"             id="internalResourceViewResolver">        <!-- 前缀 -->        <property name="prefix" value="/WEB-INF/jsp/" />        <!-- 后缀 -->        <property name="suffix" value=".jsp" />    </bean></beans>

技术分享

package mvc;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/mvc")public class mvcController {    @RequestMapping("/hello")    public String hello(){        System.out.println("hello world");        return "hello";    }}

   注释声明了它是控制器,并且定义了请求映射。

   访问的时候使用/项目名/mvc/hello 访问此方法。

 

 

    

 

配置struts2+spring,springmvc