Struct2(三) Struct2 标签
在上一篇 Struct2(二)中,我们新建了工程Struct2test用来验证hello World 程序,在index.jsp中,我们添加了一个Struct2 uri 标签用来创建一个指向hello.action 的超链接,这篇是更加详细的介绍Struct2 的标签。
在创建动态的响应上,web应用程序,不同于传统的应用程序。为了更加简单的引用一个页面的动态数据,Struct2 框架提供了一套标签。一些标签模仿标准的HTML标记,同时提供了扩展。其他标签不是标准的标签,但能提供比较有用的控制。
注意:
为了使用Struct2 标签集,你必须包括一个标签库命令,一般就是:
<%@ taglib prefix="s" uri="/struts-tags" %>.
1. Struct2 的url标签
Struct2 标签其中的一个就是创建一个指向其他web资源的链接,特别是指向本地应用程序中的资源。
注意:
HTML中提供一个标签a来创建超链接,HTML标签常常要求冗余的信息,并且不是特别方便的能够访问到框架提供的信息。
这个标签一个比较常用的功能是:链接到其他的页面,在hello World 例子中 index.jsp 中hello.action 就是使用了一个Struct2 url 标签,想了解更多关于url标签的,见 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>
与之相对应的是:struts.xml
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
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>
嵌套在标签里面的叫做参数标签,这个标签可以让我们指定一个参数
标签的生命为var类型的名称,然后能够使用这个变量为${名称},免除重复定义。
2. Struts 2 Form Tags
例子:
<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>
<s:form> 为定义一个表单
<s:textfield> html的输入的标签
<s: submit> 提交按钮
When the index page is return by the server to the browser you should see:

转为为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>
3. Struts 2 property tag
<s:property value="messageStore.message" />
修改我们上一个例子,来测试这篇的标签
在HelloWorldAction中加入:
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
记录Action执行的次数,相对应的在execute方法中,加入:
helloCount++;
每次点击超链接的时候,就会增加。
显示点击的次数:
<p>I've said hello <s:property value="helloCount" /> times!</p>
在MessageStore 中增加:
public String toString(){
return message +"from to String.";
}
展示的效果:
<p><s:property value="messageStore" /></p>
最后的HelloWorld.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>Hello World!</title>
</head>
<body>
<p>I've said hello <s:property value="helloCount" /> times!</p>
<p><s:property value="messageStore" /></p>
<h2><s:property value="messageStore.message" /></h2>
</body>
</html>
最终的效果:

上面只是Struct2 几个简单的标签,更多的标签见:Struts 2 Tag Reference 。
Struct2(三) Struct2 标签的更多相关文章
- Css - 利于搜索引擎收录的三个标签
Css - 利于搜索引擎收录的三个标签 <head> <meta charset="utf-8" /> <title>京东(JD.COM)-正品 ...
- 一、变量.二、过滤器(filter).三、标签(tag).四、条件分支tag.五、迭代器tag.六、自定义过滤器与标签.七、全系统过滤器(了解)
一.变量 ''' 1.视图函数可以通过两种方式将变量传递给模板页面 -- render(request, 'test_page.html', {'变量key1': '变量值1', ..., '变量ke ...
- BarTender怎么打印公式化的三列标签
有小伙伴在业务上有这样的需求:使用BarTender打印一行三列的标签,如下A,B,C三个并排.第一行 A1=a B1=a*2-1 C1=a*2:第二行 A2=a+1 B2=(a+1)*2-1 C2= ...
- 网站页面SEO的三个标签怎么写有利【转载】
转载自:代明博客 在SEO界,自从夫唯老师提出“四处一词”的概念以来,不管是搜索引擎还是SEOer,都格外重视页面的三个标签.三个标签书写是否成功,在很大程度上决定了网页是否能有好的排名.今天代明博客 ...
- <script>, <script async>, <script defer> 三种标签的区别
<script>, <script async>, <script defer> 三种标签的区别 <script>标签 阻塞html parsing 脚 ...
- 三个标签完成springboot定时任务配置
1. 问题描述 Java项目定时任务是必备模块,月高风黑夜跑个批处理,记录或者统计一些系统信息. 2. 解决方案: 结合springboot,只需三个标签就能完成定时任务配置. 2.1 标签1 用在s ...
- js之oop <三>属性标签
读取属性标签 获取对象属性标签,用 Object.getOwnPropertyDescriptor方法.getOwnPropertyDescriptor(); 参数:属性所在对象(object),属性 ...
- [19/05/15-星期三] HTML_body标签(超链接标签和锚点)
一.超链接标签 <html> <head> <meta charset="UTF-8"> <title>04 body超链接标签学习 ...
- HTML5快速入门(三)—— 标签综合运用
前言: 1.HTML5的发展非常迅速,可以说已经是前端开发人员的标配,在电商类型的APP中更是运用广泛,这个系列的文章是本人自己整理,尽量将开发中不常用到的剔除,将经常使用的拿出来,使需要的朋友能够真 ...
随机推荐
- iBatis查询结果部分为null的解决办法
今天第一天接触iBatis,没有系统学习过,遇到了一个简单却闹心的错误:用iBatis查询数据库中某个表的多列结果作为一个对象返回时,会出现对象的部分属性为null值得错误.例如,查询用户表中的用户I ...
- HTML5 简单实现刮刮乐效果
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF自定义下拉控件
可以搜索的下拉条 using System; using System.Collections; using System.Collections.Generic; using System.Coll ...
- 关于在centos7上安装vmtools的各种报错的问题处理
基本上如果安装vmtools时报错的错,首先记录一下报错信息 例如,本次遇见的就是 /tmp/modconfig-8mD7iy/vmhgfs-only/page.c:1625:23: 错误:提供给函数 ...
- (转)苹果推送通知服务教程 Apple Push Notification Services Tutorial
本文译自http://www.raywenderlich.com/.原文由iOS教程团队 Matthijs Hollemans 撰写,经原网站管理员授权本博翻译. 在iOS系统,考虑到手机电池电量,应 ...
- mysql中pager命令妙用
pager命令的妙用在mysql,可以大大提高工作效率. 一 当处理大量数据时,不想显示查询的结果,而只需知道查询话费的时间. mysql> select * from t3; +----- ...
- 通过数组初始化链表的两种方法:指向指针的引用node *&tail和指向指针的指针(二维指针)node **tail
面试高频题:单链表的逆置操作/链表逆序相关文章 点击打开 void init_node(node *tail,char *init_array) 这样声明函数是不正确的,函数的原意是通过数组初始化链表 ...
- jdk\willy\集群安装
一.telnet用户验证端口: yum install telnet #也可以使用 yum install telnet-ssl 安装支持SSL的telnet方式,这样安全一些,不建议使用标准的tel ...
- javascript正则
<script type="text/javascript"> //去除两边空格,如果要去除所有空格,使用/\s*即可/ String.prototype.trim ...
- RDLC 聚合函数按条件求和,显示"错误号"
=Sum(IIf(Fields!AValue.Value >0,Val(Fields!AValue.Value),)) 一直显示错误号 修改为 =Sum(IIf(Fields!AValue.Va ...