Struts2-第二篇-Using Struts 2 Tags

在上一篇Helloworld篇中,我们添加index.jsp文件中用到struts2 url tag标签去创建一个超级链接hello.action(<s:url action='hello'''/>).接下来我们继续探究url和其他struts2 tag。

Web应用与传统的web站点不同的是,web应用能够创建一个动态的响应。目的是使用引用动态数据更容易从一个page,struts提供一系列tags。一些标签和html很类似,其他标签非标准的,但是很有效的控制。

Struts2标签的作用之一就是去创建链接到其他web资源,特别是在同一个应用中的其他资源。

就像在HelloWorld篇幅中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>

当你运行HelloWorld项目是,鼠标点击超级链接是,你看到URL是"hello.action".再检查struts.xml配置

...
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
  <result name="success">/HelloWorld.jsp</result>
</action>
上面的action node映射hello.action去执行HelloWorldAction类中的方法execute。如果方法返回字符串success,页面view HelloWorld.jsp将返回给用户。


在index.jsp后添加如下带参数的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>

相比较放把值在tag's href attribute,上面把s:url标签和实际的代码块分开。内嵌在url标签的是struts2 param tag.这个标签让你指定一个参数名和参数值。

注意变量属性var="helloLink"的用处。var属性值是一个引用,后面我们能用在我们代码中去引用url.

注意上面的锚点,查看href的属性值是${helloLink}.这个页面将替代我们创建的超级链接,查询字符串参数将是properly url-encoded.

Struts 2 Form Tags

大部分的应用有几种数据输入形式。struts2 tag使输入更容易创建。

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

打开浏览器:http://localhost:8080/Using_Tags_Struts2_Mvn/index.action  注意url中,index.action,不要写成index.jsp,因为struts.xml配置文件中都是index.如下:

<action name="index">
<result>/index.jsp</result>
</action>




Struts2 property tag

在HelloWorld中的HelloWorld.jsp添加代码:

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


在HelloWorldAction.java中,添加如下代码:

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.

helloCount++;

在helloworld.jsp中添加:

< p >I've said hello < s:property  value = "helloCount"  /> times!</ p >

在MessageStore中添加:

public  String toString() {
         
     return  message +  " (from toString)" ;
         
这时候可以在HelloWorld.jsp中添加如下代码:

<p><s:property value="messageStore" /></p> 实现的效果和messageStore.message一样。

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值