Struts2之Hello World | #struts2

[url=http://struts.apache.org/2.x/]Struts2[/url]官方推荐教程[url=http://www.vaannila.com/struts-2/struts-2-tutorial/struts-2-tutorial.html]Vaan Nila's Struts 2 Tutorial[/url]中[url=http://www.vaannila.com/struts-2/struts-2-example/struts-2-hello-world-example-1.html]Struts 2 Hello World Tutorial[/url]一节是Struts2极好的入门文章,很适合我们这些新人学习。

该文章以一个简单的Hello World应用为便,向我们展示了struts2的魅力。

该例的流程非常简单:用户输入姓名后提交,在新的页面中显示问候语。

使用Struts二编写这个Hello World,需要如下步骤:

[list=1]
[*]创建Web工程并添加struts包
[*]配置过滤器
[*]配置struts.xml
[*]编写action和视图
[*]布署运行
[/list]

[b]1. 创建web工程并添加struts包[/b]

新建一个web工程Struts2Tutorial,在WEB-INFlib文件夹下加入

[list]
[*]struts2-core-2.1.8.1.jar
[*]xwork-core-2.1.6.jar
[*]freemarker-2.3.15.jar
[*]commons-logging-1.0.4.jar
[*]commons-fileupload-1.2.1.jar
[*]ognl-2.7.3.jar
[/list]

[b]2. 配置过滤器[/b]

打开web.xml文件,添加过滤器定义如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


过滤器FilterDispatcher是Struts2的核心,最常见的用途就是将用户请求转发到不同的Action执行,并返回执行结果给用户。

一般情况下,FilterDispatcher的url-pattern必须匹配所有用户请求。

见:[url]http://struts.apache.org/2.1.6/struts2-core/apidocs/org/apache/struts2/dispatcher/FilterDispatcher.html[/url]
[quote]IMPORTANT: this filter must be mapped to all requests. Unless you know exactly what you are doing, always map to this URL pattern: /*[/quote]

[b]3. 配置struts.xml[/b]

struts.xml文件用于配置Action,该文件默认应放置在工程的classpath下面,如src文件夹下,当工程编译后部署后,程序访问的其实是WEB-INF/classes/struts.xml文件。

struts.xml的dtd文件可以在struts2-core-2.1.8.1.jar的根目录下找到。

struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
<package name="default" extends="struts-default">
<action name="helloworld" class="dive.into.struts2.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>


其中pack标签声明了一个默认的包default,它继承了struts的默认配置struts-default。

action标签建立了一个名为helloworld的Action,我们可以通过/helloworld.action访问它。

result标签定义了一个名SUCCESS的执行结果,它指向/success.jsp页面。

[b]4. 编写action和视图[/b]

建立类dive.into.struts2.HelloWorld类,这是一个Action,但事实上你不用继承什么类或者实现什么接口,只需要让它符合以下规则即可:

  [b]一个返回值为String类型的public方法execute()[/b]

  其返回值会用于识别struts.xml该Action下的result标签,从而决定跳转的地址。

  [b]一组属性及其setter和getter,如果必要的话[/b]

  该属性可以在jsp页面的struts标签直接访问,非常方便。

[i][b]HelloWorld.java[/b][/i]
package dive.into.struts2;

public class HelloWorld {
private String userName;
private String message;

public String execute() {
setMessage("Hello, " + getUserName());
return "SUCCESS";
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


[i][b]index.jsp[/b][/i]
<%-- 使用Struts2标签库 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>

<%-- 创建一个提交地址为名为helloworld的Action的表单 --%>
<s:form action="helloworld">

<%--
创建一个文本域,表单提交后,该文本域的值会自动填充到
helloworld对应的Action对象的userName属性上
--%>
<s:textfield name="userName" label="User Name"/>

<%-- 创建一个提交按钮 --%>
<s:submit/>
</s:form>
</body>
</html>


[i][b]success.jsp[/b][/i]
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>
<h1>
<%--
该页面由helloworld跳转而来
此标签将会显示helloworld实例的message属性的值。
--%>
<s:property value="message"/>
</h1>
</body>
</html>


[b]5. 布署运行[/b]

在tomcat中布置工程并通过[url]http://localhost:8080/Struts2Tutorial[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章