• SSH框架学习笔记(三)----struts2的开发步骤,常规配置,异常处理,国际化


    1. struts2应用开发步骤:
      1. 在web.xml文件中定义核心Filter来拦截用户请求
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      2. 定义处理用户请求的Action类
      3. 在struts.xml中配置Action:
        <action name="login" class="action.LoginAction">
                    <result name="SUCCESS">/index.jsp</result>
                    <result name="ERROR">/error.jsp</result>
        </action>
    2. struts2的常规配置
      1. 常量配置的3中方式:
        1. 通过struts.properties文件配置
        2. 通过struts.xml文件配置如下
          <constant name="struts.enable.DynamicMethodInvocation" value="false" />
          <constant name="struts.devMode" value="false" />
        3. 通过Web应用的web.xml文件配置:在filter标签中用init-param标签配置,如下图
          <filter>
              <filter-name>struts2</filter-name>
              <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
               <init-param>
                       <param-name>struts.action.extension</param-name>
                       <param-value>action,php</param-value>
                </init-param>
          </filter>
      2. 加载struts2配置常量的顺序:struts-default.xml->struts-plugin.xml->struts.xml->struts.properties->web.xml   后面的文件会覆盖前面的
      3. struts.xml文件可以包含子模块:
        <include file="resource/conf/st.xml" />
    3. Action的实现
      1. struts2直接用Action封装HTTP请求参数,Action中应该包含与请求参数对应的实例变量,并为这些变量设置setter.getter 方法
      2. Action的那些实现类:
        1. ActionSupport:提供了许多默认方法,包括获取国际化信息方法,数据校验方法,默认的处理用户请求方法,大大简化了Action的开发
        2. ActionContext:该类不是Actiong的实现类,Action 可以通过该类来访问Servlet API
        3. ServletContextAware:实现该接口的Action可以直接访问ServletContext实例
        4. ServletRequestAware:实现它可以直接访问HttpServletRequest实例
        5. ServletResponseAware:实现它可以直接访问HttpServletResponse实例
        6. ServletActionContext:工具类,可以拿到PageContext,HttpServletRequest,HttpServletResponse,ServletContext对象
    4. Action的配置
      1. 包和命名空间:
        <package name="login" namespace="/login" extends="json">
        1. name:包的名字
        2. extends:指定该包继承的其他包,可以继承其他包中的Action定义,拦截器定义
        3. namespace:定义命名空间,有了命名空间后url=命名空间/Action名
      2. Action的基本配置
        <action name="getMenuInfoList" method="queryMenuInfoList" class="loginAction">
                    <result name="success" type="json"></result>
        </action>

        name:指定action名字  class:指定对应的Action类   method:指定对应的方法

    5. 配置struts2的异常处理:
      1. 开启异常映射功能:该功能需要一个拦截器<interceptor>,拦截器在struts-default.xml中已经有定义,struts.xml只需要继承struts-default.xml就行了
      2. 声明异常捕捉:
        1. 局部异常映射:
          <action>
                 <exception-mapping exception="异常类" result="my"/>
                  <result name="my">url</result>
          </action>
        2. 全局异常映射:
          <global-exception-mappings exception="异常类" result="sql"/>
          <global-results>
              <result name="sql">url</result>
          </global-results>
      3. 输出异常信息
        1. <s:property value="exception"/>:输出异常对象本身
        2. <s:property value="exceptionStack">:输出异常堆栈信息
    6. struts2的国际化:
      1. 国际化步骤:
        • 系统加载国际化资源文件,加载文件有2中方式:
          • 自动加载:Action范围,包范围的国际化资源文件由系统自动加载
          • 手动加载:JSP范围,全局范围的国际化资源文件需要分别使用标签,配置常量加载
        • 输出国际化,有2中方式:
          • 在视图页面输出国际化需要使用标签库
          • 在Action类中输出国际化需要使用ActionSupport的getText()方法来完成
      2. 视图页面国际化:
        1. 加载资源:<s:i18n name="包名.baseName"/>
        2. 输出国际化:
          • 在text标签中:<s:text name=""/>
          • 在表单标签中用key:<s:textfield name="username" key=""/>
      3. Action国际化:
        1. 自动加载资源的条件:
          • 资源文件的baseName与Action类的类名相同
          • 资源文件与Action类的.class文件在同个路径下
        2. 输出国际化:使用ActionSupport的getText()方法来完成
      4. 包范围国际化:
        1. 自动加载资源的条件:例如把资源保存在WEB-INFclassesorgcrazyitappaction路径下,该资源文件就可以被org.crazyit.app.action包及其所有子包的Action访问
        2. 输出国际化与Action的一样
      5. 全局国际化:
        1. 加载资源:配置struts常量struts.custom.i18n.resources=baseName
        2. 输出国际化以上方法都可用
  • 相关阅读:
    将配置文件自动复制到vs的测试项目中
    用索引器简化的C#类型信息访问
    Requested Clipboard operation did not succeed的解决办法
    在win2003上IIS部署可能出现的问题的解决方案
    Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
    在Ubuntu虚拟机中配置bridge共享上网
    Ajax学习日志
    Workflow architecture in Windows SharePoint Services (version 3):WWF和WSS V3 的关系 无为而为
    使用BizTalk的必须关注:HWS已经死了,微软已经放弃HWS了,估计替代产品就WWF。(外加其它的宣告死亡的工具和API列表) 无为而为
    我想要求主管给我们升级电脑,但是主管不肯,大家报一报公司电脑的配置,我看我的电脑是不是最差的,顺便谈谈你们公司电脑硬件升级策略 无为而为
  • 原文地址:https://www.cnblogs.com/XD-thinker/p/6727718.html
Copyright © 2020-2023  润新知