Cookbook-struts1.3.8案例分析-Logic Tags
Cookbook-struts1.3.8案例分析-Logic Tags
l Logic Tags
入口Action配置
<action path="/prepareLogic" type="examples.logic.PrepareLogicAction"> <forward name="success" path="/jsp/logic/Logic.jsp" /> </action> |
SuccessAction,繼承自Action,execute方法如下
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //往Request范圍內放置一些變量 TestBean bean = new TestBean(); request.setAttribute("testBean", bean); ArrayList items = new ArrayList(); request.setAttribute("items", items); request.setAttribute("intValue", new Integer(7)); request.setAttribute("stringValue", "Hello, world!"); /* Collection of custom beans */ ArrayList books = new ArrayList(); books.add(new BookBean("0596003285", "Programming Jakarta Struts")); books.add(new BookBean("1930110502", "Struts in Action")); books.add(new BookBean("1861007817", "Professional Struts Applications")); books.add(new BookBean("0672324725", "Struts Kick Start")); books.add(new BookBean("0471213020", "Mastering Jakarta Struts")); books.add(new BookBean("1558608621", "The Struts Framework")); books.add(new BookBean("0971661901", "Struts Fast Track")); request.setAttribute("books", books); ActionErrors errors = new ActionErrors(); errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.detail", "This is a global error #1")); errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.detail", "This is a global error #2")); errors.add("test", new ActionMessage("errors.detail", "This is a test error")); ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.detail", "This is global message #1")); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.detail", "This is global message #2")); messages.add("test", new ActionMessage("message.example.simple")); //設置Message和Error saveMessages(request, messages); saveErrors(request, errors); // Forward to the form return mapping.findForward("success"); } |
JSP文件對Bean標簽的處理
對cookie的判斷,顯示與否 <logic:present cookie="JSESSIONID"> <p>Session cookie is present.</p> </logic:present> <logic:notPresent cookie="UNKNOWN"> <p>UNKNOWN cookie is not present.</p> </logic:notPresent> 參數判斷,顯示與否 <logic:present parameter="param"> <bean:parameter name="param" id="test"/> <p><bean:write name="test"/></p> </logic:present> <logic:notPresent parameter="param"> <p>Parameter 'param' not present. <html:link action="/prepareLogic?param=The parameter is present"> Redisplay page with parameter present. </html:link> </p> </logic:notPresent> Request范圍內存在判斷,顯示與否 <h3>Bean</h3> <logic:present name="testBean"> <p>'testBean' is present.</p> </logic:present> <logic:notPresent name="anotherTestBean"> <p>'anotherTestBean' is not present.</p> </logic:notPresent> 上下文變量的屬性存在判斷,顯示與否 <logic:present name="testBean" property="fred"> <p>'fred' property is present on 'testBean'</p> </logic:present> <logic:notPresent name="testBean" property="fred"> <p>'fred' property is not present on 'testBean'</p> </logic:notPresent> <logic:present name="testBean" property="stringValue"> <p>'stringValue' property is present on 'testBean'</p> </logic:present> 集合是否為空判斷,是否顯示 <logic:present name="items"> <p>'items' was found.</p> </logic:present> <logic:empty name="items"> <p>'items' is empty</p> </logic:empty> <logic:notEmpty name="items"> <p>'items' is not empty</p> <%-- <bean:size collection="items" id="itemsSize"/> <p>Items has <bean:write name="itemsSize" /> items.</p> --%> </logic:notEmpty> 大小比較 <logic:equal name="intValue" value="7"> <p>intValue == 7</p> </logic:equal> <logic:greaterEqual name="intValue" value="7"> <p>intValue >= 7</p> </logic:greaterEqual> <logic:greaterEqual name="intValue" value="6"> <p>intValue >= 6</p> </logic:greaterEqual> <logic:greaterThan name="intValue" value="6"> <p>intValue > 6</p> </logic:greaterThan> <logic:lessEqual name="intValue" value="7"> <p>intValue <= 7</p> </logic:lessEqual> <logic:lessEqual name="intValue" value="8"> <p>intValue <= 8</p> </logic:lessEqual> <logic:lessThan name="intValue" value="8"> <p>intValue < 8</p> </logic:lessThan> 模糊匹配 <logic:match name="stringValue" value="world"> <p>stringValue matches 'world'</p> </logic:match> <logic:notMatch name="stringValue" value="earth"> <p>stringValue does not match 'earth'</p> </logic:notMatch> <logic:notMatch name="stringValue" value="world"> <p>stringValue does not match 'world'</p> </logic:notMatch> 迭代集合 <logic:iterate name="books" id="book" indexId="index"> <li><bean:write name="book" property="title"/></li> </logic:iterate> <logic:iterate name="books" id="book" offset="2" length="3"> <li><bean:write name="book" property="title"/></li> </logic:iterate> 顯示錯誤集合 <logic:messagesPresent> <p>Global errors:</p> <ul> <html:messages id="error" property="<%=ActionErrors.GLOBAL_MESSAGE%>"> <li><bean:write name="error"/></li> </html:messages> </ul> <p>Errors for 'test':</p> <ul> <html:messages id="error" property="test"> <li><bean:write name="error"/></li> </html:messages> </ul> </logic:messagesPresent> <logic:messagesNotPresent> <p>There are no errors</p> </logic:messagesNotPresent> 顯示消息 <logic:messagesPresent message="true"> <ul> <html:messages id="msg" message="true" property="<%=ActionMessages.GLOBAL_MESSAGE%>"> <li><bean:write name="msg"/></li> </html:messages> </ul> <p>Messages for 'test':</p> <ul> <html:messages id="msg" property="test" message="true"> <li><bean:write name="msg"/></li> </html:messages> </ul> </logic:messagesPresent> <logic:messagesNotPresent message="true"> <p>There are no messages</p> </logic:messagesNotPresent> |
posted on 2008-07-08 02:19 MingIsMe 閱讀(161) 評論(0) 編輯 收藏 所屬分類: 16 案例分析