| 本教程假设你已完成Hello World教程,可以在http://code.google.com/p/struts2-examples/downloads/list下载所有代码。 |
在Hello World课程中,在index.jsp 中使用了Struts 2 url 标签,本篇教程将学习更多的标签,web应用 不同于传统的web站点的地方在于可以创建动态页面,为了在页面中更容易的使用动态数据,Struts 2 framework提供了许多标签。一些标签模仿标准HTML标签的同时又额外提供了一些值,另一些是非标准的,但也容易使用。
| 为了在view中使用Struts 2标签,必须包含标签库指令,就是<%@ taglib prefix="s" uri="/struts-tags" %>。所以struts 2标签前缀都是"s"。 如果想看看Struts 2标签的TLD文件,可以在Struts 2 core jar的META-INF目录下找到 |
Struts 2 url标签
Struts 2标签的一个用途就是创建到其他web资源的链接,尤其是本应用的其他资源。
| HTML提供简单的a标签来创建超链接,HTML标签经常要我们填写多余的信息,况且HTML标签也不能很容易的访问framework提供的动态数据。 |
连接到其他页面是很常用的功能,在Hello World教程中我们有链接到hello.action的标签,可以参考url documentation了解更多信息。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>
运行Hello World,点击Hello World超链接,可以看到hello.action URL被生成。
查看struts.xml有:
...
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
...
action节点映射到hello.action,并执行execute方法,如果execute方法返回success,则发回HelloWorld.jsp 页面。
URL标签还可以带参数。
在index.jsp中加入以下内容
<s:url action="hello" var="helloLink">
<s:param name="userName">Bruce Phillips</s:param>
</s:url>
<p><a href="${helloLink}">Hello Bruce Phillips</a></p>
s:url形成了单独的代码块,并嵌入了param标签,此标签可以标注一个参数名和值。
注意var属性,用这个值我们可以引用URL。
看看anchor标签,会发现${helloLink}。view page会用我们创建的url标签替换${helloLink},参数会被适当的编码到URL中。
| 下篇教程讲解怎样访问URL查询参数。 |
Struts 2表单标签
多数应用都会用到表单,Struts 2标签可以很容易的额创建表单,浏览Form Tags Reference有更多Struts 2表单标签。
每个Struts 2表单标签都有很多相似于HTML的属性。
Struts 2 form标签的action属性表示要提交到的action。
在index.jsp中加入如下内容。
<p>Get your own personal hello by filling out and submitting this form.</p>
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
Struts 2 textfield标签提供了文本输入框,submit标签创建一个提交按钮,运行后会看见:

Struts form,textfield,submit标签被转换成如下HTML。
<form id="hello" name="hello" action="/Using_Tags_Struts2_Mvn/hello.action;jsessionid=3471d76027b5342cab44f297b567" method="post">
<table class="wwFormTable">
<tr>
<td class="tdLabel"><label for="hello_userName" class="label">Your name:</label></td>
<td><input type="text" name="userName" value="" id="hello_userName"/></td>
</tr>
<tr>
<td colspan="2"><div align="right"><input type="submit" id="hello_0" value="Submit"/>
</div></td>
</tr>
</table>
</form>
注意Struts 2生成的table,后面教程会学到怎样布局,textfield的名称和生成的HTML text的名称是一样的
| 下节学习如何处理表单提交的数据。 |
Struts 2 property标签
在Hello World 教程的HelloWorld.jsp文件有如下代码:
<s:property value="messageStore.message" />
property标签最常用的用途是得到Action类getxxx方法返回的值,然后转换成HTML发回浏览器。
在Hello World 中, "messageStore.message"告诉Struts 2先调用Action类的getMessageStore方法,返回一个MessageStore 对象, ".message"又使Struts 2调用MessageStore的getMessage方法,getMessage返回一个String,这个String就是要显示的内容。
Struts 2 property标签的一个非常有用的特性是可以自动转换常用的数据类型(int, double, boolean)为对应的String。为了证明这个特性,我们在HelloWorldAction中加入一个static int变量。
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
每次执行execute让helloCount加1,如下:
helloCount++;
用户每点一次链接,helloCount加1。
使用Struts 2 property标签,在HelloWorld.jsp文件h2标签后加入如下内容:
<p>I've said hello <s:property value="helloCount" /> times!</p>
可见即使getHelloCount方法返回一个整数,Struts 2也可以转成String。
| 注意:即使helloCount是一个satic域,getter和setter方法并不是static,因为为了让Struts 2可以调用getHelloCount方法,所以getHelloCount不可以为static。 |
如果返回对象,property标签会使Struts 2调用对象的toString方法,所以在model class中都要重写toString方法,在MessageStore class中加入如下toString方法:
public String toString() {
return message + " (from toString)";
}
在HelloWorld.jsp中加入下面内容:
<p><s:property value="messageStore" /></p>
可以看见toString方法确实被调用了

虽然本节说了不少,但只是冰山一角,看Struts 2 Tag Reference会发现更多信息
本文通过实战演示Struts2中的多种标签用法,包括URL标签、表单标签及property标签等,介绍如何利用这些标签简化Web开发过程。
1101

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



