探索與發現

          研究java技術

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 109 評論 :: 0 Trackbacks

          閱讀struts MailReader文檔筆記:
          MailReader應用程序基于struts 1.2.0開發。
          1:主頁是index.jsp。由于struts的Action不能指定歡迎頁面
          ,而首次會從服務器配置的歡迎列表去查找出相應的頁面返回給用戶,
          那么我們怎么來用struts的actions而不是普通的jsp頁面返回給用戶呢,
          一個解決方案是在一個頁面寫上要轉發到我們的歡迎頁面,代碼:
          <%@ 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>有兩個優點:
          (1)允許在url中以多種方式包含請求。
          (2)當用戶關閉cookie時,會自動重寫url,把sessionid作為請求參數包含在url當中,用于跟蹤用戶的session狀態,而不像servlet,jsp還要
          自己硬編碼實現
          它有幾個重要的屬性:
          *forward:指定全局轉發鏈接(只適用于(flobal-forwards>forward,而不能引用action forward子元素)
          *href:指定完整的url鏈接(<html:link url="http//www.sina.com"/>)
          *page:指定相對于當前網頁的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是自己開發的一個插件,它必須org.paache.struts.action.PlugIn接口,包含兩個方法init,destroy
          init在struts加載時自動被調用,destroy當應用關閉時調用,可以放一些釋放資源的語句(如關閉數據庫連接的語句等)
          并且這個里面還包含屬性pathname,也要相應的get,set方法,以便在struts框架在加載插件時,會自動調用setPathname()方法,把
          <set-property>子元素的pathname設置成MemoryDatabasePlugIn里對應屬性的值value="/WEB-INF/database.xml"
          還要注意就是<plug-in>必須位于其它配置元素后面,出現多個按順序加載

          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"
          那么它會自動創建一個ActionForms與之對應
          exception子元素,當一個用戶登錄以后,有可能 "ExpiredPasswordException"(超時) 會拋出.
          ?如果發生了的話 Struts 會捕獲exception 并發送到 "ExpiredPassword" action.

          6:
          自己開發一個定制標記<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相等就執行這里,否則不會執行-->
          </logic:equal>
          1)////
          <logic:present name="test">
          如果在action中設置了test就執行到這兒。如: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并且包含說明文檔,是每一個初學者和有經驗的,應該要看的文章

          posted on 2006-05-21 01:33 蜘蛛 閱讀(2151) 評論(5)  編輯  收藏 所屬分類: struts

          評論

          # re: 閱讀struts MailReader文檔小記 2006-05-22 14:38 綠色使者、綠色心情
          使用jstl很多時候比自定義標簽效果要更好一些,比如上面的需求  回復  更多評論
            

          # re: 閱讀struts MailReader文檔小記 2006-11-19 08:10 成人用品
          常樂集團成立于2004年9月。我們主要經營成人用品性用品的批發和零售,本著誠信第一的原則,我們不斷擴大銷售范圍,產品不斷走向全國各個城市。我們的目標是讓更多的人用我們的產品,讓更多的人能提高生活質量!
          電話:13605487249
          QQ:609797387
          網址:http://www.sex0539.com/ 常樂成人用品性用品批發商城  回復  更多評論
            

          # re: 閱讀struts MailReader文檔小記 2006-11-19 08:11 成人用品
          常樂集團成立于2004年9月。我們主要經營成人用品性用品的批發和零售,本著誠信第一的原則,我們不斷擴大銷售范圍,產品不斷走向全國各個城市。我們的目標是讓更多的人用我們的產品,讓更多的人能提高生活質量!
          電話:13605487249
          QQ:609797387
          網址:http://www.sex0539.com/ 常樂成人用品性用品批發商城  回復  更多評論
            

          # re: www.e68d.com 2007-02-16 10:34 liang
          hello  回復  更多評論
            

          # 北京千源網站建設 2007-03-30 22:56 北京千源網站建設
          千源設計一家是以設計為主導、專業為企業提供網站建設,網頁設計,平面設計,標志設計,Flash動畫設計服務的設計工作室。
          http://www.websdesign.cn  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 江城| 井冈山市| 辉南县| 东丽区| 永福县| 拜泉县| 绥阳县| 镇江市| 大荔县| 郓城县| 夹江县| 铁岭市| 金山区| 托克托县| 彭州市| 拉萨市| 冕宁县| 彝良县| 盐津县| 德化县| 盐城市| 扶余县| 台中市| 周至县| 英德市| 庆云县| 大宁县| 榆林市| 哈尔滨市| 乐山市| 涡阳县| 金堂县| 新巴尔虎右旗| 仪征市| 芦山县| 隆尧县| 新田县| 镇巴县| 淳化县| 马边| 阜新|