閱讀struts MailReader文檔筆記:
MailReader應(yīng)用程序基于struts 1.2.0開發(fā)。
1:主頁(yè)是index.jsp。由于struts的Action不能指定歡迎頁(yè)面
,而首次會(huì)從服務(wù)器配置的歡迎列表去查找出相應(yīng)的頁(yè)面返回給用戶,
那么我們?cè)趺磥?lái)用struts的actions而不是普通的jsp頁(yè)面返回給用戶呢,
一個(gè)解決方案是在一個(gè)頁(yè)面寫上要轉(zhuǎn)發(fā)到我們的歡迎頁(yè)面,代碼:
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect action="/Welcome"/>
在相應(yīng)的struts-config.xml配置文件加上
<!-- Display welcome page -->
<action path="/Welcome" forward="/welcome.jsp" />
但其它頁(yè)面還不能保證用戶不能訪問(wèn)到,我們?cè)趹?yīng)用當(dāng)中一般會(huì)把所有的
jsp頁(yè)面放到WEB-INF目錄下面,然后在struts-config.xml做一下映射就可以了,以保證用戶不能直接訪問(wèn)到。
2:<message-resources parameter="org.apache.struts.webapp.example.MessageResources" />
在同一個(gè)struts里面只能有一個(gè)默認(rèn)的存放本地化的消息文本(Resource Bundle)
那很我們指定多個(gè)的時(shí)候可以用它的一個(gè)屬性key指定
例如:
<message-resources parameter="org.apache.struts.webapp.example.AlternateMessageResources" key="alternate" />
那么我們?cè)陧?yè)面用的時(shí)候這樣出別
<bean:message key="key0"/>;
<bean:message key="key1" bundle="alternate"/>
3:<html:link>有兩個(gè)優(yōu)點(diǎn):
(1)允許在url中以多種方式包含請(qǐng)求。
(2)當(dāng)用戶關(guān)閉cookie時(shí),會(huì)自動(dòng)重寫url,把sessionid作為請(qǐng)求參數(shù)包含在url當(dāng)中,用于跟蹤用戶的session狀態(tài),而不像servlet,jsp還要
自己硬編碼實(shí)現(xiàn)
它有幾個(gè)重要的屬性:
*forward:指定全局轉(zhuǎn)發(fā)鏈接(只適用于(flobal-forwards>forward,而不能引用action forward子元素)
*href:指定完整的url鏈接(<html:link url="http//www.sina.com"/>)
*page:指定相對(duì)于當(dāng)前網(wǎng)頁(yè)的url(<html:link page="/test.do"/>
4:PlugIn(struts插件)
在struts-config.xml要加上相應(yīng)的描述語(yǔ)句
<plug-in className="org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn">
? <set-property property="pathname" value="/WEB-INF/database.xml"/>
</plug-in>
其中MemoryDatabaseplugIn是自己開發(fā)的一個(gè)插件,它必須org.paache.struts.action.PlugIn接口,包含兩個(gè)方法init,destroy
init在struts加載時(shí)自動(dòng)被調(diào)用,destroy當(dāng)應(yīng)用關(guān)閉時(shí)調(diào)用,可以放一些釋放資源的語(yǔ)句(如關(guān)閉數(shù)據(jù)庫(kù)連接的語(yǔ)句等)
并且這個(gè)里面還包含屬性pathname,也要相應(yīng)的get,set方法,以便在struts框架在加載插件時(shí),會(huì)自動(dòng)調(diào)用setPathname()方法,把
<set-property>子元素的pathname設(shè)置成MemoryDatabasePlugIn里對(duì)應(yīng)屬性的值value="/WEB-INF/database.xml"
還要注意就是<plug-in>必須位于其它配置元素后面,出現(xiàn)多個(gè)按順序加載
5:
<!-- Process a user logon -->
<action??? path="/SubmitLogon"
????????????????? type="org.apache.struts.webapp.example.LogonAction"
????????????????? name="LogonForm"
???????????????? scope="request"
???????????????? input="logon">
?<exception
?????????????????? key="expired.password"
????????????????? type="org.apache.struts.webapp.example.ExpiredPasswordException"
????????????????? path="/ExpiredPassword.do"/>
?????? </action>
scope推薦使用request,當(dāng)然也可以用session,一個(gè)ActionForm只對(duì)應(yīng)一次請(qǐng)求,不要越過(guò)request,
如果我們使用type="org.apache.struts.validator.DynaValidatorForm"
那么它會(huì)自動(dòng)創(chuàng)建一個(gè)ActionForms與之對(duì)應(yīng)
exception子元素,當(dāng)一個(gè)用戶登錄以后,有可能 "ExpiredPasswordException"(超時(shí)) 會(huì)拋出.
?如果發(fā)生了的話 Struts 會(huì)捕獲exception 并發(fā)送到 "ExpiredPassword" action.
6:
自己開發(fā)一個(gè)定制標(biāo)記<app:checkLogon/>用戶檢查用戶是否登錄
package org.apache.struts.webapp.example;
import ...
public final class CheckLogonTag extends TagSupport {
??? private String name = Constants.USER_KEY;
??? private static String LOGIN_PATH = "/Logon.do";
??? private String page = LOGIN_PATH;
??? public int doStartTag() throws JspException {
??? return (SKIP_BODY);
??? }
??? public int doEndTag() throws JspException {
??? ?boolean valid = false;
??? ?HttpSession session = pageContext.getSession();
??? ?if ((session != null) && (session.getAttribute(name) != null)) {
??? ???? valid = true;
??????? }
??????? if (valid) {
??????????? return (EVAL_PAGE);
??????? } else {
??????????? ModuleConfig config =
??????????????? (ModuleConfig) pageContext.getServletContext().getAttribute(
??????????????????? org.apache.struts.Globals.MODULE_KEY);
??????????????? try {
??????????????????? pageContext.forward(config.getPrefix() + page);
??????????????? } catch (ServletException e) {
??????????????????? throw new JspException(e.toString());
??????????????? } catch (IOException e) {
??????????????????? throw new JspException(e.toString());
??????????????? }
??????????? return (SKIP_PAGE);
??????? }
??? }
??? public void release() {
??????? super.release();
??????? this.name = Constants.USER_KEY;
??????? this.page = LOGIN_PATH;
??? }
}
但如果比較大的應(yīng)用還是用標(biāo)準(zhǔn)的jaas驗(yàn)證
7
<html:link action="/EditRegistration?action=Edit">
-------
///////////////////////////////////
<logic:equal
name="RegistrationForm"
property="action"
scope="request"
value="Edit"
>
<app:checkLogon/><!--如果action與Edit相等就執(zhí)行這里,否則不會(huì)執(zhí)行-->
</logic:equal>
1)////
<logic:present name="test">
如果在action中設(shè)置了test就執(zhí)行到這兒。如:request.setAttribute("test","test")或session.setAttribute("test","test")
<bean:write name="test"/>
</logic:present>
package org.apache.struts.webapp.example;
public final class EditSubscriptionAction extends Action
{
??? public EditSubscriptionAction()
??? {
??????? log = LogFactory.getLog("org.apache.struts.webapp.Example");
??? }
??? public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
??????? throws Exception
??? {
??????? HttpSession session = request.getSession();
??????? String action = request.getParameter("action");
??????? if(action == null)
??????????? action = "Create";
??????? String host = request.getParameter("host");
??????? if(log.isDebugEnabled())
??????????? log.debug("EditSubscriptionAction:? Processing " + action + " action");
??????? User user = (User)session.getAttribute("user");
??????? if(subscription == null && !action.equals("Create"))///create
??????? {
??????????? if(log.isTraceEnabled())
??????????????? log.trace(" No subscription for user " + user.getUsername() + " and host " + host);
??????????? return mapping.findForward("failure");
??????? }
??????? else //edit
?????? --------------------
}
8 MailReader源碼<struts 1.2 webapps下struts-mailreader.war并且包含說(shuō)明文檔,是每一個(gè)初學(xué)者和有經(jīng)驗(yàn)的,應(yīng)該要看的文章