33.Spring整合Struts2.md
目录
1.搭建环境
在idea下可以在创建module时候选择,注意WEB-INF下的classes和lib两个目录需要手动创建,并且对应的配置文件和依赖的lib需要手动拷贝到这两个文件夹下
2.编写Action,Server,Dao类
package per.liyue.integration;
import org.springframework.stereotype.Repository;
@Repository(value = "userDao")
public class UserDao
{
public void Save()
{
System.out.println("操作数据保存了数据");
}
}
package per.liyue.integration;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service(value = "userService")//这里等于配置文件中<bean id="userDao" class="per.liyue.springlearing.structure.UserDao"></bean>
//的方式
public class UserService
{
@Resource(name = "userDao")
private UserDao userDao;
public void Save()
{
userDao.Save();
}
}
package per.liyue.integration;
import com.opensymphony.xwork2.ActionSupport;
import javax.annotation.Resource;
//@Controller(value = "userAction")
public class UserAction extends ActionSupport
{
@Resource(name = "userService")
private UserService userService;
@Override
public String execute()
{
userService.Save();
return SUCCESS;
}
}
3.配置bean.xml文件,建议按照类的不同来分类。例如将所有Action类的配置都写入到bean-Action.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"
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.xsd">
<context:component-scan base-package="per.liyue.integration"></context:component-scan>
</beans>
<?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"
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.xsd">
<context:component-scan base-package="per.liyue.integration"></context:component-scan>
</beans>
<?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"
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.xsd">
<context:component-scan base-package="per.liyue.integration"></context:component-scan>
</beans>
4.配置struts.xml
4.1修改jsp
<%--
Created by IntelliJ IDEA.
User: liyue
Date: 2016/11/15
Time: 10:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>First integration</title>
</head>
<body>
Hello !
</body>
</html>
4.2将Action类配置到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>
<package name="user" extends="struts-default">
<action name="user" class="per.liyue.integration.UserAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
5.配置Web.xml文件
struts2的配置如之前,spring的配置可以参考api文档(搜索关键字context-param),将完整的例子拷贝到我们的代码中。
如果spring配置文件的路径放到了classes下,需要手动拷贝
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--
1.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>/*</url-pattern>
</filter-mapping>
<!--
2.spring配置:具体配置内容可以在api文档中搜索关键字context-param查找到
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--多个xml文件用空格或者逗号隔开,可以用*来全局匹配-->
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
6.配置tomcat和输出
6.1配置tomcat
6.1.1在edit configuration中配置:

6.1.2部署配置

6.2工程配置
打开Pro
ject Structure配置
6.2.1Path
将输出路径修改为项目输出路径
6.2.2勾选依赖文件

7.运行
33.Spring整合Struts2.md的更多相关文章
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- Spring框架学习(5)spring整合struts2
内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...
- Spring整合Struts2的方法
一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...
- 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action
SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib 开发基本包 Struts2有一 ...
- spring整合struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...
- Spring 整合 Struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...
- Spring整合Struts2,Hibernate的xml方式
作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...
随机推荐
- int 和String之间的互转
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- 【HDOJ】1401 Solitaire
双向BFS+状态压缩. /* 1401 */ #include <iostream> #include <queue> #include <map> #includ ...
- 【算法Everyday】第三日 KMP算法
题目 你知道的. 分析 分析不来. 代码 void OutputArray(int* pArr, int iLen) { ; i < iLen; i++) { printf("%d\t ...
- JDK安装配置与升级
一.jdk1.4卸载 Redhat Enterprise 5 中自带安装了jdk1.4,在安装jdk1.6前,把jdk1.4卸载: 1. 首先查看系统自带的JDK版本: [root@linux ~]# ...
- HDU 5501 背包问题
需要按照B/C的值从大到小排序. #include<cstdio> #include<cstring> #include<iostream> #include< ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- codeforces 610B
Description Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n an ...
- 细说Lucene源码(一):索引文件锁机制
大家都知道,在多线程或多进程的环境中,对统一资源的访问需要特别小心,特别是在写资源时,如果不加锁,将会导致很多严重的后果,Lucene的索引也是如此,lucene对索引的读写分为IndexReader ...
- 改写URL的查询字符串QUERY_STRING(转)
查询字符串是指URL请求中“问号”后面的部分.比如,http://www.nowamagic.net/?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar. 1. 利用QSA转换查 ...
- 使用Tcl脚本分配FPGA管脚
自己主动生成Tcl文件 Project -> Generate Tcl File for Project... 弹出例如以下对话框.设置脚本路径. 编辑引脚 使用set_location_ass ...