web.xml文件得配置

1.背景介绍

1.web.xml文件的特点

必须有且只有一个根节点,大小写比较铭感,标签不嵌套,必须配对。


2、web.xml文件的作用


web.xml文件是用来初始化配置的信息:比如Welcom页面、srvlet、servlet-mapping、filter、listener、启动加载级别等。当你的web工厂没用到这些时,你可以不用web.xml文件来配置你的Application.


3、web.xml能做的事情


在web.xml的模式(Schema(架构))文件中定义了多种标签元素,web.xml中就可以出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能,一般来说记住一些常用的就好。


2.知识剖析 


1.Spring框架解决字符串编码问题:过滤器CharacterEncodingFilter(filer-name)

我们在开发时,经常要对中文字符进行处理,进行处理中文字符的方式也有很多种,可以转码工具类,设置过滤器进行转码等等。

在使用到spring框架时,我们可以直接在web.xml配置文件下,添加配置,进行字符的处理(设置过滤器)


2.在web.xml配置监听器ContextLoaderListener(listener-class)ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

3.部署applicationContext的xml文件


4、DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。

3.常见问题

1)在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?

2)url-pattern配置为"/"和"/*"的区别

3)web.xml 文件中一般包括 servlet, spring, filter, listener的配置。那么他们是按照一个什么顺序加载呢?

4.解决方案

1.配置一个编码过滤器

2.其中/和/*的区别 < url-pattern > /   不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet(前端控制器)类 。 < url-pattern > /* 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错

3.加载顺序为: listener >> filter >> servlet >> spring
加载顺序会影响对spring bean的调用.比如filter需要用到bean,但是加载顺序是先加载filter 后加载spring,则filter中初始化操作中的bean为null

5.编码实战

这部分编码是我们在web这个项目里基本上常用的,如果还需要其他的还可以再去配置。

xml version="1.0" encoding="UTF-8"?>



<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  
    
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xml,classpath:spring-service.xmlparam-value>
  context-param>

  
  <servlet>
    <servlet-name>dispatcherservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:controller.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>dispatcherservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <jsp-config>
    <taglib>
      <taglib-uri>/tagstaglib-uri>
      <taglib-location>/WEB-INF/tld/datetag.tldtaglib-location>
    taglib>
  jsp-config>


  
  <filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter
    filter-class>
  filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>



web-app>

5.参考文献

文献一:  https://blog.csdn.net/zuoluoboy/article/details/4213053


文献二:  http://www.cnblogs.com/hxsyl/p/3435412.html


文献三:  https://blog.csdn.net/qing_gee/article/details/50965562

你可能感兴趣的:(java)