Struts2 整合Spring(Maven,注解版)
这两天正在试验Struts2与Spring框架的整合,和他们各自的“注解”。今天就总结一下这两个框架怎么用注解进行整合。
一,加入两者的依赖包,除了两者的必要依赖外,还需要导入struts2-spring-plugin.jar来完成两者的整合。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.2.1</version>
</dependency>
二,web.xml的配置:在此需要用context-param标签添加Spring的配置文件的位置,并且添加Spring的contextLoaderListener监听,自动装配ApplicationContext(再次出就是bean.xml文件)的配置信息
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.toybrick.pro.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
三,beans.xml Spring配置文件,因为我们要用注解的形式来进行配置,所以这里我们就不要再spring配置文件里声明bean了。在这里需要通过component-scan标签
开启自动Spring的自动扫描功能(多个包之间用逗号隔开)
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="org.toybrick.pro.dao.impl,org.toybrick.pro.action"></context:component-scan>
</beans>
四,struts.xml ,需要要让spring来管理Struts2的对象
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="objectFactory" value="spring"/>
</struts>
五,登陆逻辑处理类,将所有的action都是用注解形式类声明。注意紫色部分我们将这个类声明为了一个springbean并规定了scope为prototype。注意红色部分,我们用注解形式注入了一个属性。@Autowired注解用来注入属性,但是默认的注入形式是按类型注入,@Quality注解用来将注入类型修改为按名称注入。
@Namespace("/main/user")
@ParentPackage(value="struts-default")
@Results({
@Result(name="success" , location="/main/desktop/desktop.jsp"),
@Result(name="fail" , location="/main/user/login.jsp")
})
@Service(value="userAction")
@Scope(value="prototype")
public class UserAction extends BaseAction {
private static final long serialVersionUID = 1L;
@Autowired
@Qualifier("userDao")
private UserDao userDao;
private User entity;
@Action("login")
public String login(){
String result="";
try {
userDao.login(entity);
result="success";
} catch (ServiceException e) {
System.out.println(e.getMessage());
this.setMessage(e.getMessage());
result="fail";
e.printStackTrace();
}
return result;
}
@Override
public void validate() {
if(entity == null){
entity = new User();
}
if(entity.getName() == null || "".equals(entity.getName())){
addFieldError("message", "用户名不可为空!");
}
if(entity.getPassw() == null || "".equals(entity.getPassw())){
addFieldError("message", "密码不可为空!");
}
}
public User getEntity() {
return entity;
}
public void setEntity(User entity) {
this.entity = entity;
}
}
六,数据访问对象(DAO)的实现类,我们将它声明为一个spring bean,用来注入到上面的UserAction中。
@Repository(value="userDao")
public class UserDaoImpl implements UserDao {
public void login(User user) throws ServiceException {
ServiceException e = new ServiceException();
if( ! user.getName().equals(user.getPassw())){
e.setMessage("用户名密码错误!");
throw e;
}
}
七,登陆页面(用了bootstrap框架)
<div class="panel panel-primary" style="margin:100px;">
<div class="panel-heading">登录</div>
<div class="panel-body">
<form action="<%=contextPath%>/main/user/login.action" method="post" class="col-md-4 col-md-offset-4" >
<div class="alert alert-warning" role="alert"><s:property value="message"/></div>
<div class="input-group margin">
<span class="input-group-addon" id="userNameAddon" >账号</span>
<input type="text" id="userName" name="entity.name" value="" class="form-control" required placeholder="用户名/邮箱/手机号" aria-describedby="userNameAddon"/>
</div>
<div class="input-group margin">
<span class="input-group-addon" id="passwordAddon" >密码</span>
<input type="password" id="password" name="entity.passw" value="" class="form-control" required placeholder="密码" aria-describedby="passwordAddon"/>
</div>
<button type="submit" class="btn btn-block btn-primary margin">登录</button>
</form>
</div>
</div>
到此,我们就完成了以注解形式整合Struts2和Spring两大框架。
Struts2 整合Spring(Maven,注解版)的更多相关文章
- Spring Boot整合MyBatis(非注解版)
Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...
- struts2整合spring的思路
struts2整合spring有有两种策略: >sping容器负责管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件. >利用spring的自动装配,Action将自动会从Sp ...
- struts2整合spring出现的Unable to instantiate Action异常
在struts2整合spring的时候,完全一步步按照官方文档上去做的,最后发现出现 Unable to instantiate Action,网上一搜发现很多人和我一样的问题,配置什么都没有错误,就 ...
- 集成框架 javaweb开发平台ssmy_m(生成代码) java struts2 mybatis spring maven jquery
网页地址 http://blog.csdn.net/lpy3654321/article/details/31841573 项目设想,在项目开发中,我们的开发者大多数时间都在反复开发 相同的keywo ...
- struts2整合spring应用实例
我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明stru ...
- Struts2—整合Spring
Struts2—整合Spring Spring框架是一个非常优秀的轻量级java EE容器,大部分javaEE应用,都会考虑使用Spring容器来管理应用中的组件. Struts2是一个MVC框架,是 ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- spring mvc注解版01
spring mvc是基于servlet实现的在spring mvc xml版中已经说过了,注解版相较于xml版更加简洁灵活. web项目的jar包: commons-logging-1.1.3.ja ...
随机推荐
- 浅谈系统架构<一>
前言:博主刚刚从事于Web后端开发与学习不久,开发项目经验也是有限的.不过今天依旧将一些个人的想法记录下来,我的构想或许不太正确,还望各位大牛能给我多多建议. 首先:我们从编程开始讲起 博主是偏向于后 ...
- 在.net中使用GAC
转自:http://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的dll的 ...
- mysql配置远程连接方法之一(改表法)
1.问题:如果在远程连接报错:1130-host ... is not allowed to connect to this MySql server,可能是你的帐号不允许从远程登陆,只能在local ...
- JavaScript常用技术总结!~~
//如果当前窗口不是最外层窗口,把最外层窗口链接改成当前窗口 if (window != top) top.location.href = location.href; //value值移入消失 $( ...
- 浅谈 jQuery 事件源码定位问题
该方法已过期,chrome 48还是49开始,自带各种流行框架的事件绑定解析. 勾上这个选项即可. 昨天群里有人问了个事件源码定位的问题,简单描述下是这样的. 在一个不是自己写的页面上,如何快速定位到 ...
- 《征服 C 指针》摘录4:函数 与 指针
一.指向函数的指针 函数名可以在表达式中被解读成“指向函数的指针”,因此,正如代码清单 2-2 的实验那样,写成 func 就可以取得指向函数的指针. “指向函数的指针”本质上也是指针(地址),所以可 ...
- 【重装系统】线上Linux服务器(2TB)分区参考方案
如果是线上服务器,假设它是 2TB 的 SATA 硬盘.8GB 内存,建议按如下方式进行分区: / 20480M(20G)(主分区) /boot 128M swap 10240M /data 2016 ...
- Redis学习 - 配置属性:protected-mode
根据redis的说明,protected-mode在同时存在如下两种情况时触发: 1) The server is not binding explicitly to a set of address ...
- FZU 2112 并查集、欧拉通路
原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
- python Function
Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...