1、创建maven工程,引入jar依赖
pom.xml文件如下,maven相关知识请参看maven学习。
属性:设置版本等信息
依赖管理:锁定依赖jar包的具体版本
依赖:
- spring:
- struts2
- mybatis
- mysql驱动
- c3p0
- slf4j
- junit、jstl
- servlet jsp
1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
2、配置web.xml
配置spring监听器
1
2
3
4
5
6
7
8<!-- applicationContext对象仅加载一次,在服务器器启动时加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/applicationContext*.xml</param-value>
</context-param>配置struts2核心过滤器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<!-- Struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 指定action所在包路径 -->
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.topvision</param-value>
</init-param>
<!-- 指定struts.xml文件路径,默认classpath下 -->
<init-param>
<param-name>filterConfig</param-name>
<param-value>classpath:struts.xml</param-value>
</init-param>
</filter>
<!-- 设置url过滤 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>设置编码,防止乱码
1
2
3
4
5
6
7
8
9
10
11
12
13<!-- 编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>设置首页
1
2
3
4
5
6
7
8
9<!-- 首页设置 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
3、applicationContext.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |
4、Action编写
1 |
|
5、配置struts.xml
struts.xml文件路径要与struts2核心过滤器中配置的路径一致,默认在classpath下即可。
1 | <?xml version="1.0" encoding="UTF-8" ?> |
6、测试
首页设置
1
2
3
4
5
6
7
8
9
10
11<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="aaa/loginTest.tv?username=中文&password=123">请点击这个链接</a><br>
</body>
</html>login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功</title>
</head>
<body>
<h2>跳转成功</h2>
<s:property value="@java.lang.Integer@MAX_VALUE"/>
<s:property value="username"/>
<s:property value="password"/>
<s:debug></s:debug>
</body>
</html>