配置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的更多相关文章
- BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&hibernate
1.在lib中加入必要的包,导入后结果如下: lib打包下载:SSH-lib.jar (struts2.3.1.2 spring3.0.5 hibernate3.6.10.Final) 只包含必要 ...
- java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架
1.安装JDK: 安装之后要配置环境变量,在系统变量里: 新建变量名JAVA_HOME,变量值C:\Program Files\Java\jdk1.8.0_77: 新建变量名CLASSPATH,变量值 ...
- BAE 环境下配置 struts2 + spring + hibernate(SSH)(二)struts2
在myeclipse下开发的 应用但是 放到BAE下就出现了问题,虽然显示发布成功,但是访问的时候就会出现503 Service Unavailable 错误.通过调整 web.xml 发现纯Serv ...
- SSM三大框架整合配置(Spring+SpringMVC+MyBatis)
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- Maven pom配置(Spring+SpringMvc+mybaties)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- BAE 环境下配置 struts2 + spring + hibernate(SSH)(一)准备
1.首先选择版本控制 SVN 或者 Git ,但是由于Git在windows下需要环境,所以优先选择SVN. 2.安装一个SVN客户端 windows下使用TortoiseSVN:立即下载 注意:BA ...
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...
- 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...
- Struts2和SpringMVC简单配置以及区别总结
Struts2: struts 2 是一个基于MVC(mode-view-con)设计模式的Web应用框架,是由Struts1和WebWork两个经典框架发展而来的. 工作流程: 1客户端浏览器发出H ...
随机推荐
- .NET项目开发—浅谈面向对象的纵横向关系、多态入口,单元测试(项目小结)
阅读目录: 1.开篇介绍 2.使用委托消除函数串联调用 2.1.使用委托工厂转换两个独立层面的对象 3.多态入口(面向对象继承体系是可被扩展的) 4.多态的受保护方法的单元测试(Protected成员 ...
- [MySQL性能优化系列]LIMIT语句优化
1. 背景 假设有如下SQL语句: SELECT * FROM table1 LIMIT offset, rows 这是一条典型的LIMIT语句,常见的使用场景是,某些查询返回的内容特别多,而客户端处 ...
- Topshelf 创建windows服务注意事项
其中项目应该是控制台应用程序 test.exe需要赋与管理员权限,右键属性可以定义. test.exe install test.exe unstall
- 在Windows Server 2012 R2中搭建SQL Server 2012故障转移集群
需要说明的是我们搭建的SQL Server故障转移集群(SQL Server Failover Cluster)是可用性集群,而不是负载均衡集群,其目的是为了保证服务的连续性和可用性,而不是为了提高服 ...
- c#多态性
class Program { static void Main(string[] args) { //声明一个派生类 devierExample de = new devierExample(); ...
- Java中的集合排序
1. 定义排序 class ComparatorDefault implements Comparator { public int compare(Object arg0, Object arg1) ...
- Service 广播 到Fragment
//Fragment public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Sys ...
- 手动处理datanode磁盘间使用不均的问题
http://wiki.apache.org/hadoop/FAQ#On_an_individual_data_node.2C_how_do_you_balance_the_blocks_on_the ...
- zookeeper原理
Zookeeper与paxos算法:http://www.riaos.com/ria/11299 Paxos算法1:http://blog.csdn.net/chen77716/article/det ...
- cefsharp设置网页接受语言Accept-Language
1.设置浏览器的请求控制器 webView.RequestHandler = new RequestHandler(); 2.新建RequestHandler类继承IRequestHandler接口, ...