JPetStore5展示層分析
l Web.xml文件
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
表示action的擴展名為shtml
<security-constraint>
<web-resource-collection>
<web-resource-name>
Restrict access to JSP pages
</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>
With no roles defined, no access granted
</description>
</auth-constraint>
</security-constraint>
限制任何人直接訪問jsp文件
l 系統入口
Index.html文件
含有下列超連接:<p><a href="shop/index.shtml">Enter the Store</a></p>
<action path="/shop/index" type="org.apache.struts.beanaction.BeanAction"
name="catalogBean" parameter="*" validate="false">
<forward name="success" path="/catalog/Main.jsp"/>
</action>
轉向/catalog/Main.jsp文件
注意:org.apache.struts.beanaction.BeanAction
這個類是被封裝到beanaction.jar包的,由Clinton寫的。
這個類與BaseBean的子類一起合作,把action和formbean整合在formbean一個類里。
并且action調用的方法就是formbean里定義的方法,依據actionmapping里的parameter參數值確定調用哪個方法。如果沒有parameter參數,就調用名叫action配置值的函數。
靠setMessage報告消息給頁面,只能支持一種語言。
因為沒有action類,所以不能調用action的saveMessages和saveErrors方法。
缺點:不能防止重復提交action,即對Token的操作。
l ActionBean基類
此基類繼承了ValidatorActionForm,所以,可以使用validator框架。
l Action配置
<action path="/shop/newAccountForm" type="org.apache.struts.beanaction.BeanAction"
name="accountBean" scope="session" parameter="*"
validate="false">
<forward name="success" path="/account/NewAccountForm.jsp"/>
</action>
由上面可以看到path的值是隨便定義的,不必局限于/xxx一層。
l 界面重用
<html:form action="/shop/newAccount.shtml" method="post">
<html:hidden name="accountBean" property="validation" value="new"/>
<h3>User Information</h3>
<table>
<tr>
<td>User ID:</td><td><html:text name="accountBean" property="username"/></td>
</tr><tr>
<td>New password:</td><td><html:password name="accountBean" property="password"/></td>
</tr><tr>
<td>Repeat password:</td><td><html:password name="accountBean" property="repeatedPassword"/></td>
</tr>
</table>
<%@ include file="IncludeAccountFields.jsp" %>
<input type="submit" name="submit" value="Create Account"/>
</html:form>
l Get參數綁定actionBean
<html:link page="/shop/viewCategory.shtml?categoryId=FISH">
categoryID綁定到Category 類的categoryId屬性
l ActionMapping的scope屬性
屬性值大多是不是session,為什么不是request?
因為有下面的代碼
<logic:present name="accountBean" scope="session">
<logic:equal name="accountBean" property="authenticated" value="true" scope="session">
<html:link page="/shop/signoff.shtml">Sign Out</html:link>
<img align="middle" src="../images/separator.gif"/>
<html:link page="/shop/editAccountForm.shtml">My Account</html:link>
</logic:equal>
</logic:present>