閱讀struts MailReader文檔筆記:
MailReader應用程序基于struts 1.2.0開發(fā)。
1:主頁是index.jsp。由于struts的Action不能指定歡迎頁面
,而首次會從服務(wù)器配置的歡迎列表去查找出相應的頁面返回給用戶,
那么我們怎么來用struts的actions而不是普通的jsp頁面返回給用戶呢,
一個解決方案是在一個頁面寫上要轉(zhuǎn)發(fā)到我們的歡迎頁面,代碼:
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect action="/Welcome"/>
在相應的struts-config.xml配置文件加上
<!-- Display welcome page -->
<action path="/Welcome" forward="/welcome.jsp" />
但其它頁面還不能保證用戶不能訪問到,我們在應用當中一般會把所有的
jsp頁面放到WEB-INF目錄下面,然后在struts-config.xml做一下映射就可以了,以保證用戶不能直接訪問到。
2:<message-resources parameter="org.apache.struts.webapp.example.MessageResources" />
在同一個struts里面只能有一個默認的存放本地化的消息文本(Resource Bundle)
那很我們指定多個的時候可以用它的一個屬性key指定
例如:
<message-resources parameter="org.apache.struts.webapp.example.AlternateMessageResources" key="alternate" />
那么我們在頁面用的時候這樣出別
<bean:message key="key0"/>;
<bean:message key="key1" bundle="alternate"/>
3:<html:link>有兩個優(yōu)點:
(1)允許在url中以多種方式包含請求。
(2)當用戶關(guān)閉cookie時,會自動重寫url,把sessionid作為請求參數(shù)包含在url當中,用于跟蹤用戶的session狀態(tài),而不像servlet,jsp還要
自己硬編碼實現(xiàn)
它有幾個重要的屬性:
*forward:指定全局轉(zhuǎn)發(fā)鏈接(只適用于(flobal-forwards>forward,而不能引用action forward子元素)
*href:指定完整的url鏈接(<html:link url="http//www.sina.com"/>)
*page:指定相對于當前網(wǎng)頁的url(<html:link page="/test.do"/>
4:PlugIn(struts插件)
在struts-config.xml要加上相應的描述語句
<plug-in className="org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn">
? <set-property property="pathname" value="/WEB-INF/database.xml"/>
</plug-in>
其中MemoryDatabaseplugIn是自己開發(fā)的一個插件,它必須org.paache.struts.action.PlugIn接口,包含兩個方法init,destroy
init在struts加載時自動被調(diào)用,destroy當應用關(guān)閉時調(diào)用,可以放一些釋放資源的語句(如關(guān)閉數(shù)據(jù)庫連接的語句等)
并且這個里面還包含屬性pathname,也要相應的get,set方法,以便在struts框架在加載插件時,會自動調(diào)用setPathname()方法,把
<set-property>子元素的pathname設(shè)置成MemoryDatabasePlugIn里對應屬性的值value="/WEB-INF/database.xml"
還要注意就是<plug-in>必須位于其它配置元素后面,出現(xiàn)多個按順序加載
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,當然也可以用session,一個ActionForm只對應一次請求,不要越過request,
如果我們使用type="org.apache.struts.validator.DynaValidatorForm"
那么它會自動創(chuàng)建一個ActionForms與之對應
exception子元素,當一個用戶登錄以后,有可能 "ExpiredPasswordException"(超時) 會拋出.
?如果發(fā)生了的話 Struts 會捕獲exception 并發(fā)送到 "ExpiredPassword" action.
6:
自己開發(fā)一個定制標記<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;
??? }
}
但如果比較大的應用還是用標準的jaas驗證
7
<html:link action="/EditRegistration?action=Edit">
-------
///////////////////////////////////
<logic:equal
name="RegistrationForm"
property="action"
scope="request"
value="Edit"
>
<app:checkLogon/><!--如果action與Edit相等就執(zhí)行這里,否則不會執(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并且包含說明文檔,是每一個初學者和有經(jīng)驗的,應該要看的文章