Stuts标签库

Struts标签库概述
HTML标签库
Bean标签库
Logic标签库

Struts标签库概述
Struts框架提供了许多的标签,根据功能和使用习惯的不同可以分为五个标签库。
HTML标签库
用来创建能够和Struts框架和其它相应的HTML标签交互的HTML输入表单。
Bean标签库
在访问JavaBeans及其属性,以及定义一个新的Bean时使用。
Logic标签库
可以用来进行逻辑判断、集合迭代和流程控制。
Nested标签库
增强对其它的Struts标签的嵌套使用的能力。
Tiles标签库
创建复合式网页

HTML标签库
生成HTML基本元素的标签
生成表单相关的标签
显示信息的标签

生成HTML基本元素的标签
<html:html>标签
生成HTML<html>元素
<html:base>标签
生成HTML<base>元素
<html:img>标签
生成HTML<img>元素
<html:link>标签
生成HTML<a>元素
<html:rewrite>标签
生成用户请求的URL

举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
<html:base target="_blank" />
<!-- <html:base target="_blank"/> -->
<!-- <html:base target="_parent"/> -->
<!-- <html:base target="_self"/> -->
<!-- <html:base target="_top"/> -->
</head>
<body>
<!-- src或page都代表相对路径(注意page前面有"/") ,alt提示文字,width代表宽度 height代表长度 -->
<html:img src="img/img1.jpg" alt="图片1" width="50px" height="50px" />
<html:img page="/img/img2.jpg" alt="图片2" width="50px" height="50px" />
<br>
<html:link href="a.jsp">
<!-- 生成用户请求的url -->
<html:rewrite page="/a.jsp" />
</html:link>
<br>
<html:link page="/b.jsp">
<html:rewrite page="/b.jsp" />
</html:link>
<br>
<html:link forward="c">
<html:rewrite page="/c.jsp" />
</html:link>
<br>
<%
String name = "fire";
pageContext.setAttribute("NAME", name);
%>
<html:link href="d.jsp" paramId="id" paramName="NAME">
<html:rewrite page="/d.jsp" paramId="id" paramName="NAME" />
</html:link>
<br>
<%
HashMap map = new HashMap();
map.put("name", "fire");
map.put("password", "admin");
pageContext.setAttribute("MAP", map);
%>
<html:link page="/e.jsp" name="MAP">
<html:rewrite page="/e.jsp" name="MAP" />
</html:link>
</body>
</html>

生成表单相关的标签
<html:form>标签 生成表单
<html:text>标签 生成文本框控件
<html:password>标签 生成密码框控件
<html:textarea>标签 生成文本域控件
<html:hidden>标签 生成隐藏文本框控件
<html:radio>标签 生成单选按钮控件
<html:checkbox>标签 生成复选框控件
<html:multibox>标签 生成动态复选框控件
<html:select>标签 生成下拉列表框控件
<html:submit>标签 生成提交按钮控件
<html:reset>标签 生成重置按钮控件
<html:cancel>标签 生成取消按钮控件
举例:
USER.JSP
<%@ page language="java" pageEncoding="gbk"%>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.struts.util.LabelValueBean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%
List list=new ArrayList();
list.add(new LabelValueBean("黑色","black"));
list.add(new LabelValueBean("白色","white"));
list.add(new LabelValueBean("红色","red"));
list.add(new LabelValueBean("黄色","yellow"));
list.add(new LabelValueBean("蓝色","blue"));
pageContext.setAttribute("colors",list);
%>

<html>
<head>
<title>JSP for UserForm form</title>
</head>
<body>
<html:form action="/user">
<html:hidden property="id" value="007"/>
姓名:<html:text property="name"/><br>
密码:<html:password property="pwd"/><br>
性别:
<html:radio property="sex" value="boy">男</html:radio>
<html:radio property="sex" value="girl">女</html:radio><br>
婚否:<html:checkbox property="married"></html:checkbox><br>
爱好:
<html:multibox property="interest" value="book"/>看书
<html:multibox property="interest" value="movie"/>电影
<html:multibox property="interest" value="football"/>足球
<html:multibox property="interest" value="internet"/>上网<br>
国家:
<html:select property="country">
<html:option value="USA">美国</html:option>
<html:option value="China">中国</html:option>
<html:option value="England">英国</html:option>
</html:select><br>
城市:
<html:select property="city">
<html:optionsCollection property="citys" label="name" value="id"/>
</html:select><br>
颜色:
<html:select property="color" multiple="true" size="5">
<html:options collection="colors" property="value" labelProperty="label"/>
</html:select><br>
备注:
<html:textarea property="remark" rows="5" cols="30"/><br>
<html:submit/><html:reset/><html:cancel></html:cancel>
</html:form>
</body>
</html>

OUTPUT.JSP
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title>JSP for UserForm form</title>
</head>
<body>
编号:<bean:write name="userForm" property="id"/><br>
姓名:<bean:write name="userForm" property="name"/><br>
密码:<bean:write name="userForm" property="pwd"/><br>
性别:<bean:write name="userForm" property="sex"/><br>
婚否:<bean:write name="userForm" property="married"/><br>
爱好:
<logic:iterate id="item" name="userForm" property="interest">
<bean:write name="item"/>
</logic:iterate><br>
国家:<bean:write name="userForm" property="country"/><br>
城市:<bean:write name="userForm" property="city"/><br>
颜色:
<logic:iterate id="item" name="userForm" property="color">
<bean:write name="item"/>
</logic:iterate><br>
备注:<bean:write name="userForm" property="remark"/>
</body>
</html>

显示信息的标签
<html:errors>标签
用于显示错误信息
<html:messages>标签
用于显示正常信息
举例:
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html>
<head>
<title>JSP for UserForm form</title>
</head>
<body>
<html:errors property="name"/>
<!-- id:用来命名从消息集合中检索出的每个ActionMessage对象 -->
<!-- message:指定消息的来源。如果为true,则从request或session范围内检索出属性key为Globals.MESSAGE_KEY的ActionMessage对象 -->
<html:messages id="message" message="true">
<bean:write name="message"/>
</html:messages>
<hr>
<html:form action="/user">
name : <html:text property="name"/>
<html:submit/>
</html:form>
</body>
</html>


Bean标签库
访问HTTP请求信息或JSP隐含对象
访问Web应用资源
定义或输出JavaBean

访问HTTP请求信息或JSP隐含对象
<bean:header>标签
访问HTTP请求中的报头信息
<bean:parameter>标签
访问请求中指定参数的值
<bean:cookie>标签
访问Cookie信息
<bean:page>标签
访问JSP隐含对象
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<%
Cookie c=new Cookie("hello","HelloWorld");
c.setMaxAge(24*60*60);
response.addCookie(c);
%>
<html:link href="main.jsp?user=fire">Main</html:link>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>main</title>
</head>
<body>
<bean:header id="host" name="Host"/>
<bean:header id="agent" name="User-Agent"/>
<bean:parameter id="u" name="user"/>
<bean:cookie id="c" name="hello" value="hi"/>
<bean:page id="s" property="session"/>
<b>Host:</b><bean:write name="host"/><hr>
<b>User-Agent:</b><bean:write name="agent"/><hr>
<b>Param:</b><bean:write name="u"/><hr>
<b>Cookic_name:</b><bean:write name="c" property="name"/><br>
<b>Cookic_value:</b><bean:write name="c" property="value"/><hr>
<b>Session_id:</b><bean:write name="s" property="id"/>
</body>
</html>

访问Web应用资源
<bean:message>标签
显示资源文件中的信息
<bean:resource>标签
把Web资源装载到一个JavaBean中
<bean:struts>标签
访问Struts的内在配置对象
<bean:include>标签
包含一个Web资源
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<bean:message key="hello" arg0="fire"/><hr>
<bean:struts id="f" forward="main"/>
<bean:write name="f" property="name"/>:
<bean:write name="f" property="path"/><hr>
<bean:resource id="m1" name="/main.jsp"/>
<bean:write name="m1"/><hr>
<bean:include id="m2" page="/main.jsp"/>
<bean:write name="m2" filter="false"/>
</body>
</html>

定义或输出JavaBean
<bean:define>标签
定义一个变量
<bean:size>标签
获得Collection或Map集合的长度
<bean:write>标签
显示JavaBean或其属性的内容
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<bean:define id="h" value="HelloWorld"/>
<bean:write name="h"/><hr>
<%
String[] s=new String[5];
request.setAttribute("array",s);
%>
<bean:size id="l" name="array"/>
<bean:write name="l"/>
</body>
</html>

Logic标签库
比较运算
字符串运算
判断指定内容是否存在
控制请求的转发与重定向
循环遍历集合元素



比较运算
<logic:equal>标签
比较变量是否与指定变量相等
<logic:notEqual>标签
比较变量是否与指定变量不相等
<logic:greaterEqual>标签
比较变量是否大于或等于指定变量
<logic:greaterThan>标签
比较变量是否大于指定变量
<logic:lessEqual>标签
比较变量是否小于或等于指定变量
<logic:lessThan>标签
比较变量是否小于指定变量
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<html:link href="main.jsp?user=fire&x=50&y=100">Main</html:link>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<logic:equal value="fire" parameter="user">
user=fire<hr>
</logic:equal>
<logic:notEqual value="fire" parameter="user">
user!=fire<hr>
</logic:notEqual>
<logic:greaterEqual value="50" parameter="x">
x>=50<hr>
</logic:greaterEqual>
<logic:greaterThan value="30" parameter="x">
x>30<hr>
</logic:greaterThan>
<logic:lessEqual value="100" parameter="y">
y<=100<hr>
</logic:lessEqual>
<logic:lessThan value="200" parameter="y">
y<200
</logic:lessThan>
</body>
</html>

字符串运算
<logic:match>标签
判断变量中是否包含指定的常量字符串
<logic:notMatch>标签
判断变量中是否不包含指定的常量字符串
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>index</title>
</head>
<body>
<%
request.setAttribute("users","admin,user1,user2");
%>
<b>users=</b><bean:write name="users"/><hr>
<logic:match value="user1" name="users">
users中包含user1<hr>
</logic:match>
<logic:notMatch value="user3" name="users">
users中不包含user3<hr>
</logic:notMatch>
</body>
</html>

判断指定内容是否存在
<logic:empty>标签
判断指定的变量是否为null,或者为空字符串
<logic:notEmpty>标签
判断指定的变量是否不为null,或者不为空字符串
<logic:present>标签
判断指定的对象是否存在
<logic:notPresent>标签
判断指定的对象是否不存在
<logic:messagesPresent>标签
判断指定的消息是否存在
<logic:messagesNotPresent>标签
判断指定的消息是否不存在
举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="org.apache.struts.action.*" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<bean:define id="s1" value="hi"/>
<jsp:useBean id="d1" class="java.util.Date" scope="request"/>
<%
ActionMessages messages=new ActionMessages();
messages.add("hello",new ActionMessage("hello"));
request.setAttribute("am",messages);
%>
<logic:notEmpty name="s1">
<bean:write name="s1"/><hr>
</logic:notEmpty>
<logic:empty name="s2">
没有定义变量s2<hr>
</logic:empty>
<logic:present name="d1">
<bean:write name="d1"/><hr>
</logic:present>
<logic:notPresent name="d2">
没有定义对象d2<hr>
</logic:notPresent>
<logic:messagesPresent name="am" property="hello">
<html:messages id="h" name="am" property="hello">
<bean:write name="h"/><hr>
</html:messages>
</logic:messagesPresent>
<logic:messagesNotPresent name="am" property="world">
不存在key为"world"的资源消息
</logic:messagesNotPresent>
</body>
</html>

控制请求的转发与重定向
<logic:forward>标签
进行请求转发
<logic:redirect>标签
进行请求重定向
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<html:link href="a.jsp">forward</html:link><br>
<html:link href="b.jsp">redirect</html:link>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<logic:forward name="c"/>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<logic:redirect forward="c"/>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<h1>C</h1>
</body>
</html>

循环遍历集合元素
<logic:iterate>标签
用于循环遍历集合
Collection
Enumeration
Iterator
Map
Array

举例:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>index</title>
</head>
<body>
<%
HashMap map=new HashMap();
map.put("b01","张三");
map.put("b02","李四");
map.put("b03","王五");
map.put("b04","路人甲");
map.put("b05","路人乙");
request.setAttribute("MAP",map);
%>
<logic:iterate id="item" name="MAP">
<bean:write name="item" property="key"/>
<bean:write name="item" property="value"/>
<br>
</logic:iterate>
<hr>
<logic:iterate id="item" name="MAP" indexId="id" offset="1" length="3">
${id+1}.
<bean:write name="item" property="key"/>
<bean:write name="item" property="value"/>
<br>
</logic:iterate>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值