Struts2的配置稍显繁琐,有时候一次成功,有时候想运行个Hello World都非很大劲,总是有这样那样的错误。避免这种问题出现的唯一办法就是,理解每个配置文件,理解为什么这样或那样做,记录疑惑,尤其要记录解决了疑惑的答案。我们唯一的目的就是:遇到某个曾经遇到的问题、一时又想不起来怎么解决的时候,一查笔记全部搞定。
Struts2 Hello World实例
使用:JDK 1.5.0_17 + eclipse-jee-europa-winter-win32 + Tomcat 5.5 + struts-2.0.14-ga
以下是Struts2 Hello World实例的文件内容。在eclipse中新建一个动态Web工程,命名为hello。
*** File: HelloWorld.java ***
// 包名,配置action实现类时,注意引用完整的包路径
package tutorial;
// 导入创建一个Struts2 action必须扩展的一个类
import com.opensymphony.xwork2.ActionSupport;
/**
* 创建一个action HelloWorld
* 创建一个Struts2 action必须扩展ActionSupport类
*/
public class HelloWorld extends ActionSupport {
// 版本序列号,ActionSupport实现了可序列化接口java.io.Serializable
private static final long serialVersionUID = 1L;
// 定义消息字符串常量
public static final String MESSAGE = "Struts is up and running ...";
// action被请求时执行的方法
public String execute() throws Exception {
setMessage(MESSAGE);
// 常量“SUCCESS”是在ActionSupport所实现的Action接口中定义的action的返回结果
return SUCCESS;
}
// 定义消息字符串变量
private String message;
// 设置消息内容
public void setMessage(String message){
this.message = message;
}
// 取得消息内容
public String getMessage() {
return message;
}
}
*** File End ***
*** File: HelloWorld.jsp ***
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- 第一行引用了Struts2的标签库 -->
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<!-- 使用Struts标签获得action的属性值,通过action属性的setters和getters -->
<h2><s:property value="message" /></h2>
</body>
</html>
*** File End ***
*** File: web.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="StrutsWeb" 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">
<!-- Struts2的核心Filter:名字和实现类 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- 配置拦截器所要拦截的URL,/*的意思是拦截所有请求 -->
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Web应用的默认页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
*** File End ***
*** File: struts.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"struts-2.0.dtd">
<!-- Eclipse提示找不到这个struts-2.0.dtd -->
<!--可以从struts2-core-2.xxxx.jar解压出struts-2.0.dtd,和struts.xml放在一起-->
<struts>
<!-- 包的定义,包名和Action类所在的包没有联系 -->
<!-- extends定义了包要扩展的包,包tt继承了包struts-default中的拦截器和Action -->
<package name="tt" extends="struts-default">
<!-- HelloWorld Action的定义:名字和实现类 -->
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
*** File End ***
至此,可以启动Tomcat服务器,使用http://localhost:8080/tutorial/HelloWorld.action访问action,页面上会显示"Struts is up and running ..."。基本的运行过程如下:
1. 容器接到“HelloWorld.action”请求。根据web.xml中的配置,所有请求被转发到拦截器FilterDispatcher。FilterDispatcher是Struts2框架的入口。
2. Struts2框架再struts.xml中找到 action映射,从而知道了"HelloWorld" action的定义。框架实例化该action并执行action的excute方法。
3. action的excute方法设置消息内容,并返回SUCCESS。框架从action映射找到返回结果SUCCESS对应的页面文HelloWorld.jsp。框架告诉Tomcat容器,把HelloWorld.jsp作为HelloWorld.action请求的response。
4. HelloWorld.jsp被处理时,Struts2的标签<s:property value="message" />调用action的getMessage方法,标签将消息内容添加到response。
5. 浏览器流到一个纯HTML response。
这确实是对Struts2 tutorial的翻译和整理,通过运行这个例子、整理笔记的过程得到对Struts2更直观具体的理解,才是最重要的。
Struts2 Hello World实例
使用:JDK 1.5.0_17 + eclipse-jee-europa-winter-win32 + Tomcat 5.5 + struts-2.0.14-ga
以下是Struts2 Hello World实例的文件内容。在eclipse中新建一个动态Web工程,命名为hello。
*** File: HelloWorld.java ***
// 包名,配置action实现类时,注意引用完整的包路径
package tutorial;
// 导入创建一个Struts2 action必须扩展的一个类
import com.opensymphony.xwork2.ActionSupport;
/**
* 创建一个action HelloWorld
* 创建一个Struts2 action必须扩展ActionSupport类
*/
public class HelloWorld extends ActionSupport {
// 版本序列号,ActionSupport实现了可序列化接口java.io.Serializable
private static final long serialVersionUID = 1L;
// 定义消息字符串常量
public static final String MESSAGE = "Struts is up and running ...";
// action被请求时执行的方法
public String execute() throws Exception {
setMessage(MESSAGE);
// 常量“SUCCESS”是在ActionSupport所实现的Action接口中定义的action的返回结果
return SUCCESS;
}
// 定义消息字符串变量
private String message;
// 设置消息内容
public void setMessage(String message){
this.message = message;
}
// 取得消息内容
public String getMessage() {
return message;
}
}
*** File End ***
*** File: HelloWorld.jsp ***
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- 第一行引用了Struts2的标签库 -->
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<!-- 使用Struts标签获得action的属性值,通过action属性的setters和getters -->
<h2><s:property value="message" /></h2>
</body>
</html>
*** File End ***
*** File: web.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="StrutsWeb" 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">
<!-- Struts2的核心Filter:名字和实现类 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- 配置拦截器所要拦截的URL,/*的意思是拦截所有请求 -->
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Web应用的默认页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
*** File End ***
*** File: struts.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"struts-2.0.dtd">
<!-- Eclipse提示找不到这个struts-2.0.dtd -->
<!--可以从struts2-core-2.xxxx.jar解压出struts-2.0.dtd,和struts.xml放在一起-->
<struts>
<!-- 包的定义,包名和Action类所在的包没有联系 -->
<!-- extends定义了包要扩展的包,包tt继承了包struts-default中的拦截器和Action -->
<package name="tt" extends="struts-default">
<!-- HelloWorld Action的定义:名字和实现类 -->
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
*** File End ***
至此,可以启动Tomcat服务器,使用http://localhost:8080/tutorial/HelloWorld.action访问action,页面上会显示"Struts is up and running ..."。基本的运行过程如下:
1. 容器接到“HelloWorld.action”请求。根据web.xml中的配置,所有请求被转发到拦截器FilterDispatcher。FilterDispatcher是Struts2框架的入口。
2. Struts2框架再struts.xml中找到 action映射,从而知道了"HelloWorld" action的定义。框架实例化该action并执行action的excute方法。
3. action的excute方法设置消息内容,并返回SUCCESS。框架从action映射找到返回结果SUCCESS对应的页面文HelloWorld.jsp。框架告诉Tomcat容器,把HelloWorld.jsp作为HelloWorld.action请求的response。
4. HelloWorld.jsp被处理时,Struts2的标签<s:property value="message" />调用action的getMessage方法,标签将消息内容添加到response。
5. 浏览器流到一个纯HTML response。
这确实是对Struts2 tutorial的翻译和整理,通过运行这个例子、整理笔记的过程得到对Struts2更直观具体的理解,才是最重要的。