美文网首页
Struts2配置文件-web.xml

Struts2配置文件-web.xml

作者: PHOME_M | 来源:发表于2016-06-16 21:35 被阅读0次

    一、web.xml配置文件
    1.welcome-file-list和welcome-file元素
    是用来指定欢迎页面的,welecome-file-list元素可以包含多个welcome-file元素,每个welcome-file指定一个欢迎页面。

      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    

    2.filter和filter-mapping 元素
    filter元素用于声明一个过滤器,使用该元素可以同时拦截多个请求的url,filter-mapping用来指定与过滤器关联的URL

      <!-- 配置Struts2框架核心控制器 -->
      <filter>
       <filter-name>struts</filter-name>
       <!-- 这里使用快捷键command+shift+T打开open type,搜索 -->
       <!-- 指定filter的实现类,此处使用的是Struts2提供的过滤器类 -->
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <!-- 定义filter所拦截的url地址 -->
      <filter-mapping>
      <!-- filter的名字,该名字必须是filter元素中已声明过的过滤器的名字 -->
       <filter-name>struts</filter-name>
       <!-- 定义filter负责拦截的url地址 -->
       <url-pattern>/*</url-pattern>
      </filter-mapping>
    

    3.error-page元素
    error-page元素用来指定错误处理页面,可以通过配置错误码元素error-code以避免用户直接看到原始错误信息,还可以配置异常类型元素exception-type来指定java中的异常类。

    <!-- 配置异常页 -->
      <error-page>
       <error-code>404</error-code>  <!-- 指定错误代码 -->
       <location>/error.jsp</location> <!-- 如果发生HTTP404错误,则返回location子元素中的指定文件 --> 
      </error-page>
      <!-- 配置error-page元素用于捕获java异常 -->
      <error-page>
       <exception-type>java.lang.Exception</exception-type><!-- 指定异常类 -->
       <location>/error.jsp</location>
      </error-page>
    

    4.listener元素
    该元素用来注册监听器类,并使用子元素listener-class指定监听程序的完整限定类名,一般用于初始化Spring 框架

    <!-- 监听器 -->
      <listener>
       <listener-class>org.springframework.web.context.ContextloaderListener</listener-class>
      </listener>
    

    5.session-config元素
    该元素用来指定回话过期时间,session对象里面存放的值会自动失效

    <!-- 回话时间配置 -->
      <session-config>
       <session-timeout>30</session-timeout>
      </session-config>
    

    6.init-param元素
    该元素用来定义参数,在web.xml中可以有多个init-param元素

      <init-param>
       <param-name>strust.il8n.encoding</param-name>
       <param-value>UTF-8</param-value>
      </init-param>
    

    相关文章

      网友评论

          本文标题:Struts2配置文件-web.xml

          本文链接:https://www.haomeiwen.com/subject/jwnmdttx.html