Elipcse版本:Luna Service Release 1 (4.4.1),下载地址:https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR1/eclipse-jee-luna-SR1-win32.zip
Apache版本:apache-tomcat-6.0.37
Struts2:本次使用版本2.3.16.3.GA,下载地址http://struts.apache.org/download.cgi#struts23163

如上图,选择下载struts-2.3.16.3-all.zip并解压
Eclipse和Tomcat的安装不再细说了,注意看一下Struts2项目的配置:
1、创建动态web项目
在Eclipse中选择File-New-Dynamic Web Project创建动态项目:

按下图创建名称为Struts2HelooWorld的项目,在Target runtime中选择Apache Tomcat v6.0并按提示选择Tomcat的安装根目录,单击“Finish”完成项目创建

2、添加Struts2 库到项目
在解压的struts-2.3.16.3-all目录下的lib目录中复制需要的库,并在Struts2HelloWorld项目的WebContent/WEB-INF/lib上右键粘贴:

需要导入的库如下,每个库的说明请参加struts2的文档:

3、配置过滤器
在Struts2HelloWorld项目的WebContent/WEB-INF/web.xml中添加如下配置:

如下:
- <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>
4、创建action
按下图创建一个包:


在该包中增加一个HelloWorld类,实现xwork2的Action接口:


按如下内容编辑该类:
- package com.mystruts.action;
-
- import com.opensymphony.xwork2.Action;
-
- /**
- * @author david
- *
- */
- public class HelloWorld implements Action {
-
- private String message;
-
-
- /**
- * @return the message
- */
- public String getMessage() {
- return message;
- }
-
-
- /* (non-Javadoc)
- * @see com.opensymphony.xwork2.Action#execute()
- */
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- message = "Hello World!";
- return SUCCESS;
- }
-
- }
5、添加jsp页面
在添加jsp文件之前,首先我们修改一下eclipse的默认项目编码,否则中文会是乱码,修改方法如下:
选择eclipse的主菜单中的window-Preferences

JSP Files的编码选择UTF-8

然后添加HellowWorld.jsp文件:


按下文编辑HelloWorld.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">
- <%@ taglib prefix="s" uri="/struts-tags" %>
-
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>欢迎界面</title>
- </head>
- <body>
- <h2><s:property value="message"/></h2>
- </body>
- </html>
6、添加struts.xml配置文件:

文件内容如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
- <package name="HelloWorld" extends="struts-default">
- <action name="HelloWorld" class="com.mystruts.action.HelloWorld">
- <result name="success">/HelloWorld.jsp</result>
- </action>
- </package>
- </struts>
7、调试



在浏览器中输入:http://localhost:8080/Struts2HelloWorld/HelloWorld
结果如下:
