Struts2教程三——Using Struts 2 Tags

本教程假设你已完成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了解更多信息。

index.jsp
<%@ 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有:

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中加入以下内容

url tag with param
<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中加入如下内容。

Struts 2 Form
<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。

Struts Form Tags Converted To 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文件有如下代码:

Struts Property Tag
<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变量。

Add Static Field
private static int helloCount = 0;
	
public int getHelloCount() {
	return helloCount;
}

public void setHelloCount(int helloCount) {
	HelloWorldAction.helloCount = helloCount;
}

每次执行execute让helloCount加1,如下:

Increase helloCount
helloCount++;

用户每点一次链接,helloCount加1。

使用Struts 2 property标签,在HelloWorld.jsp文件h2标签后加入如下内容:

Use Property Tag To Display helloCount Value
<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方法:

Add toString Method To Class MessageStore
public String toString() {
		
	return message + " (from toString)";
		
}	

在HelloWorld.jsp中加入下面内容:

Using Property Tag to Call toString
<p><s:property value="messageStore" /></p>

可以看见toString方法确实被调用了


虽然本节说了不少,但只是冰山一角,看Struts 2 Tag Reference会发现更多信息

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值