hello<br>
<b>test</b>
<%=1%>
struts1.x-bean标签库实例
使用前请在WebRoot下新建一个test.jsp文件,内容随意,例如
在配置文件中新建一个test.hello键值,内容随意,如
test.hello =hello~
配置struts-config.xml,在global-forwards里建立一个
<forward name="go" path="bean.jsp"></forward>
再在action-mapping里增加一个action:
<action path="/hello" type="com.yourcompany.struts.action.HelloAction" ></action>
建立两个bean:
【User.java】
-
package com.yourcompany.struts.bean;
-
-
public class User {
-
-
private String name;
-
-
private String sex;
-
-
private String address;
-
-
private Company company;
-
-
return name;
-
}
-
-
this.name = name;
-
}
-
-
return sex;
-
}
-
-
this.sex = sex;
-
}
-
-
return address;
-
}
-
-
this.address = address;
-
}
-
-
public Company getCompany() {
-
return company;
-
}
-
-
public void setCompany(Company company) {
-
this.company = company;
-
}
-
-
}
【Company.java】
其他不管它,新建bean.jsp,复制以下内容:
-
<%@ page language="java" import="java.util.*,com.yourcompany.struts.bean.*" pageEncoding="GBK"%>
-
-
<%@ 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" %>
-
-
-
<html:html lang="true">
-
<head>
-
<html:base />
-
<title>taglib: Bean</title>
-
</head>
-
-
<body>
-
<h2>1. 用于访问http请求或jsp隐含对象的标签</h2>
-
-
<h3>bean:cookie</h3>
-
<ul>
-
<%Cookie cookie = new Cookie("userName","YOYO");
-
response.addCookie(cookie); %>
-
<bean:cookie id="myCookie" name="userName" value="no" />
-
Cookie名稱: ${myCookie.name}<br>
-
Cookie的值: ${myCookie.value}<br>
-
</ul>
-
-
<h3>bean:parameter</h3>
-
<ul>
-
<bean:parameter id="paramValue" name="msg" value="no message" />
-
傳遞來的msg值: ${paramValue}<br>
-
</ul>
-
-
<h3>bean:header</h3>
-
<ul>
-
<bean:header id="userLang" name="Accept-Language" />
-
用戶使用的語言:${userLang}<br>
-
<bean:header id="browser" name="User-Agent" />
-
瀏覽器:${browser}
-
</ul>
-
-
<h3>bean:page</h3>
-
<ul>
-
<bean:page id="req" property="request" />
-
服務器地址:${req.localAddr}<br>
-
服務器使用的端口:${req.localPort}<br>
-
站點路徑:<bean:write name="req" property="contextPath" />
-
</ul>
-
-
<hr>
-
-
<h2>2. 用于定义或输出javabean的bean标签</h2>
-
<%
-
User user = new User();
-
user.setName("YOYO");
-
user.setAddress("cuc.fjnu");
-
user.setSex("female");
-
Company company = new Company();
-
company.setName("SPOTO");
-
company.setAddress("Fuzhou");
-
user.setCompany(company);
-
-
pageContext.setAttribute("userInfo",user);
-
%>
-
-
<h3>bean:define</h3>
-
<ul>
-
<li>定義一個新的bean</li><br>
-
<bean:define id="newstr" value="hello,world!"></bean:define>
-
${newstr}<br>
-
<br>
-
-
<li>根據已有的bean,創建新的bean</li><br>
-
<bean:define id="newUserInfo" name="userInfo" scope="page" type="com.yourcompany.struts.bean.User"></bean:define>
-
姓名:${newUserInfo.name}<br>
-
性別:${newUserInfo.sex}<br>
-
<br>
-
-
<li>根據已有的bean的屬性創建新的bean</li><br>
-
<bean:define id="userAdd" name="userInfo" property="address"></bean:define>
-
地址:${userAdd}<br>
-
<br>
-
-
</ul>
-
-
<h3>bean:write</h3>
-
<ul>
-
<li>輸出數字</li><br>
-
米有格式:<bean:write name="number" scope="request"/><br>
-
使用"###,###.####"格式輸出:<bean:write name="number" scope="request" format="###,###.####" /><br>
-
使用"000,000.0000"格式輸出:<bean:write name="number" scope="request" format="000,000.0000" /><br>
-
<br>
-
-
<li>輸出日期</li><br>
-
默認輸出:<bean:write name="today" /><br>
-
用"yyyy-MM-dd"格式輸出:<bean:write name="today" format="yyyy-MM-dd" /><br>
-
<br>
-
-
<li>輸出HTML字符串</li><br>
-
<% pageContext.setAttribute("htmlstr","<font color=red>redfont</font>"); %>
-
默認輸出:<bean:write name="htmlstr" /><br>
-
filter為true時的輸出:<bean:write name="htmlstr" filter="true" /><br>
-
filter為false時的輸出:<bean:write name="htmlstr" filter="false" /><br>
-
<br>
-
-
<li>輸出bean</li><br>
-
姓名:<bean:write name="userInfo" property="name" /><br>
-
地址:<bean:write name="userInfo" property="address" /><br>
-
性別:<bean:write name="userInfo" property="sex" /><br>
-
公司:<bean:write name="userInfo" property="company.name" /><br>
-
公司地址:<bean:write name="userInfo" property="company.address" /><br>
-
<br>
-
-
<li>使用ignore屬性:當對象為null時不會報錯</li><br>
-
<bean:write name="novalue" ignore="true" />
-
-
</ul>
-
-
<h3>bean:size</h3>
-
<%
-
list.add("hello");
-
list.add("test");
-
pageContext.setAttribute("strlist",list);
-
%>
-
<bean:size id="length" name="strlist" scope="page" />
-
<ul>獲得list的長度為${length}</ul>
-
-
<hr>
-
-
<h2>3. 用于访问web应用资源的bean标签</h2>
-
-
<h3>bean:resource</h3>
-
<ul>
-
<!-- 自己寫一個test.jsp -->
-
<li>默認將它讀取為字符串</li><br>
-
<bean:resource id="inputResource" name="test.jsp" />
-
<bean:write name="inputResource" /><br>
-
<br>
-
-
<li>作為InputStream讀</li><br>
-
<bean:resource id="inputstream" name="test.jsp" input="yes" />
-
<bean:write name="inputstream" /><br>
-
<br>
-
-
</ul>
-
-
<h3>bean:struts</h3>
-
<ul>
-
<!-- 在struts-config.xml里配一個global-forward,name為go -->
-
<li>forward</li><br>
-
<bean:struts forward="go" id="fwd_go"/>
-
forward=go時的path:<bean:write name="fwd_go" property="path"/><br>
-
<br>
-
-
<!-- 在struts-config.xml里配一個action-mapping,path為/hello -->
-
<li>mapping</li><br>
-
<bean:struts mapping="/hello" id="map" />
-
/hello.do時的action:<bean:write name="map" property="type" /><br>
-
<br>
-
-
</ul>
-
-
<h3>bean:include</h3>
-
<ul>
-
<!-- 自己寫一個test.jsp -->
-
<bean:include id="includefile" page="/test.jsp" />
-
<bean:write name="includefile" />
-
</ul>
-
-
<h3>bean:message</h3>
-
<ul>
-
<!-- 要用時請先在配置文件里加上test.hello鍵值 -->
-
<bean:message key="test.hello" />
-
</ul>
-
-
</body>
-
</html:html>
详细可见内容注释。部署好后即可运行。