开发环境:
JDK: JDK6
IDE: MyEclipse6.0
Tomcat: MyEclipse Tomcat
一、下载Struts2
struts2.2 http://struts.apache.org/download.cgi#struts2211
xwork2.1 http://www.opensymphony.com/xwork/download.action
二、第一个Struts2项目HelloWorld
项目效果:在地址栏运行/helloworld,页面上显示Hello World!
1.新建web工程,取名Struts2_01_HelloWorld
2.导入Struts2相关jar包
将struts-2.2.1.1-all.zip/lib下的7个jar(见下表)拷贝到%project_home%/WebRoot/WEB-INF/lib下
- commons-fileupload-1.2.1.jar
- commons-io-1.3.2.jar
- freemarker-2.3.16.jar
- javassist-3.7.ga.jar
- ognl-3.0.jar
- struts2-core-2.2.1.1.jar
- xwork-core-2.2.1.1.jar
3.在%project_home%/src下创建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>
</struts>
4.在%project_home%/WebRoot/WEB-INF/web.xml中配置核心过滤器StrutsPrepareAndExecuteFilter
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
5.关联源代码和API文档
关联Struts2源代码
在 %project_home%/Referenced Libraries/struts2-core-2.2.1.1.jar 点击右键选择 Properties->Java Source Attachment ,点击 External Folder ... 按钮,弹出文件对话框.选择struts-2.2.1.1/src/core/src/main/java存放的路径,再点击Apply按钮完成操作。
关联Struts2的API文档
在 %project_home%/Referenced Libraries/struts2-core-2.2.1.1.jar 点击右键选择 Properties->Javadoc Location,点击Browse... 按钮,弹出文件对话框.选择struts-2.2.1.1/docs存放的路径,再点击Apply按钮完成操作。
关联xwork源代码
在 %project_home%/Referenced Libraries/xwork-core-2.2.1.1.jar点击右键选择 Properties->Java Source Attachment ,点击 External Folder ... 按钮,弹出文件对话框.选择xwork-2.1.5/core/src/main/java存放的路径,再点击Apply按钮完成操作。
关联xwork的API文档。
在 %project_home%/Referenced Libraries/xwork-core-2.2.1.1.jar点击右键选择 Properties->Javadoc Location,点击Browse... 按钮,弹出文件对话框.选择xwork-2.1.5/docs/xwork-apidocs/存放的路径,再点击Apply按钮完成操作。
注意:在光标定位到源代码的类名或属性名或方法名上,再按F1可查看API文档
6.让struts.xml出提示
选择Myeclipse菜单栏上的Preferences -> MyEclipse -> Files and Editors -> XML - >XML Catalog,
在右边面板中点击 add...按钮,弹出And Xml Catalog Entry对话框
Location(dtd文件存放物理路径) : D:\struts2\struts-2.2.1.1\src\core\src\main\resources\struts-2.1.dtd
Key Type: URI
Key : http://struts.apache.org/dtds/struts-2.1.dtd
填写完后,点击OK。
7.编写Action
在%project_home%/src下创建包 com.puckasoft.web.action
在包下创建类 HelloWorldAction
写法一 : 普通类
package com.puckasoft.web.action;
public class HelloWorldAction {
public String execute() throws Exception {
return "success";
}
}
写法二 : 实现Action接口
package com.puckasoft.web.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action{
public String execute() throws Exception {
return "success";
}
}
写法三 : 继承ActionSupport
package com.puckasoft.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
public String execute() throws Exception {
return "success";
}
}
Struts2的Action可以是一个普通类,或实现Action接口,或继承ActionSupport类,通常在类中定义execute方法来处理请求。方法的返回值决定了跳转的视图信息, 通过返回的字符串在struts.xml中找到对应的配置项,就知道要跳转的页面路径了。每请求一次Action,会创建一个对象,这样可以避免线程同步问题。
8.编写页面
在%project_home%/WebRoot下创建helloworld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
Hello world!
</body>
</html>
9.在struts.xml中配置action
<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="do,action" />
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="com.puckasoft.web.action.HelloWorldAction" method="execute">
<result name="success" type="dispatcher">/helloworld.jsp</result>
</action>
</package>
</struts>
配置文件解析:
struts.xml主要配置常量,包,bean,还可以引用其他配置文件。这里重点讲解常量和包的配置
a.常量配置:
Struts2常量可以在web.xml,struts.xml,struts.properties等多处配置
- 在web.xml中的StrutsPrepareAndExecuteFilter中配置
<init-param> <param-name>参数名</param-name> <param-value>参数值</param-value> </init-param>
- 在struts.xml中配置
<constant name="参数名" value="参数值">
- 在struts.properties配置
参数名=参数值
不推荐在web.xml中配置,比较繁琐,可读性差
struts.xml和struts.properties选择一种即可
加载顺序如下:
1.struts2-core-2.2.1.1.jar/struts-default.xml
2.struts2-core-2.2.1.1.jar/org/apache/struts2/default.properties
3.%class_path%/struts.xml
4.%class_path%/struts.properties
5.web.xml
例如:配置开发模式,struts.devMode的默认值是false,改成true时可以热启动web服务器
<constant name="struts.devMode" value="true" />
Action的访问路径是由Action的名字,所在包的命名空间,和常量struts.action.extension共同决定的
struts.action.extension的默认值是action, ,
也就是说要访问配置文件中的Action 可以运行/helloworld 和 /helloworld.action
指定请求Action的后缀为do,action后,就只能通过/helloworld.do 和helloworld.action 访问Action了
<constant name="struts.action.extension" value="do,action" />
b.包的配置:
Struts2使用包管理一组业务功能相关的Action, 包中定义Struts2核心组件Action,拦截器和拦截器栈。
配置格式 <package name="" namespace="" extends="" abstract=""/>
name属性(必选) : 包的名字,最好根据业务功能模块命名,如果其他包要继承该包,必须通过该属性进行引用
namespace属性(可选,默认为空字符串) : 定义该包的命名空间,命名空间作为访问该包下的Action的路径的一部分
abstract属性(可选) : 定义该包是否抽象,抽象时不能包含action。
extends属性(可选) : 定义父包,可继承父包的Action,拦截器和拦截器栈。通常继承struts-default包, 因为它 定义了一些拦截器和跳转类型。这些拦截器实现了 诸如将请求中把请求参数封装到action、文件上传和数据验证等等核心功能。
struts-default包定义在struts2-core-2.2.1.1.jar/struts-default.xml中,struts-default.xml是Struts2 默认配置文件,Struts2每次都会自动加载。
<package name="struts-default" abstract="true">
<result-types>
<result-type name="chain"
class="com.opensymphony.xwork2.ActionChainResult"/>
...
<result-type name="plainText"
class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
<interceptors>
<interceptor name="alias"
class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
...
<interceptor name="multiselect"
class="org.apache.struts2.interceptor.MultiselectInterceptor" />
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
...
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack"/>
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</package>
c.Action配置
配置格式 <action name="" class="" method=""/>
name属性(必填):action的名字
class属性(选填,默认值为ActionSupport类):action的类名
method属性(选填,默认值为execute):action的方法名
ActionSupport的execute方法是这样定义的
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
public String execute() throws Exception {
return SUCCESS;
}
d.Result配置
配置格式<result name="" type=""> </result>
name属性(选填,默认值为success):result名字,和Action的execute方法的返回对应
type属性(选填,默认值为dispatcher):result类型,dispatcher为服务器内部跳转
以上配置也可以简化写成,当Action仅仅只实现跳转功能时,就可以这样配置。
<action name="helloworld">
<result>/helloworld.jsp</result>
</action>
10.部署测试运行
三、Struts2处理流程
用户在浏览器访问某地址时,浏览器将其请求发送给Web服务器,Web服务器判断应交给哪个Wep应用程序来处理,并读取Web应用程序中的web.xml,web.xml中配置struts2的核心过滤器接收所有的访问请求,请求就发生给过滤器的doFilter方法,该方法在struts2的配置文件中找到对应的包,如果包中定义了拦截器,则先通过拦截器,然后到达Action,Action处理完后,会返回Result,过滤器将Result对应的视图页面返回给客户端浏览器。
本文介绍如何使用Struts2框架搭建简单的HelloWorld项目,包括环境搭建、项目配置、页面编写及部署测试等步骤。

305

被折叠的 条评论
为什么被折叠?



