简介
这篇文章主要讲如何在eclipse中使用struts2,文章使用的struts2的版本是2.5.14.1,
会与其他的版本有一小点的差别,文章里已经说明。例子的完整源码在文末,亲测没有任何错误。
struts2下载
官网下载地址 USTC镜像站下载地址
最新版是2.5.14.1,这个版本的一些jar包与旧版本不太一样,不过变化不大。
具体的变化建议阅读2.5版本的 version-notes
图1 struts2下载页面
这里选择完整的包(Full Distribution)下载。
下载解压后的文件结构如下图:
图2 struts2文件结构图
apps中是使用struts2的一些例子,docs是文档,包括帮助文档和api文档,lib是jar包,src中是源码。
一个简单的例子
eclipe
环境
需要的环境
- tomcat 我使用的是:tomcat-8.0.42
- jdk > 7 我使用的是:jdk-1.8.0_131<
- eclipse 我使用的是:eclipse neon (4.6.2)
首先保证tomcat能够正常运行,关于tomcat的配置这里就不讲了。
详细的步骤
-
新建一个WEB项目,名为HelloWorld
在eclipse中建一个普通的WEB项目。保证项目能够运行。 -
添加相关的jar包
把需要的jar包从struts2的lib目录复制到WEB-INF/lib文件夹下,最基础的需要8个jar包:
commons-fileupload-1.3.3.jar、commons-io-2.5.jar、commons-lang3-3.6.jar、freemarker-2.3.26.jar、
log4j-api-2.9.1.jar、ognl-3.1.15.jar、struts2-core-2.5.14.1.jar、javassist-3.20.0-GA.jar注意:struts2.5之前的版本有点不同,还需要xwork-core.jar,不需要log4j-api-2.7.jar。原因是struts2.5把xwork的源码
合并到了struts-core中。struts2.5之前使用logging API,而struts2.5用log4j 2 API取代。
如果你听从了我的建议,阅读了struts2.5的 version-notes ,你就能知道详细的原因。图3 需要的jar包
-
在web.xml中配置struts2框架的核心控制器StrutsPrepareAndExexuteFilter
图4 web.xml的配置
Filter的完整类名struts2.5以前是 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
更多改变请阅读 struts2.5 version-notes -
在src目录下新建一个业务控制Action类,继承自com.opensymphony.xwork2.ActionSupport,内容如下:
图5 新建一个Action
-
Action需要在Struts2的核心配置文件中进行配置
Struts2的核心配置文件为struts.xml,放在src目录下。图6 struts.xml内容
注意struts.xml的放置位置。
-
新建一个result.jsp文件,用来action显示返回的视图
图7 result.jsp的内容
-
最后运行HelloWorld项目,在浏览器访问http://localhost:8080/HelloWorld/helloworld
最后展现的内容应该是result.jsp的内容。控制台输出Action的打印内容
到这里,struts2就算配置完成了。
涉及的代码
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>HelloWorld</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心*** --> <filter> <!-- Filter的名字 --> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5以前可能有所不同 --> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <!-- 拦截所有的url --> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
-
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- name action的名字,访问时使用helloworld.action访问,class:实现类 --> <action name="helloworld" class="cn.xhcoding.action.HelloWorldAction"> <!-- 结果集,即action中SUCCESS返回的视图 --> <result> /result.jsp </result> </action> </package> </struts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
-
HelloWorldAction.java
package cn.xhcoding.action; public class HelloWorldAction extends com.opensymphony.xwork2.ActionSupport{ @Override public String execute() throws Exception { System.out.println("正在执行的Action"); // 返回视图 SUCCESS,这是框架定义 return SUCCESS; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- result.jsp
<%@ 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>Action Result</title>
</head>
<body>
<h1>恭喜成功配置好基本的struts2环境</h1>
<h2>Hello World, I am Successful</h2>
</body>
</html>
参考网址:
struts2官方文档:http://struts.apache.org/docs/
struts2.5版本改变:http://struts.apache.org/docs/version-notes-25.html