Struts Bean标签库中的标签可以访问已存在的JavaBean以及它们的属性,还可以定义新的Bean,把它存放在page范围内或用户指定的范围内,供网页其他元素访问。Bean标签库大概分为三类:
- 用于访问HTTP请求信息或JSP隐含对象的Bean标签
- 用于访问Web应用资源的Bean标签
- 用于定义或者输出JavaBean的Bean标签
访问HTTP请求信息或JSP隐含对象
- <bean:cookie>:访问Cookie信息
- <bean:header>:访问HTTP请求中的Header信息
- <bean:parameter>;访问请求参数。
- <bean:page>:访问JSP隐含对象。
1.<bean:cookie>标签
该标签可以检索保存在浏览器中的Cookie,具有属性id,name,multiple,value,id是对cookie在该页面中的唯一标识,在页面其它地方可以通过id进行引用。相当于用cookie直接引用。Name是相应的cookie名称。Multiple可以赋予任意值,如果设置了该属性,可以检索出所有和Cookie名字匹配的Cookie名字匹配的Cookie,此时,id属性定义了一个Cookie数组类型的变量,而不是单个Cookie类型的变量。例如:
<bean:cookie id="myCookie" name="tags/cookiedemo" value="ddddddddd"/>
<bean:write name="myCookie" property="name"/>
<bean:write name="myCookie" property="value"/>
<%
String names=myCookie.getName();
String value=myCookie.getValue();
out.println("this cookie name is "+names+",value is "+value+"");
%>
2.<bean:header>标签
该标签用于检索HTTP请求中的Header信息。如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean。如果指定了multiple属性则依据刚取回的值创建一个String[]类型的数组。例如:
<logic:present header="User-Agent">
<!-- 其它标签通过绑定到page作用域中的属性使用该值 -->
您的浏览器是<bean:header id="userAgent" name="User-Agent"/>
<bean:write name="userAgent" /><br/>
<!-- JSP脚本通过scripting变量使用该值 -->
<%
out.println("您的浏览器是"+userAgent+"。<br/>");
%>
</logic:present>
<%
String id = "userAgent";
String name = "User-Agent";
String value = ((HttpServletRequest)pageContext.getRequest()).getHeader(name);
out.println("============="+value);
%>
3.<bean:parameter>标签
该标签用于检索HTTP请求参数,具有以下属性:
id属性:定义一个java.lang.String类型的变量,这个变量存放在page范围内。
name属性:指定请求参数名。
value属性:请求指定参数的默认值
如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean。如果指定了multiple属性则依据刚取回的值创建一个String[]类型的数组。例如:
<html:link page="/parameter2.jsptestString=this+is+a+test&testInt=123456">
请求参数
</html:link>
<bean:parameter id="test1" name="testString" value="" />
The first test is: <bean:write name="test1" />
<bean:parameter id="test2" name="testInt" value=""/>
The second test is:<bean:write name="test2"/>
4.<bean:page>标签
该标签用于检索获取JSP隐含对象,如session、request和response等,具有以下属性:
id属性:定义了一个引用隐含对象的变量,这个变量存放在page范围
property属性:指定隐含对象的名字,可选值包括application,config,request,response,session 例如:
<bean:page id="mySession" property="session"/>
<%
out.println("Session time:"+mySession.getCreationTime());
%>
<bean:message>:显示Resource Bundle中的消息。
<bean:resource>;把Web资源装载到一个JavaBean中。
<bean:struts>;访问Struts的内在配置对象。
<bean:include>;包含一个web资源。
<bean:write name="value" filter="false"/><br/>
out.println(value);
%>
message2=this is a test too!
该标签用于检索、获得Web资源内容,如:网页的源代码。该标签的属性有:id有其他标签一样,name指定Web资源的路径,input如果没有设置,则id属性默认为一个字符类型对象,如果给input赋值yes,则id属性所定义的对象为java.IputStream。例如:
<bean:write name="indexpage" filter="false"/>
该标签用于获取Struts框架内的一些对象,如AationForm和ActionForward等。。<bean:struts>标签的id属性定义一个page范围的变量,用来引用Struts框架的内在对象,必需设置formbean,forward,mapping属性中的一个属性,来指定被引用的Struts内在对象。
name:<bean:write name="listFormBean" property="name"/><br/>
type:<bean:write name="listFormBean" property="type"/><br/>
dynamic:<bean:write name="listFormBean" property="dynamic"/><br/>
<bean:write>: 显示JavaBean属性的内容。
<bean:size>: 获得集合对象或数组对象的长度。
该标签用于定义一个变量,id属性指定变量的名字,toScope属性指定这个变量的存放范围,如果没有设置,则这个变量存放在page范围内,给id属性定义的变量赋值有三种方式:
第一种是,通过value属性直接赋值;
第二种是,通过name和porperty共同指定一个变量来给id所定义的变量,name——bean,porperty——属性;
第三种是,通过type属性和name联合指定id所定义的变量类型,type——id定义变量的完整类型,name——存在的javaBean。例如:
<bean:define id="string" value="this is a test"/>
<bean:write name="string"/>--%>
Date d = new Date();
pageContext.setAttribute("currDate",d);
%>
<bean:define id="milliseconds" name="currDate" property="time"/>
该标签用于获取集合或者数组的长度。
id属性定义一个Integer类型的变量,那么属性指定已经存在的Map,Collection或数组变量,id属性定义的变量值为Map,collection或数组的长度。
name属性为对象名字。例如:
<%
ArrayList testlist=new ArrayList();
testlist.add(new Integer(1));
testlist.add(new Integer(2));
testlist.add(new Integer(3));
pageContext.setAttribute("listforcount",testlist);
%>
<bean:size id="size" name="listforcount"/>
长度为:<bean:write name="size"/>
该标签用于输出各种类型的对象,有点类似与System.out.println()。例如:
<%
String a = "string for test";
pageContext.setAttribute("test",a);
%>
<bean:write name="test"/>
- 标签logic的示例用法。
- LogicCompare.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <%@ page import="logictaglibs.SomeBean" %>
- <html:html>
- <head>
- <title>Logic Compare sample code</title>
- </head>
- <body bgcolor="white">
- <h3>Logic Compare sample code</h3>
- <p>This page provides examples of the following Struts LOGIC tags:<br>
- <ul>
- <li><logic:equal></li>
- <li><logic:lessEqual></li>
- <li><logic:lessThan></li>
- <li><logic:greaterEqual></li>
- <li><logic:greaterThan></li>
- <li><logic:notEqual></li>
- </ul>
- <%
- Cookie c = new Cookie("username", "Linda");
- c.setComment("A test cookie");
- c.setMaxAge(3600); //60 seconds times 60 minutes
- response.addCookie(c);
- %>
- <logic:equal cookie="username" value="Linda" >
- UserName in Cookie is Linda <p>
- </logic:equal>
- <logic:equal header="Accept-Language" value="zh-cn" >
- Client?¡¥s language is: zh-cn. <p>
- </logic:equal>
- <logic:greaterThan parameter="arg1" value="100" >
- The first request parameter is greater than 100 <p>
- </logic:greaterThan >
- <%
- request.setAttribute("intBean",new Integer(100));
- %>
- <logic:equal name="intBean" value="100" >
- The value of intBean is "100".<p>
- </logic:equal >
- <%
- SomeBean bean=new SomeBean();
- bean.setName("Linda");
- request.setAttribute("someBean",bean);
- %>
- <logic:notEqual name="someBean" property="name" value="Tom" >
- The name of someBean is not "Tom" <p>
- </logic:notEqual >
- <% request.setAttribute("number","100"); %>
- <logic:equal name="number" value="100.0" >
- "100" equals "100.0" <p>
- </logic:equal >
- <logic:lessThan name="number" value="100.0a" >
- "100" is less than "100.0a" <p>
- </logic:lessThan >
- </body>
- </html:html>
- LogicForward.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <html:html>
- <head>
- <title>Logic Forward sample code</title>
- </head>
- <body bgcolor="white">
- <logic:forward name="index"/>
- </body>
- </html:html>
- LogicIterate.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <%@ page import="java.util.HashMap" %>
- <%@ page import="java.util.Vector" %>
- <html:html>
- <head>
- <title>Logic Iterate sample code</title>
- </head>
- <body bgcolor="white">
- <h3>Logic Iterate sample code</h3>
- <p>This page provides examples of the following Struts LOGIC tags:<br>
- <ul>
- <li><logic:iterate></li>
- </ul>
- <table border="1">
- <tr>
- <td>
- <%--
- Variables used on this page
- --%>
- <%
- HashMap months = new HashMap();
- months.put("Jan.", "January");
- months.put("Feb.", "February");
- months.put("Mar.", "March");
- request.setAttribute("months", months);
- %>
- <%--
- The following section shows iterate.
- --%>
- <logic:iterate id="element" indexId="ind" name="months">
- <bean:write name="ind"/>.
- <bean:write name="element" property="key"/>:
- <bean:write name="element" property="value"/><BR>
- </logic:iterate><P>
- </td>
- <td>
- <%
- HashMap h = new HashMap();
- String vegetables[] = {"pepper", "cucumber"};
- String fruits[] = {"apple","orange","banana","cherry","watermelon"};
- String flowers[] = {"chrysanthemum","rose"};
- String trees[]={"willow"};
- h.put("Vegetables", vegetables);
- h.put("Fruits", fruits);
- h.put("Flowers", flowers);
- h.put("Trees",trees);
- request.setAttribute("catalog", h);
- %>
- <%--
- The following section shows iterate.
- --%>
- <logic:iterate id="element" indexId="ind" name="catalog">
- <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
- <logic:iterate id="elementValue" name="element" property="value" >
- -----<bean:write name="elementValue"/><BR>
- </logic:iterate>
- </logic:iterate><P>
- </td>
- <td>
- <%
- Vector animals=new Vector();
- animals.addElement("Dog");
- animals.addElement("Cat");
- animals.addElement("Bird");
- animals.addElement("Chick");
- request.setAttribute("Animals", animals);
- %>
- <logic:iterate id="element" name="Animals">
- <bean:write name="element"/><BR>
- </logic:iterate><p>
- </td>
- <td>
- <logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
- <bean:write name="index"/>.<bean:write name="element"/><BR>
- </logic:iterate><p>
- </td>
- <td>
- <logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
- <bean:write name="header"/><BR>
- </logic:iterate>
- </td>
- </tr>
- <table>
- </body>
- </html:html>
- LogicMatch.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <%@ page import="java.util.Enumeration" %>
- <html:html>
- <head>
- <title>Logic Match sample code</title>
- </head>
- <body bgcolor="white">
- <h3>Logic Match sample code</h3>
- <p>This page provides examples of the following Struts LOGIC tags:<br>
- <ul>
- <li><logic:match></li>
- <li><logic:notMatch></li>
- </ul>
- <%--
- Variables used on this page
- --%>
- <%
- request.setAttribute("authorName", "LindaSun");
- %>
- <%--
- The following section shows match and notMatch.
- --%>
- <logic:match name="authorName" scope="request" value="Linda">
- <bean:write name="authorName"/> has the string 'Sun' in it.
- </logic:match>
- <logic:notMatch name="authorName" scope="request" value="Linda">
- <bean:write name="authorName"/> doesn't have the string 'Linda' in it.
- </logic:notMatch>
- <BR>
- <logic:match name="authorName" scope="request" value="Linda" location="start">
- <bean:write name="authorName"/> starts with the string 'Linda'.
- </logic:match>
- <logic:notMatch name="authorName" scope="request" value="Linda" location="start">
- <bean:write name="authorName"/> doesn't start with the string 'Linda'.
- </logic:notMatch>
- <BR>
- <logic:match header="user-agent" value="Windows">
- You're running Windows
- </logic:match>
- <logic:notMatch header="user-agent" value="Windows">
- You're not running Windows
- </logic:notMatch>
- <BR>
- </body>
- </html:html>
- LogicPresence.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <%@ page import="java.util.HashMap" %>
- <%@ page import="org.apache.struts.Globals" %>
- <%@ page import="org.apache.struts.action.ActionMessage" %>
- <%@ page import="org.apache.struts.action.ActionErrors" %>
- <html:html>
- <head>
- <title>Logic Presence sample code</title>
- </head>
- <body bgcolor="white">
- <h3>Logic Presence sample code</h3>
- <p>This page provides examples of the following Struts LOGIC tags:<br>
- <ul>
- <li><logic:empty></li>
- <li><logic:messagesPresent></li>
- <li><logic:messagesNotPresent></li>
- <li><logic:notEmpty></li>
- <li><logic:notPresent></li>
- <li><logic:present></li>
- </ul>
- <%--
- Variables used on this page
- --%>
- <%
- ActionErrors errors = new ActionErrors();
- errors.add("totallylost", new ActionMessage("application.totally.lost"));
- request.setAttribute(Globals.ERROR_KEY, errors);
- request.setAttribute("myerrors", errors);
- request.setAttribute("emptyString", "");
- %>
- <%--
- The following section shows empty and notEmpty.
- --%>
- <logic:empty name="emptyString">
- The variable named emptyString is empty!<P>
- </logic:empty>
- <logic:notEmpty name="emptyString">
- The variable named emptyString is not empty!<P>
- </logic:notEmpty>
- <P>
- <%--
- The following section shows present and notPresent.
- --%>
- <logic:present name="noSuchBean" property="noSuchProperty">
- Both noSuchBean and noSuchProperty exist!
- </logic:present>
- <logic:notPresent name="noSuchBean" property="noSuchProperty">
- Either noSuchBean or noSuchProperty does not exist!
- </logic:notPresent>
- <P>
- <logic:present name="emptyString" >
- There is a JavaBean named "emptyString". <p>
- </logic:present>
- <logic:notPresent name="emptyString" property="noSuchProperty">
- EmptyString doesn't have such a property named "noSuchProperty".
- </logic:notPresent>
- <P>
- <logic:present header="user-agent">
- Yep, we got a user-agent header.
- </logic:present>
- <logic:notPresent header="user-agent">
- No, user-agent header does not exist.
- </logic:notPresent>
- <P>
- <%--
- The following section shows messagesPresent and messagesNotPresent.
- --%>
- <logic:messagesPresent>
- Yes, there are errors.
- </logic:messagesPresent><P>
- <logic:messagesPresent name="myerrors">
- Yes, there are errors in myerrors collection.
- </logic:messagesPresent><P>
- <logic:messagesNotPresent message="true" >
- There are no normal messages.
- </logic:messagesNotPresent><P>
- <logic:messagesNotPresent property="noSuchError">
- There is no error named "SuchError".
- </logic:messagesNotPresent><P>
- <logic:messagesPresent property="totallylost">
- There is an error named "totallylost".
- </logic:messagesPresent>
- </body>
- </html:html>
- LogicRedirect.jsp
- <%@ page language="java" %>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- <html:html>
- <head>
- <title>Logic Redirect sample code</title>
- </head>
- <body bgcolor="white">
- <logic:redirect href="http://www.apache.org"/>
- </body>
- </html:html>
- package logictaglibs;
- public class SomeBean{
- public SomeBean(){}
- private String name;
- public void setName(String name){this.name=name;}
- public String getName(){return this.name;}
- }