Struts標簽之Bean標簽
Bean標簽:主要與JavaBean有關
回顧:
<jsp:useBean id=”對象名稱” scope=”保存范圍(四種屬性范圍)” class=”包.類”>
<jsp:setProperty name=”id(jsp:useBean中使用)” property=”屬性” />
<jsp:getPorperty name=”id” property=”屬性” />
只要是通過標簽設置的對象,則自動將對象保存在一個固定的范圍之中,同scope指定
一般對象都使用”id”進行表示
如果要使用對象,在標簽通過”name”屬性完成
1、 Bean標簽
在Struts中提供了一系列的與JavaBean有關的操作標簽,稱為Bean標簽
1.1、<bean:define>標簽
定義或復制一個對象
定義對象一般為String類型
復制對象->迭代標簽
<bean:define id=”str” value=”Hello World” />
<h1>${str}</h1>
如果可以通過EL進行訪問,則表示”str”保存在了四種屬性范圍之中,等同于下面的語句:
String str = “Hello World”
pageContext.setAttribute(“str”,str);
如果不使用EL該如何輸出呢?通過Struts中的<bean:write>進行打印
<bean:write name=”str” />
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>BeanDemo01.jsp</title>
</head>
<body>
<bean:define id="str" value="Hello World"></bean:define>
<h1>${str}</h1>
<h2><bean:write name="str"/></h2>
</body>
</html:html>
強調: id:就表示一個存放在四種屬性范圍之中的名稱
name:表示使用一個存放在四種屬性范圍中的對象
1.2、<bean:size>標簽
求出長度:數組、Collection、Map
標簽肯定數據存放在四種屬性范圍之中
輸出Map長度
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>BeanDemo02.jsp</title>
</head>
<body>
<%
Map m = new HashMap();
m.put("one","1");
m.put("two","2");
m.put("three","3");
//將Map對象保存在四種屬性范圍之中
request.setAttribute("map",m);
%>
<bean:size id="len" name="map" scope="request"/>
<h1>長度是:${len }</h1>
</body>
</html:html>
輸出Collection長度
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>BeanDemo3.jsp</title>
</head>
<body>
<%
Collection co = new ArrayList();
co.add("one");
co.add("two");
co.add("three");
//將co對象保存在四種屬性范圍之中
request.setAttribute("coll",co);
%>
<bean:size id="len" name="coll" scope="request"/>
<h1>長度是:${len }</h1>
</body>
</html:html>
1.3、<bean:write>標簽
打印對象,也可以打印對象中的屬性
在JSP2.0中可以使用EL代替Struts中的<bean:write>標簽
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.tanm.struts.*" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>BeanDemo04.jsp</title>
</head>
<body>
<%
SimpleBean simple = new SimpleBean();
simple.setName("張三");
simple.setPassword("15202");
//將此對象保存在屬性范圍之中
request.setAttribute("simple",simple);
%>
<h1>使用EL:</h1>
<h2>姓名: ${simple.name }</h2>
<h2>密碼: ${simple.password }</h2>
<hr>
<h1>使用Bean標簽:</h1>
<h2>姓名: <bean:write name="simple" property="name" scope="request"/></h2>
<h2>密碼: <bean:write name="simple" property="password" scope="request"/></h2>
</body>
</html:html>
1.4、<bean:message>標簽
Struts國際化
Struts中存在一個資源文件(用于保存所有的錯誤消息)
在<bean:message>標簽中提供了一個占位功能,在輸出的文件中占著一位,這一位的數據等待標簽填寫
Welcome="u6bb22"u8fce{0}"u5159"u4e34
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>BeanDemo04.jsp</title>
</head>
<body>
<bean:message key="welcome" arg0="譚明"/>
</body>
</html:html>
posted on 2007-10-17 16:31
譚明 閱讀(1004)
評論(0) 編輯 收藏 所屬分類:
Struts