• 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的更多相关文章

  1. BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&hibernate

    1.在lib中加入必要的包,导入后结果如下: lib打包下载:SSH-lib.jar  (struts2.3.1.2  spring3.0.5 hibernate3.6.10.Final) 只包含必要 ...

  2. java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架

    1.安装JDK: 安装之后要配置环境变量,在系统变量里: 新建变量名JAVA_HOME,变量值C:\Program Files\Java\jdk1.8.0_77: 新建变量名CLASSPATH,变量值 ...

  3. BAE 环境下配置 struts2 + spring + hibernate(SSH)(二)struts2

    在myeclipse下开发的 应用但是 放到BAE下就出现了问题,虽然显示发布成功,但是访问的时候就会出现503 Service Unavailable 错误.通过调整 web.xml 发现纯Serv ...

  4. SSM三大框架整合配置(Spring+SpringMVC+MyBatis)

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  5. Maven pom配置(Spring+SpringMvc+mybaties)

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  6. BAE 环境下配置 struts2 + spring + hibernate(SSH)(一)准备

    1.首先选择版本控制 SVN 或者 Git ,但是由于Git在windows下需要环境,所以优先选择SVN. 2.安装一个SVN客户端 windows下使用TortoiseSVN:立即下载 注意:BA ...

  7. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  8. 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...

  9. Struts2和SpringMVC简单配置以及区别总结

    Struts2: struts 2 是一个基于MVC(mode-view-con)设计模式的Web应用框架,是由Struts1和WebWork两个经典框架发展而来的. 工作流程: 1客户端浏览器发出H ...

随机推荐

  1. 聊下 git remote prune origin

    在你经常使用的命令当中有一个git branch –a 用来查看所有的分支,包括本地和远程的.但是时间长了你会发现有些分支在远程其实早就被删除了,但是在你本地依然可以看见这些被删除的分支. 你可以通过 ...

  2. WebApi Post 后台无法获取参数的解决方案

    事件回放: 之前一段时间,公司里前端用的Angularjs 发送http请求也是用的ng的组件,后台是.Net的WebApi 前端 var data = { PArgs: { PageIndex: 0 ...

  3. 从零自学Hadoop(05):Ambari

    阅读目录 序 引入背景 Ambari介绍 在线安装 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Sour ...

  4. 【hbase】——bulk load导入数据时value=\x00\x00\x00\x01问题解析

    一.存入数据类型 Hbase里面,rowkey是按照字典序进行排序.存储的value值,当用filter进行数据筛选的时候,所用的比较算法也是字典序的. 1.当存储的value值是float类型的时候 ...

  5. AES —— JAVA中对称加密和解密

    package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...

  6. ios合并静态库

    lipo -create SQY/iOS/iphoneos/libGamePlusAPI.a SQY/iOS/iphonesimulator/libGamePlusAPI.a -output SQY/ ...

  7. 如何读取Access里的OLE类型的图片

    身份证一类读卡器读取的照片信息,保存在Access数据库中一般为OLE型字段,图片为BMP格式,因为是用其读卡器写入的,其数据类型为常二进制数据. 再用报表或EXCEL读取这些图片时,如果将该图片字段 ...

  8. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  9. Something Wrong or Something Right

    其实,你还可以和高中一样 其实,你还可以和高中一样,每天不情愿的早早起床,走在冬天漆黑的清晨里.食堂还没有开门,你就去商店买面包和牛奶,接着快步走进教学楼,轻声咒骂一声老师要求的时间太早,然后打开一本 ...

  10. Django博客功能实现—文章评论的显示

    功能:在打开文章之后,能在文章下面是显示文章的评论,有父级评论.思路:在文章详情的视图里面,获取这个文章的全部评论,得到显示列表,然后用模板显示出来.步骤:一,在views.py的文章详情中获取评论: ...