读书人

struts2官方课程(二)-使用struts2的标

发布时间: 2012-10-28 09:54:44 作者: rapoo

struts2官方教程(二)--使用struts2的标签

?

?在教程一中我们使用了一个url标签用来创建了一个链接到hello.action。当你运行web项目,并点击创建的url时,你将会在浏览器中看见该url是hello.action。action节点中的hello将会执行HelloWorldAction的execute方法,成功后将会返回到helloworld.jsp页面。

1、带有参数的url标签

?通常情况下,我们需要在url中添加请求参数,比如说userName等。为了添加一个参数,我们可以使用struts2的 ? ? ? ? ? param标签,嵌套在url标签里

<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>

?不像在教程一中那样,我们单独把url列在一个块中,其中嵌套的是参数,这个标签让你指定一个参数名比如 ? ? ?说userName和一个值比如说Bruce Phillips。请注意var属性,该属性告诉框架我们可以使用该属性 ? ? ? ?来指定所创建的url。即页面锚标记中的${helloLink}。

?

2、struts2的表单标签创建strut2的表单标签如下
<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>

textfield标签提供了一个文本输入框,submit标签创建了一个提交按钮

?

3、struts2 property 标签

在教程一种,我们使用了

?

<s:property value="messageStore.message" />
?

?来取得messageStore对象的message字段。struts框架首先调用Action类的getMessageStore 方法返回MessageStore对象,然后再根据.message部分调用getMessage 方法取得message,返回到浏览器。

struts2的标签会帮助你自动转换所有基本数据类型?

?

添加静态字段来演示以上的特性

private static int helloCount = 0;public int getHelloCount() {return helloCount;}public void setHelloCount(int helloCount) {HelloWorldAction.helloCount = helloCount;}
helloCount++;
??每次当页面指向该类的action被指向后,helloCount将被递增
<p>I've said hello <s:property value="helloCount" /> times!</p>

教程一地址,附有项目文件,教程二是基于一的更改

读书人网 >软件架构设计

热点推荐