[关闭]
@MrXiao 2017-12-20T16:53:46.000000Z 字数 10756 阅读 903

Struts2(2)- 搭建struts及spring整合Demo

Struts2


简单介绍struts2整合spring项目的搭建。

工程结构图

1、创建maven工程,引入jar依赖

pom.xml文件如下,maven相关知识请参看maven学习
属性:设置版本等信息
依赖管理:锁定依赖jar包的具体版本
依赖:

  1. spring:
  2. struts2
  3. mybatis
  4. mysql驱动
  5. c3p0
  6. slf4j
  7. junit、jstl
  8. servlet jsp
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.topvision</groupId>
  5. <artifactId>s2sm</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <name>s2sm Maven Webapp</name>
  8. <url>http://maven.apache.org</url>
  9. <!-- 属性 -->
  10. <properties>
  11. <spring.version>4.3.12.RELEASE</spring.version>
  12. <mybatis.version>3.4.5</mybatis.version>
  13. <struts.version>2.3.24.1</struts.version>
  14. </properties>
  15. <!-- 锁定版本 -->
  16. <dependencyManagement>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context</artifactId>
  21. <version>${spring.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-aspects</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-orm</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-test</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-web</artifactId>
  41. <version>${spring.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.apache.struts</groupId>
  45. <artifactId>struts2-core</artifactId>
  46. <version>${struts.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.struts</groupId>
  50. <artifactId>struts2-spring-plugin</artifactId>
  51. <version>${struts.version}</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.apache.struts</groupId>
  55. <artifactId>struts2-json-plugin</artifactId>
  56. <version>${struts.version}</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.mybatis</groupId>
  60. <artifactId>mybatis</artifactId>
  61. <version>${mybatis.version}</version>
  62. </dependency>
  63. </dependencies>
  64. </dependencyManagement>
  65. <dependencies>
  66. <!-- Spring -->
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-test</artifactId>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-context</artifactId>
  74. </dependency>
  75. <dependency>
  76. <groupId>org.springframework</groupId>
  77. <artifactId>spring-aspects</artifactId>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-orm</artifactId>
  82. </dependency>
  83. <dependency>
  84. <groupId>org.springframework</groupId>
  85. <artifactId>spring-web</artifactId>
  86. </dependency>
  87. <!-- Struts -->
  88. <dependency>
  89. <groupId>org.apache.struts</groupId>
  90. <artifactId>struts2-core</artifactId>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.apache.struts</groupId>
  94. <artifactId>struts2-spring-plugin</artifactId>
  95. </dependency>
  96. <dependency>
  97. <groupId>org.apache.struts</groupId>
  98. <artifactId>struts2-json-plugin</artifactId>
  99. </dependency>
  100. <!-- mybatis -->
  101. <dependency>
  102. <groupId>org.mybatis</groupId>
  103. <artifactId>mybatis</artifactId>
  104. </dependency>
  105. <dependency>
  106. <groupId>org.mybatis</groupId>
  107. <artifactId>mybatis-spring</artifactId>
  108. <version>1.3.1</version>
  109. </dependency>
  110. <!-- mysql驱动 -->
  111. <dependency>
  112. <groupId>mysql</groupId>
  113. <artifactId>mysql-connector-java</artifactId>
  114. <version>5.1.6</version>
  115. <scope>runtime</scope>
  116. </dependency>
  117. <!-- c3p0 -->
  118. <dependency>
  119. <groupId>c3p0</groupId>
  120. <artifactId>c3p0</artifactId>
  121. <version>0.9.1.2</version>
  122. </dependency>
  123. <!-- slf4j -->
  124. <dependency>
  125. <groupId>org.slf4j</groupId>
  126. <artifactId>slf4j-log4j12</artifactId>
  127. <version>1.7.2</version>
  128. </dependency>
  129. <!-- junit -->
  130. <dependency>
  131. <groupId>junit</groupId>
  132. <artifactId>junit</artifactId>
  133. <version>4.9</version>
  134. <scope>test</scope>
  135. </dependency>
  136. <!-- jstl -->
  137. <dependency>
  138. <groupId>javax.servlet</groupId>
  139. <artifactId>jstl</artifactId>
  140. <version>1.2</version>
  141. </dependency>
  142. <!-- servlet jsp -->
  143. <dependency>
  144. <groupId>javax.servlet</groupId>
  145. <artifactId>servlet-api</artifactId>
  146. <version>2.5</version>
  147. <scope>provided</scope>
  148. </dependency>
  149. <dependency>
  150. <groupId>javax.servlet</groupId>
  151. <artifactId>jsp-api</artifactId>
  152. <version>2.0</version>
  153. <scope>provided</scope>
  154. </dependency>
  155. </dependencies>
  156. <build>
  157. <plugins>
  158. <plugin>
  159. <groupId>org.apache.maven.plugins</groupId>
  160. <artifactId>maven-compiler-plugin</artifactId>
  161. <version>2.5</version>
  162. <configuration>
  163. <source>1.8</source>
  164. <target>1.8</target>
  165. <encoding>UTF-8</encoding>
  166. </configuration>
  167. </plugin>
  168. </plugins>
  169. <finalName>s2sm</finalName>
  170. </build>
  171. </project>

2、配置web.xml

  1. 配置spring监听器

    1. <!-- applicationContext对象仅加载一次,在服务器器启动时加载 -->
    2. <listener>
    3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    4. </listener>
    5. <context-param>
    6. <param-name>contextConfigLocation</param-name>
    7. <param-value>classpath:/config/applicationContext*.xml</param-value>
    8. </context-param>
  2. 配置struts2核心过滤器

    1. <!-- Struts2核心过滤器 -->
    2. <filter>
    3. <filter-name>struts2</filter-name>
    4. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    5. <!-- 指定action所在包路径 -->
    6. <init-param>
    7. <param-name>actionPackages</param-name>
    8. <param-value>com.topvision</param-value>
    9. </init-param>
    10. <!-- 指定struts.xml文件路径,默认classpath下 -->
    11. <init-param>
    12. <param-name>filterConfig</param-name>
    13. <param-value>classpath:struts.xml</param-value>
    14. </init-param>
    15. </filter>
    16. <!-- 设置url过滤 -->
    17. <filter-mapping>
    18. <filter-name>struts2</filter-name>
    19. <url-pattern>/*</url-pattern>
    20. </filter-mapping>
  3. 设置编码,防止乱码

    1. <!-- 编码 -->
    2. <filter>
    3. <filter-name>encodingFilter</filter-name>
    4. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    5. <init-param>
    6. <param-name>encoding</param-name>
    7. <param-value>UTF-8</param-value>
    8. </init-param>
    9. </filter>
    10. <filter-mapping>
    11. <filter-name>encodingFilter</filter-name>
    12. <url-pattern>/*</url-pattern>
    13. </filter-mapping>
  4. 设置首页

    1. <!-- 首页设置 -->
    2. <welcome-file-list>
    3. <welcome-file>index.html</welcome-file>
    4. <welcome-file>index.htm</welcome-file>
    5. <welcome-file>index.jsp</welcome-file>
    6. <welcome-file>default.html</welcome-file>
    7. <welcome-file>default.htm</welcome-file>
    8. <welcome-file>default.jsp</welcome-file>
    9. </welcome-file-list>

3、applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tool="http://www.springframework.org/schema/tool"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd
  15. http://www.springframework.org/schema/aop
  16. http://www.springframework.org/schema/aop/spring-aop.xsd
  17. http://www.springframework.org/schema/tool
  18. http://www.springframework.org/schema/tool/spring-tool-3.2.xsd
  19. ">
  20. <!-- 开启注解配置Bean -->
  21. <!-- 设置扫描包路径 -->
  22. <context:annotation-config/>
  23. <context:component-scan base-package="com.topvision"/>
  24. <!-- 开启注解事务 -->
  25. <tx:annotation-driven transaction-manager="transactionManager"/>
  26. <!-- 事务管理器 -->
  27. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  28. <property name="dataSource" ref="dataSource"></property>
  29. </bean>
  30. <!-- 数据库C3P0 -->
  31. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  32. <property name="driverClass" value="${s2sm.jdbc.driverClass}"/>
  33. <property name="jdbcUrl" value="${s2sm.jdbc.url}"/>
  34. <property name="user" value="${s2sm.jdbc.username}"/>
  35. <property name="password" value="${s2sm.jdbc.password}"/>
  36. <property name="initialPoolSize" value="${s2sm.cpool.initialPoolSize}" />
  37. <property name="minPoolSize" value="${s2sm.cpool.minPoolSize}" />
  38. <property name="maxPoolSize" value="${s2sm.cpool.maxPoolSize}" />
  39. <property name="acquireIncrement" value="${s2sm.cpool.acquireIncrement}" />
  40. <property name="maxIdleTime" value="${s2sm.cpool.maxIdleTime}"/>
  41. <property name="acquireRetryAttempts" value="${s2sm.cpool.acquireRetryAttempts}"/>
  42. <property name="acquireRetryDelay" value="${s2sm.cpool.acquireRetryDelay}"/>
  43. </bean>
  44. <!-- 加载properties -->
  45. <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
  46. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  47. <property name="locations">
  48. <list>
  49. <value>classpath:/config/*.properties</value>
  50. </list>
  51. </property>
  52. </bean>
  53. </beans>

4、Action编写

  1. @Controller
  2. @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  3. public class LoginAction extends BaseAction{
  4. private static final long serialVersionUID = -4442251270114214406L;
  5. private String username;
  6. private String password;
  7. public String loginTest() {
  8. String key = username + ":" + password;
  9. ActionContext context2 = ActionContext.getContext();
  10. context2.put("key", key);
  11. return SUCCESS;
  12. }
  13. public String getUsername() {
  14. return username;
  15. }
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. public String getPassword() {
  20. return password;
  21. }
  22. public void setPassword(String password) {
  23. this.password = password;
  24. }
  25. }

5、配置struts.xml

struts.xml文件路径要与struts2核心过滤器中配置的路径一致,默认在classpath下即可。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 所有匹配*.action的请求都由struts2处理 -->
  7. <constant name="struts.action.extension" value="tv,mb" />
  8. <!-- 主题 simple,xhtml,css_xhtml和ajax-->
  9. <constant name="struts.ui.theme" value="simple"/>
  10. <!-- 是否启用开发模式 -->
  11. <constant name="struts.devMode" value="true" />
  12. <!-- struts配置文件改动后,是否重新加载,有助于开发 -->
  13. <constant name="struts.configuration.xml.reload" value="true" />
  14. <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
  15. <constant name="struts.i18n.reload" value="true" />
  16. <!-- 设置浏览器是否缓存静态内容,开发是设为false,部署后true -->
  17. <constant name="struts.serve.static.browserCache" value="false" />
  18. <!-- Content Model : ((package | include | bean | constant)*, unknown-handler-stack?) -->
  19. <package name="login" namespace="/aaa" extends="struts-default">
  20. <action name="loginTest" class="com.topvision.s2sm.login.action.LoginAction" method="loginTest">
  21. <result>/WEB-INF/jsp/login/login.jsp</result>
  22. </action>
  23. </package>
  24. </struts>

6、测试

  1. 首页设置

    1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    3. <html>
    4. <head>
    5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    6. <title>Insert title here</title>
    7. </head>
    8. <body>
    9. <a href="aaa/loginTest.tv?username=中文&password=123">请点击这个链接</a><br>
    10. </body>
    11. </html>
  2. login.jsp

    1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    2. <%@ taglib prefix="s" uri="/struts-tags" %>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>成功</title>
    8. </head>
    9. <body>
    10. <h2>跳转成功</h2>
    11. <s:property value="@java.lang.Integer@MAX_VALUE"/>
    12. <s:property value="username"/>
    13. <s:property value="password"/>
    14. <s:debug></s:debug>
    15. </body>
    16. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注