1、引入struts2

<!-- struts2 和心包 排除javassist  因为hibernate也有 会发生冲突-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
 </dependency>
        <!-- struts2和spring整合包 这样会使action对每个请求new一个action对象 非单例
注意区别:springMVC注解是Cotroller 是单例的-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<!-- struts2注解支持包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>

2、引入struts配置文件

<?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> <!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 所有匹配*.action的请求都由struts2处理 可以value='action,do'-->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 生产环境false-->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 生产环境false-->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 生产环境true-->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 生产环境false -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 单位:Byte-->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 注意OGNL可能速度上比jstl慢一些 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" /> <package name="basePackage" extends="struts-default">
</package> </struts>

3、在web.xml加入sututs2的配置处理

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- Struts2配置 -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、添加Action

注解@Action  不需要spring 扫描   Action会被struts2-spring-plugin交由spring上下文中管理  因为struts2的Action中有很多类变量,所以是多例的

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "userAction")
public class UserAction
{
private static final Logger logger = Logger.getLogger(UserAction.class); private UserServiceI userService; public UserServiceI getUserService() {
return userService;
} /**
* spring注入
*/
@Autowired
public void setUserService(UserServiceI userService) {
this.userService = userService;
} public void test()
{
logger.info("Action test测试");
} /**
* 动态请求
*/
public void test2()
{
logger.info("Action test2测试");
} /**
* 通过上下文获取service
*/
public void test3()
{
logger.info("Action test3测试");
//获取上下文(spring的上下文)
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
//获取注入被扫描的service
UserServiceI userService1 = (UserServiceI)ac.getBean("userService");
userService1.test();
} /**
* 通过注入获取service
*/
public void test4()
{
logger.info("Action test4测试");
userService.test();
}
}

5、测试

先maven install  然后tomcat部署启动  这个顺序不能反了;

在浏览器输入:

http://localhost:8080/sshe/userAction!test.action

http://localhost:8080/sshe/userAction!test2.action

看控制台输出:

[sy.action.UserAction]Action test测试
[sy.action.UserAction]Action test2测试

上下文获取service

http://localhost:8080/sshe/userAction!test3.action

spring注入service

http://localhost:8080/sshe/userAction!test4.action

struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合的更多相关文章

  1. struts2+Hibernate4+spring3+EasyUI环境搭建之四:引入hibernate4以及spring3与hibernate4整合

    1.导入hibernate4 jar包:注意之前引入的struts2需要排除javassist  否则冲突 <!-- hibernate4 --> <dependency> & ...

  2. struts2+Hibernate4+spring3+EasyUI环境搭建之五:引入jquery easyui

    1.下载jquery easyui组件     http://www.jeasyui.com/download/index.php 2.解压 放到工程中  如图 3.jsp引入组件:必须按照如下顺序 ...

  3. struts2+Hibernate4+spring3+EasyUI环境搭建之一:准备工作

    SSHE环境搭建第一步:安装软件(经验:安装软件路径最好不要有空格.括弧.中文等特殊符号)1.Jdk72.tomcat73.maven34.MyEclipse10.7 破解及优化设置(设置本地安装jd ...

  4. struts2+Hibernate4+spring3+EasyUI环境搭建之二:搭建spring

    三.搭建spring3 1.引入spring3依赖 <!-- spring3 --> <dependency> <groupId>org.springframewo ...

  5. Struts2.5的的环境搭建及跑通流程

    Struts2.5 struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,st ...

  6. struts2 + spring3 + mybatis3 环境搭建

    struts2 + spring3 + mybatis3 1. 框架下载 struts2: http://struts.apache.org/ 下载 struts-2.3.14-all.zip spr ...

  7. 基于struts环境下的jquery easyui环境搭建

    下载地址: http://download.csdn.net/detail/cyberzhaohy/7348451 加入了json包:jackson-all-1.8.5.jar,项目结构例如以下: 測 ...

  8. struts2,hibernate4,spring3配置时问题汇总及解决办法

    文章转载于wanglihu的博客,原文链接http://wanglihu.iteye.com/blog/1897718 1.java.lang.NoClassDefFoundError: org/ob ...

  9. Struts2学习笔记(一)——环境搭建

    1.创建Web项目并导入Struts2的主要jar包 在MyEclipse中新建Web项目,然后在lib目录下添加必须的jar包: 2.创建jsp页面 1).创建test.jsp页面: <bod ...

随机推荐

  1. jQuery--隐藏事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. python判断文件目录是否存在

    import os os.path.isfile('test.txt')  # 如果不存在就返回False os.path.exists(directory)  # 如果目录不存在就返回False o ...

  3. Codeforces 672

    题目链接:http://codeforces.com/contest/672/problem A. Summer Camp(打表) 题意:123456789...一串字符串,问第n个是什么数字. 塞一 ...

  4. super.getClass()方法

    下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...

  5. Form验证(转)

    代码写 N 久了,总想写得别的.这不,上头说在整合两个项目,做成单一登录(Single Sign On),也有人称之为“单点登录”.查阅相关文档后,终于实现了,现在把它拿出来与大家一起分享.或许大家会 ...

  6. UVa 11774 (置换 找规律) Doom's Day

    我看大多数人的博客只说了一句:找规律得答案为(n + m) / gcd(n, m) 不过神题的题解还须神人写.. We can associate at each cell a base 3-numb ...

  7. HDU 5303 Delicious Apples 美味苹果 (DP)

    题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...

  8. None

    0 值的整型 / 浮点型.空字符串('').空列表([]). 空元组((,)).空字典({}).空集合(set())都等价于 False,但是不等于 None thing = None if thin ...

  9. apache开源项目--Synapse

    Apache Synapse一个易于使用.轻量级的XML与Web Services管理和集成中间件.可用于搭建SOA和ESB的基础平台.Apache Synapse支持多种标准包括:XML.XSLT. ...

  10. noip2003提高组题解

    这一年的前三题虽然难度不高,但是第二题极为繁琐,想在考场上用较短的时间拿到第二题的分数难上加难.所以必须要调整策略,争取拿其他三题的分数.第四题是比较普通的搜索题,分数比较好拿,但是很容易想成树形DP ...