Struts2 官方教程之Using Struts 2 Tags (五)

来源:互联网 发布:java电商项目描述 编辑:程序博客网 时间:2024/05/16 11:06

This tutorial assumes you've completed the Hello World tutorial and have a working Hello_World_Struts2_Ant (or Hello_World_Struts2_Mvn) project. The example code for this tutorial, Using_Tags_Struts2_Ant or Using_Tags_Struts2_Mvn, is available on Google Code -http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Using_Tags_Struts2_Ant (or Using_Tags_Struts2_Mvn). In that folder will be a README.txt file with instructions on how to build and run the example application.

假设你已经完成了前面的小例子,以及给出了,本节所需要要下载的链接。在README.txt文件中介绍了怎样build和运行这个案例。

In the Hello World lesson, we added to the index.jsp a Struts 2 url tag to create a hyperlink to the hello.action. This tutorial will explore the url and other Struts 2 tags further.在HelloWorld例子中,我们在index.jsp中加了Struts 2 url标签来创建了一个超链接到hello.action,本教程将会探讨url和其他的一些Struts 2标签

Web applications differ from conventional websites in that web applications can create a dynamic response. To make it easier to reference dynamic data from a page, the Struts 2 framework offers a set of tags. Some of the tags mimic standard HTML tag while providing added value. Other tags create non-standard, but useful controls.

web应用和传统网站的区别在于,应用程序能够动态响应。为了使之从一个网页更加容易地引用动态数据,Struts 2提供了一系列标签。一些标签模仿了标准HTML标签同事也提供了一些增加的值的标签。另一些标签创建是非标准的,但是是非常有用控制标签。

To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive is<%@ taglib prefix="s" uri="/struts-tags" %>. So the prefix for all the Struts 2 tags will be "s".要使用Struts 2标签,你需要添加一个标签指令,一般是<%@ taglib prefix="s" uri="/struts-tags" %>。
If you want to actually read the Struts 2 tag TLD file, you'll find it in the META-INF folder of the Struts 2 core jar.

Struts 2 url Tag

One use of the Struts 2 Tags is to create links to other web resources, especially to other resources in the local application.

使用Struts 2标签创建一个链接到别一个应用,特别是本来的其他资源。

While HTML provides a simple a tag for creating hyperlinks, the HTML tag often requires us to include redundant information. Also the HTML tag cannot easily access dynamic data provided by the framework.   

HTML提供一个简单标签来创建超链接,它常常需要我们包含冗余的信息。所以HTML标签不能轻松地访问这个框架提供的动态数据。

A very common use case in web applications is linking to other pages. In the Hello World tutorial we added to index.jsp a link to the hello.action using the Struts 2 url tag. Please refer to theurl documentation for more information about the url tag.

一个在web应用中常常使用的例子是链接到其他页。你可以点击url documentation了解更多url tag

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>

When you run the Hello World tutorial in your Servlet container and then mouse over the Hello World hyperlink created by the Struts 2 url tag you'll see that the URL created is hello.action (relative to the web context's root folder).

Examine the struts.xml configuration in the Hello World tutorial and you will find this:

struts.xml
...<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">  <result name="success">/HelloWorld.jsp</result></action>...

The action node above maps the hello.action to the execute method of class HelloWorldAction. If the execute method returns success, the view page HelloWorld.jsp (in web context root folder) will be returned to the user.

上面说的,主要意思就是,当点击含有hello的超链接后,之后的一些响应步骤,之前已经说过,此处不再赘述。

A common use case is that the URL also needs to include a value for a query string parameter such as userName. To add a query string parameter and its value use the Struts 2 param tag, nested inside the url tag. 经常在使用Url标签时,需要包含一个值用于查找的字符串,比如username,下面就是使用Struts 2参数标签的例子。

For the Using Tags tutorial add the following to index.jsp just after the link for Hello World.

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>

Rather then put the url tag as the value for the anchor tag's href attribute, we've separated out the s:url tag into its own code block. Nested inside the url tag is the Struts 2 param tag. This tag lets you specify a parameter name (e.g. userName) and a value for that paramter (e.g. Bruce Phillips).

Notice the use of the var attribute. The value of the var attribute is a reference we can use later in our code to refer to the url created.

Examine the anchor tag above. Notice the value of the href attribute is ${helloLink}. The view page will substitute the hyperlink we created using the url tag for the ${helloLink} reference. Note that the query string parameter will be properly URL-encoded (Bruce+Phillips).

In the next tutorial we'll cover how Struts can access the query string parameter value.

上半部分代码声明了一个变量(var="helloLink"),下半部分代码使用了这个变量。实际上这个变量就是一个带参数userName(值为Bruce Phillips)的action。

这种方式比<p><ahref="<s:url action='hello'/>">Hello World</a></p> 要好看的多,而且可以复用。

在经过编译后的HTML代码应该是这个样子的:

<a href="/struts2_003/hello.action?userName=Bruce+Phillips">Hello Bruce Phillips</a>

Struts 2 Form Tags

Most applications will use several data entry forms. The Struts 2 tags make creating input forms easy. Consult theForm Tags Reference for all the details about the Struts 2 form tags.

Each of the Struts 2 form tags has numerous attributes to mimic the normal HTML form tag attributes.

To create the outer shell of the form, use the Struts 2 form tag. The action attribute sets the action name to submit to.

Add the following markup to index.jsp after the Hello Bruce Phillips link.

Struts2 textfield 标签提供了一个输入文本框并且 submit 标签创建了一个  submit 按钮, 当页面被请求返回到浏览器时能看到

 

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>

The Struts 2 textfield tag provides a input html tag of tag text and the submit tag creates a submit button. When the index page is return by the server to the browser you should see:

The Struts form, textfield, and submit tags were converted to this HTML.

Struts Form Tags Converted To HTML

编译后的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>

Note how Struts 2 created a table inside the form to position the form elements. In later tutorials you'll learn how to specify the layout (table, CSS). The Struts 2 textfield tag created an HTML input tag of type text with a name value that matches the name value of the textfield tag. Struts 2 also created a label HTML tag based on the label value of the textfield tag.

要注意的是 Struts2 在form中创建了一个表格用来定位 form的元素,、Struts2 textfield 标签创建了一个HTML输入标签,它的名称值和Struts2 textfield中的name值是一样的. Struts2也基于textfield标签创建了一个 HTML Label 标签.

//补充说明: label 的作用是当你点击这个Label,即Your name时, 文档框将获得焦点. 还记得姓名单选按钮吗? 如果要选中单选按钮必须点击那个小圆圈, 但是有了 label 通过设置 label 标签的for属性时,单击一个label 就表示选中了 for="id" ,那个id所指向的元素. 这样就不用戴着眼镜去选那么小的圆圈了.. Struts2 ,自动帮我们设置好了这个步骤.

 

In the next tutorial we'll cover how to use Struts 2 to process this form submission.

Struts 2 property tag

In the Hello World tutorial's example application on JSP HelloWorld.jsp was this code:

Struts Property Tag
<s:property value="messageStore.message" />

The most common use of the property tag is used to "get" the value returned by calling a public get method (of the Action class) and then to include that value in the HTML returned to the browser.

As discussed in the Hello World tutorial, the value of "messageStore.message" instructs Struts 2 to first call method getMessageStore of the Action class. That method call returns a MessageStore object. The ".message" part instructs Struts 2 to call the getMessage method of the MessageStore object. The getMessage method returns a String which will be included in the HTML returned to the browser.

On very useful feature of the Struts 2 property tag is that it will automatically convert the most common data types (int, double, boolean) to their String equivalents. To demonstrate this feature let's add a static int variable to class HelloWorldAction.

在Hello Wolrd 教程中已经谈论过了关于 messageStore.message 的值,它告诉Struts2第一次请求的是Action类的getMessageStore()方法. 这个方法将会返回一个 MessageStore的对象. ".message"的部分告诉Struts2要去调用MesageStore对象的getMessage()方法. getMessage()方法会返回一个字符串,这个字符串最终将会包含在返回的HTML页面中显示。

 

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

Each time the execute method is called we'll increase helloCount by 1. So add this code to the execute method of class HelloWorldAction.

Increase helloCount
helloCount++;

Whenever a user clicks one of the links on page index.jsp (or submits the form), method execute of class HelloWorldAction will be run and the static field helloCount will be increased by one.

To include the value of the helloCount attribute in the HelloWorld.jsp we can use the Struts 2 property tag. Add the following to HelloWorld.jsp after the h2 tag.

Use Property Tag To Display helloCount Value
<p>I've said hello <s:property value="helloCount" /> times!</p>

So even though the getHelloCount method returns an integer type, Struts 2 converted it to type String and placed it into the body of the p tag.

Note that even though helloCount is a static field, the get and set methods for helloCount are not static. For Struts 2 to call the getHelloCount method to get the value of helloCount, the getHelloCount method cannot be static.

Struts2 property 标签一个非常有用的特性是它能够自动的转换普通的数据类型(int,double,boolean)成一个String类型的等价物。

//注意:尽管 helloCount是一个static类型的数据, 但是它的 get/set 方法不是static类型的. 如果在Struts2中通过调用 getHelloCount()的方法来得到 helloCount 的值, 那么 getHelloCount()方法不能是 static 类型的.

 

If the value returned by the get method is an object, then the property tag will cause Struts 2 to call the object's toString method. Of course, you should always override Class Object's toString method in your model classes. Add the following toString method to the MessageStore class:

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

Add the following to HelloWorld.jsp

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

Since getMessageStore of HelloWorldAction class returns a MessageStore object, Struts 2 will call the toString method of class MessageStore. The string returned by that toString method will be displayed in the browser.

We covered a lot in this tutorial, but we've really only scratched the surface of how to use the Struts 2 tags. Consult theStruts 2 Tag Reference for much more information about all the Struts 2 tags.

原创粉丝点击