33.Spring整合Struts2.md

时间:2021-06-19 11:32:26

目录

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中配置:

33.Spring整合Struts2.md

6.1.2部署配置

33.Spring整合Struts2.md

6.2工程配置

打开Pro33.Spring整合Struts2.md

ject Structure配置

6.2.1Path

将输出路径修改为项目输出路径

6.2.2勾选依赖文件

33.Spring整合Struts2.md

7.运行