標籤使用方法詳解

Struts中的<bean:write name="neeke" scope="request" />標籤相當於<%=request.getAttribute("neeke") %>,當然這裏也不一定是request,也可能是session等,其中neeke是屬性的名字。

首先我們來看一看action中的代碼:

package cn.ineeke.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class BeanWriteAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response)
            throws Exception {
             request.setAttribute("myblog", "<a href='http://www.ineeke.cn'>http://www.ineeke.cn</a>");
            return mapping.findForward("success");
     }
}

上面這段代碼的作用很簡單,就是向request中存值,接着跳轉到成功頁面。再來看看成功頁的代碼是什麼樣的。

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.    <head>
  6.      <title>Struts標籤BeanWrite學習</title>
  7.    </head>
  8.    <body>
  9.      <bean:write name="myblog" scope="request"/>
  10.    </body>
  11. </html>

在上面的代碼中,我們使用<bean:write>標籤從request中將myblog取出,並顯示在網頁上。
此時是按照文本格式直接輸出的,
而我們存放的時候是帶HTML代碼的,如果我們想讓它以超級鏈接的形式顯示在網頁中呢?

其實這很簡單,此標籤有一個filter屬性,其意在於是否以文本格式輸出,默認情況下其值爲true,所以我們只需將其改爲如下所示即可。
<bean:write name="myblog" scope="request" filter="false"/>

http://www.ineeke.cn/archives/StrutsBeanWriteFilter/

首先要想使用struts的標籤庫,則必須在jsp中指定所使用的標籤庫地uri,即標籤庫的標準。引入方法如下:
<%@ taglib prefix="bean" http://struts.apache.org/tags-bean">http://struts.apache.org/tags-bean"%>
  接下來就可使用該標籤庫中的標籤了,前綴是bean。我們這篇文章主要介紹write標籤,bean:write相當於<%=request.getAttribute("something")%> 其中something是屬性的名字。下面bean:write的詳細介紹如下:

bean:write常用的屬性有如下幾個: 
1。name,用來指定屬性的名字
2。filter,用來指定是否屏蔽到屬性值的HTML格式
3。property,用來指定name所代表的對象的屬性名字
4。format,用來指定顯示的時間,數字,日期等的格式

例子一:
某處設置了request.setAttribute("hello","hello world");
則在某個jsp頁面中,用struts的write標籤取出並顯示的方式如下:
<bean:write name="hello"/>,則頁面上顯示出hello world。

例子二:
某處設置了request.setAttribute("bj","<font color='red>歡迎你</font>");
則在某個jsp頁面中,用struts的write標籤取出並按紅色的方式顯示的方式如下:
<bean:write name="bj" filter="false"/>,則頁面上顯示出紅色的歡迎你。如果filter屬性默認爲true,如果不設置爲false的話,則那麼顯示出的內容就爲 <font color='red>歡迎你</font> 。

例子三:
某處設置了request.setAttribute("date",new Date());
則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示日期的方法如下:
<bean:write name=“date”/>,此爲默認的顯示方法,顯示出的時間爲:Fri Mar 28 15:04:21 CST 2008
<bean:write name=“date” format="yyyy-MM-dd HH:mm:ss"/>,此爲自己指定日期的顯示格式,顯示出的時間爲2008-3-28 15:04:21

例子四:
某處設置了request.setAttribute("n",“1223333.333”);
則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示數字的方法如下:
<bean:write name=“n”/>,此爲默認的顯示方法,顯示出的數字位1223333.333
<bean:write name=“n” format="###,###.####"/>,此爲自己指定數字的顯示格式,顯示出的時間爲1,223,333.333
如果希望小數點後的不足四位時,缺位補0,則應
<bean:write name=“n” format="###,###.0000"/>,此爲自己指定數字的顯示格式,顯示出的時間爲1,223,333.3330

例子五:
假如有User類和Groupe類,User類有屬性名字userName,年齡age,性別sex和所屬的Groupe,Groupe類有屬性組名groupeName,並均具有相應的get和set方法。
某處設置了request.setAttribute("user",new User("張三","20","男",new Groupe("三組")));
則在某個jsp頁面中,用struts的write標籤取出並按指定方式顯示結構體的方法如下:
用戶名:<input type="text" value="<bean:write name=“user” property="userName"/>">
年齡:<input type="text" value="<bean:write name=“user” property="age"/>">
性別:<input type="text" value="<bean:write name=“user” property="sex"/>">
組名:<input type="text" value="<bean:write name=“user” property="groupe.groupeName"/>">

(三)

下面的代碼片段示例了bean:write標籤輸出User-Agent:

<logic:present header="User-Agent">
    <bean:header id="header" name="User-Agent"/>
    <bean:write name="header"/>
</logic:present>

下面的代碼片段示例了bean:write標籤格式化輸出當前日期,其中now是在DataForm中定義的一個java.util.Date類型的域(值爲new Date()),format.date.standard是在資源文件中的一個鍵(format.date.standard=yyyy-MM-dd):

<bean:define id="date" name="dataForm" property="now"/>
<br/><bean:write name="date"/>
<br/><bean:write name="date" format="MM/dd/yyyy"/>
<br/><bean:write name="date" formatKey="format.date.standard"/>

上面代碼運行的結果爲:

Sun Jun 04 17:04:05 CST 2006
06/04/2006
2006-06-04

bean:write標籤將指定的bean的屬性值寫到當前的JspWriter中,並且可以對輸出進行格式化。


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