<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    歡迎使用我的 在線工具

    小D

    讀歷史、看小說、寫程序都是我所愛。技術(shù)不好,頭腦不靈光,靠的是興趣。
    隨筆 - 35, 文章 - 25, 評論 - 13, 引用 - 0
    數(shù)據(jù)加載中……

    Struts1.x的簡單解讀

    1 Struts1.x 簡單配置

    a )、配置 struts

    ?????? * 拷貝 struts lib 下的所有 jar WEB-INF/lib

    ?????? * 修改 web.xml 文件,配置 ActionServlet

    ?????? * 提供 struts-config.xml 文件

    ??????

    b) 、創(chuàng)建登錄項目

    ?????? * 創(chuàng)建 jsp 頁面( login.jsp,login_success.jsp,login_error.jsp ?

    ?????? * 創(chuàng)建 LoginActionForm.java

    ?????? * 創(chuàng)建 LoginAction.java

    ?????? * 配置 struts-config.xml 文件

    ?

    2 I18N 問題

    ?????? 首先創(chuàng)建國際化資源文件: MessageResources_zh_CN.properties ,如此格式,

    然后在 Struts-config.xml 中配置一個 Action 處理,該編碼問題,并做如下代碼:

    ?????? ?????? String lang = request.getParameter("lang");// 取得指定的語言

    ????????????? Locale currentLocale = Locale.getDefault();// 得到本地 Locale

    ?????????????

    ????????????? if("zh".equals(lang)){

    ???????????????????? currentLocale = new Locale("zh","CN");

    ????????????? }else if("en".equals(lang)){

    ???????????????????? currentLocale = new Locale("en","US");

    ????????????? }

    ????????????? // 修改 session Globals.LOCALE_KEY

    ????????????? request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);

    ?????????????

    ?

    3 、采用 DispathAction

    ?????? * 如果覆寫 DispathAction 中的 execute 方法,必須顯示的用 super 調(diào)用 execute 方法

    ?????? * parameter 參數(shù)值不能是 execute perform

    ?????? * 了解 <action> 標簽中的 parameter 的含義

    ?????? * 了解 DispathAction 中的 unspecified 方法的含義

    ?

    4 、學會使用 FormBean

    ?????? 在配置文件中配置的 ActionForm 在解析 ActionMapping 時,會以 Action name key ,然后在 HttpSession 中存儲該 ActionForm 的值,那么我們在整個會話中都會得到這個 Form ,而且這個 Form 在每次請求的時候都會更新。

    ?

    5 ActionServlet 初始化方法

    ?? public void init() throws ServletException {

    ??????? final String configPrefix = "config/" ;

    ??????? final int configPrefixLength = configPrefix.length() - 1;

    ?

    ??????? // Wraps the entire initialization in a try/catch to better handle

    ??????? // unexpected exceptions and errors to provide better feedback

    ??????? // to the developer

    ??????? try {

    // 初始化我們系統(tǒng)的資源信息綁定

    ??????????? initInternal();

    ????????? ??// 初始化控制器的字符集轉(zhuǎn)換信息,通過配置 web.xml 中的 convertNull 參數(shù)來進行數(shù)據(jù)類型的轉(zhuǎn)換。通過注冊轉(zhuǎn)換器來實現(xiàn)。

    initOther();?

    // 初始化我們當前請求的 Servlet 控制器

    ????????? ??initServlet();

    // 解析配置文件 web.xml 文件中的 chainConfig 初始化參數(shù)去配置在 CatalogFactory 中默認的 org.apache.commons.chain.Catalog

    ??????????? initChain();

    // 在當前的 Servlet 上下文中使用 Globals.ACTION_SERVLET_KEY 保存當前的 ActionServlet

    ??????????? getServletContext().setAttribute(Globals. ACTION_SERVLET_KEY , this );

    // 初始化 ModuleConfig 的模版工廠

    ??????????? initModuleConfigFactory();

    ?

    ??????? ????// 初始化當前需要的 ModuleConfig, 即開始初始化 struts-config.xml

    ??????????? ModuleConfig moduleConfig = initModuleConfig( "" , config );

    ?????? ??? // 初始化配置文檔中的國際化資源信息,并將其存入當前的 ServletCotext 中。

    ??????????? initModuleMessageResources(moduleConfig);

    ?????? ??? // 初始化配置文檔中的插件

    ??????????? initModulePlugIns(moduleConfig);

    ?????????? // 初始化 FormBeans 并建起存入 moduleConfig 中,其實是保存在當前的 HttpSession 中,每次請求替換一次該 FormBeans

    ??????????? initModuleFormBeans(moduleConfig);

    // 初始化全局化的 Forward Forward 保存在當前的 ActionConfig 或者 moduleConfig

    ??????????? initModuleForwards(moduleConfig);

    // 初始化全局 Exception Exception 保存在當前的 ActionConfig 或者 moduleConfig

    ??????????? initModuleExceptionConfigs(moduleConfig);

    // 初始化 Action ,將當前 ActionConfig 保存在 moduleConfig 中,用路徑做為 key

    ??????????? initModuleActions(moduleConfig);

    // 凍結(jié)當前配置模版,任何修改將會導(dǎo)致 IllegalStateException

    ??????????? moduleConfig.freeze();

    // 取得當前 Application web.xml 中的所有的初始化參數(shù),然后在解析配置文件

    ??????????? Enumeration names = getServletConfig().getInitParameterNames();

    ?

    ??????????? while (names.hasMoreElements()) {

    ??????????????? String name = (String) names.nextElement();

    ?

    ??????????????? if (!name.startsWith(configPrefix)) {

    ??????????????????? continue ;

    ? ?????????????? }

    ?

    ??????????????? String prefix = name.substring(configPrefixLength);

    ?

    ??????????????? moduleConfig =

    ??????????????????? initModuleConfig(prefix,

    ??????????????????????? getServletConfig().getInitParameter(name));

    ??????????????? initModuleMessageResources(moduleConfig);

    ??????????????? initModulePlugIns(moduleConfig);

    ??????????????? initModuleFormBeans(moduleConfig);

    ??????????????? initModuleForwards(moduleConfig);

    ??????????????? initModuleExceptionConfigs(moduleConfig);

    ?????????????? ?initModuleActions(moduleConfig);

    ??????????????? moduleConfig.freeze();

    ??????????? }

    // 保持模版前綴的字符串數(shù)組在 ServletContext 中。使用 Globals.MODULE_PREFIXES_KEY ??????????? this .initModulePrefixes( this .getServletContext());

    // 溫柔的釋放我們創(chuàng)建的 ConfigDigester

    ??????????? this .destroyConfigDigester();

    ??????? } catch (UnavailableException ex) {

    ??????????? throw ex;

    ??????? } catch (Throwable t) {

    ??????????? // The follow error message is not retrieved from internal message

    ??????????? // resources as they may not have been able to have been

    ??????????? // initialized

    ??????????? log .error( "Unable to initialize Struts ActionServlet due to an "

    ??????????????? + "unexpected exception or error thrown, so marking the "

    ??????????????? + "servlet as unavailable.? Most likely, this is due to an "

    ??????????????? + "incorrect or missing library dependency." , t);

    ?????????? ? throw new UnavailableException(t.getMessage());

    ??????? }

    ??? }

    ??????

    ?

    ?

    ?

    ?

    ?

    6 Struts 請求處理流程

    ?????? ?????? 1 )、請求通過 doGet doPost 方法委托給 ActionServlet process 方法。

    ??? ??? // 執(zhí)行標準的請求處理方法,并創(chuàng)建相應(yīng)的回復(fù)

    ?????? protected void process(HttpServletRequest request,

    ??????? ??????? HttpServletResponse response)

    ??????? ??????? throws IOException, ServletException {

    ?????? ??? ??? // 根據(jù)請求選擇相應(yīng)的模版信息,而且添加相應(yīng)的請求屬性到當前的請求

    ModuleUtils.getInstance().selectModule(request, getServletContext());

    ??? ??? // 為選中的模版,選擇相應(yīng)的模版配置信息對象

    ??????? ModuleConfig config = getModuleConfig(request);

    ??? ??? // 根據(jù)當前的模版配置信息,返回一個請求處理器,如果沒有的話則返回 null ,該方法不會創(chuàng)建 RequestProcessor

    ??????? RequestProcessor processor = getProcessorForModule(config);

    ?????? // 如果該模版的請求處理器為 null ,則使用模版信息創(chuàng)建一個 RequestProcessor

    ??????? if (processor == null ) {

    ??????????? processor = getRequestProcessor(config);

    ??? ????}

    ?????? // 使用請求處理器來處理當前的請求, RequestProcessor 以及 process 方法是整個 Struts 的核心對象和方法

    ??????? processor.process(request, response);

    }

    ?

    2 )、 RequestProcessor process 方法

    ?

    ??? public void process(HttpServletRequest request, HttpServletResponse response)

    ??????? throws IOException, ServletException {

    ??????? // Wrap multipart requests with a special wrapper (用一個特效的包裝器來包裝多媒體請求,如果當前請求不是多媒體的則方法原理的請求對象)

    ??????? request = processMultipart(request);

    ?

    ??????? // Identify the path component we will use to select a mapping

    ?????? // 根據(jù)請求 URI ,選擇一個 Action Mapping 去分派請求,如果沒有則報錯或結(jié)束方法

    ??????? String path = processPath(request, response);

    ?

    ??????? if (path == null ) {

    ??????????? return ;

    ??????? }

    ?

    ??????? if ( log .isDebugEnabled()) {

    ??????????? log .debug( "Processing a '" + request.getMethod() + "' for path '"

    ??????????????? + path + "'" );

    ??????? }

    ?

    ??????? // Select a Locale (地點) for the current user if requested

    ??????? processLocale(request, response);

    ?

    ??????? // Set the content type and no-caching headers if requested

    ??????? processContent(request, response);

    ??????? processNoCache(request, response);

    ?

    ??????? // General purpose preprocessing (預(yù)處理) hook true

    則表示正常執(zhí)行下面語句,否則表示已經(jīng)完成處理

    ??????? if (!processPreprocess(request, response)) {

    ??????????? return ;

    ??????? }

    ?????? // session 中移除或者添加修改 Grobel.MESSAGE_KEY Grobel.ERROR_KEY

    ??????? this .processCachedMessages(request, response);

    ??????

    ??????? // Identify the mapping for this request ,根據(jù) path request 選擇一個 ActionMapping ,來訪問相應(yīng)的 Action ActionMapping 提供給 RequestProcessor 對于 Action 的各種處理信息。

    ??????? ActionMapping mapping = processMapping(request, response, path);

    ?

    ??????? if (mapping == null ) {

    ??????????? return ;

    ??????? }

    ?

    ??????? // Check for any role required to perform this action

    ??????? if (!processRoles(request, response, mapping)) {

    ??????????? return ;

    ??????? }

    ?

    ??????? // Process any ActionForm bean related to (涉及) this request

    ??????? ActionForm form = processActionForm(request, response, mapping);

    // 根據(jù) request 中的請求參數(shù),組裝特殊的 ActionForm

    ??????? processPopulate(request, response, form, mapping);

    ?

    ??????? // 如果這個請求沒有取消而且這個請求的 ActionMapping 沒有取消驗證,調(diào)用指定的 ActionForm 中的 validate 方法,然后轉(zhuǎn)發(fā)到 input 路徑指定的頁面也就是錯誤發(fā)生的頁面。返回 true 我們將繼續(xù)處理,否則我們已經(jīng)被控制轉(zhuǎn)發(fā)到 input 表單頁面中。

    ??????? try {

    ??????????? if (!processValidate(request, response, form, mapping)) {

    ??????????????? return ;

    ??????????? }

    ??????? } catch (InvalidCancelException e) {

    // 詢問我們的異常處理器去處理異常。有調(diào)用的 ExceptionHandler 返回 ActionForward 實例

    ActionForward forward = processException(request, response, e, form, mapping);

    // 根據(jù)指定的機制轉(zhuǎn)發(fā)或者重定向到指定的目的地。

    ??????????? processForwardConfig(request, response, forward);

    ??????????? return ;

    ??????? } catch (IOException e) {

    ??????????? throw e;

    ??????? } catch (ServletException e) {

    ??????????? throw e;

    ??????? }

    ?

    ??????? // Process a forward or include specified by this mapping

    ??????? if (!processForward(request, response, mapping)) {

    ??????????? return ;

    ??????? }

    ?

    ??????? if (!processInclude(request, response, mapping)) {

    ??????????? return ;

    ??????? }

    ?

    ??????? // 返回一個用于處理當前請求的 ActionForm 實例,如果必要的話返回一個新實例

    ??????? Action action = processActionCreate(request, response, mapping);

    ?

    ??????? if (action == null ) {

    ??????????? return ;

    ??????? }

    ?

    ??????? // 詢問指定的 Action 實例去處理當前的請求。根據(jù)調(diào)用的 Action 返回 ActionForward 實例用作以后的處理。

    ??????? ActionForward forward =

    ??????????? processActionPerform(request, response, action, form, mapping);

    ?

    ???? ???// 根據(jù)指定的機制轉(zhuǎn)發(fā)或者重定向到指定的目的地。

    ??????? processForwardConfig(request, response, forward);

    ??? }

    posted on 2009-11-04 21:13 vagasnail 閱讀(295) 評論(0)  編輯  收藏 所屬分類: Java框架


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产成人精品免费视频大全| 亚洲国产天堂久久综合网站| 亚洲youwu永久无码精品| 99re免费在线视频| 亚洲人成电影在线天堂| 久久大香伊焦在人线免费| 久久亚洲精品中文字幕三区| 日本在线看片免费| 亚洲视频小说图片| 免费AA片少妇人AA片直播| 亚洲va在线va天堂成人| 在线a毛片免费视频观看| 精品国产亚洲AV麻豆| 亚洲伊人久久综合中文成人网| 国产精品九九久久免费视频 | 亚洲综合色区中文字幕| 男女超爽刺激视频免费播放| 亚洲人成色777777老人头| 在线观着免费观看国产黄| caoporm超免费公开视频| 久久精品国产亚洲AV麻豆王友容| 久久久久久AV无码免费网站下载 | 免费在线观看的黄色网址| sss日本免费完整版在线观看| 久久亚洲色一区二区三区| 日韩电影免费在线观看中文字幕 | 久久久亚洲AV波多野结衣| 成人毛片视频免费网站观看| 亚洲精品色在线网站| 国产亚洲精品自在久久| 91麻豆最新在线人成免费观看| 亚洲暴爽av人人爽日日碰| 亚洲精品高清国产一线久久| 999国内精品永久免费视频| 亚洲精品色在线网站| 久久久久亚洲av无码专区蜜芽| 最新中文字幕免费视频| 两个人看www免费视频| 久久久国产亚洲精品| 亚洲人色婷婷成人网站在线观看 | 国产精品免费综合一区视频|