Cookbook-struts1.3.8案例分析-Bean Tags
Cookbook-struts1.3.8案例分析-Bean Tags
l Bean Tags
入口Action配置
<action path="/prepareBean" type="examples.bean.PrepareBeanAction"> <forward name="success" path="/jsp/bean/Bean.jsp" /> </action> |
SuccessAction,繼承自Action,execute方法如下
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Set-up an example bean ExampleBean example = new ExampleBean(); example.getList().add("List entry #1"); example.getList().add("List entry #2"); example.getList().add("List entry #3"); example.getList().add("List entry #4"); // Add a nested bean example.setNested(new NestedBean()); // Place the example bean in the request scope request.setAttribute("example", example); // Forward to the test page return mapping.findForward("success"); } |
ExampleBean的定義
/**Abooleanvalue*/ privatebooleanbooleanValue = false; /**Adoublevalue*/ privatedoubledoubleValue = 45213.451; /**Afloatvalue*/ privatefloatfloatValue = -123.582F; /**Anintegervalue*/ privateintintValue = 256; /**Alongintegervalue*/ privatelonglongValue = 1321546L; /**Ashortintegervalue*/ privateshortshortValue = 257; /**Astringvalue*/ private String stringValue = "Hello, world!"; /**AdateValuevalue*/ private java.util.Date dateValue = new java.util.Date(); /**Alist*/ private List list = new ArrayList(); /**Anarray*/ private String[] array = { "Red", "Green", "Blue", "Black", "Orange" }; /**Anestedbean*/ private NestedBean nested = null; /**HTMLformattedmarkup*/ private String html = "<p>This is a <strong>simple</strong> example of " + "<em>HTML</em> formatted text.</p>"; |
NestedBean的定義
/**Astringvalue*/ private String stringValue = "This is a string from NestedBean"; |
JSP文件對Bean標(biāo)簽的處理
輸出request范圍內(nèi)example這個bean的原始屬性 <bean:write name="example" property="booleanValue" /> 輸出request范圍內(nèi)example這個bean的String屬性 <bean:write name="example" property="stringValue" /> 輸出request范圍內(nèi)example這個bean的date屬性 <bean:write name="example" property="dateValue" /> 格式化數(shù)字 <bean:write name="example" property="doubleValue" format="$0,000.00"/> 格式化數(shù)字 <bean:write name="example" property="floatValue" formatKey="format.currency"/> 格式化日期 <bean:write name="example" property="dateValue" format="MMM d yyyy '@' HH:mm"/> 格式化日期 <bean:write name="example" property="dateValue" formatKey="format.date"/> 輸出列表元素 <bean:write name="example" property="list[2]" /> 輸出數(shù)組元素 <bean:write name="example" property="array[3]" /> 輸出嵌套的string值 <bean:write name="example" property="nested.stringValue"/> 過濾html里的<&>等符號 <bean:write name="example" property="html"/> 不過濾html符號 <bean:write name="example" property="html" filter="false"/> 定義和顯示bean值 <bean:define id="define1" value="This is a test string"/> <bean:write name="define1" /> 定義bean值,值取自request范圍內(nèi)的變量 <bean:define name="example" property="nested" id="define2" /> <bean:write name="define2" property="stringValue" /> 取得并顯示request header的值 <bean:header name="Accept-Encoding" id="encodings" /> <bean:write name="encodings" /> 從資源文件獲取值 <bean:message key="message.example.simple"/> <bean:message key="message.example.replaceable" arg0="a slightly more complex" arg1="2 replaceable" /> 從parameter里獲取值,賦給變量,然后顯示該變量 <bean:parameter name="param1" id="p1" value=""/> <bean:write name="p1" /> <bean:parameter name="param3" id="p3" value="DEFAULT"/> <bean:write name="p3" /> 取得list的大小,并顯示他 <bean:size name="example" property="list" id="listSize" /> <bean:write name="listSize" /> 定義cookie值,并顯示其屬性 <bean:cookie id="sess" name="JSESSIONID" value="JSESSIONID-IS-UNDEFINED"/> <bean:write name="sess" property="value"/> |
posted on 2008-07-08 02:13 MingIsMe 閱讀(116) 評論(0) 編輯 收藏 所屬分類: 16 案例分析