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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章