SpringMVC+Spring+MyBatis 的综合练习 4 (配置 web.xml )

继续做基础配置……这步完事应该可以写代码了。呃~~~好像不对,还得先配置并运行 Mybatis Generator 先!!
唉~~漫漫长路……谁能告诉我,究竟会有多少错~~~停停停,打住!别再犯错了。


昨天,因为在 applicationContext.xml 中少写了一个大括号,让我痛苦地找了几个小时。就是下面这行:

<property name="password" value="${jdbc.password}"></property>

因为少了右边的大括号,于是password没能正确传入,而是传入了null。于是服务器就卡死在登录环节了。虽然我用dbForge登录没问题(废话,你正确输入密码了怎么会有问题!),但是……算了,不提了,只能说我太笨了。老了啊~~~~倒是借此机会看了很多文章,知道了报错的其他可能。
废话少说,今后再仔细些就是了。


因为是 Web 项目,自然要对 web.xml 进行一系列的配置。之所以从这个开始,是因为它是项目其他配置文件的源头,所以我觉得这样记录下来,并且今后也按照这个顺序配置项目,这样可以避免遗漏。具体代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- 1. 启动Spring容器 -->

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- 2. 配置SpringMVC前端控制器,拦截所有请求 -->

    <!-- The front controller of this Spring Web application, responsible for 
        handling all application requests -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 3. 字符编码过滤器 ,一定要放在所有过滤器之前 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 4. 使用Rest风格的URI,将页面普通的post请求转为指定的delete和put请求 -->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

可以看见,到目前为止,出现了两个新的需要配置的 xml 文件:

  • applicationContext.xml: 看上面第12行出现的哟。
  • dispatcherServlet.xml: 这个出现在上面代码的第27行呢。

如果缺少这两个配置文件,Tomcat是拒绝启动滴~~下篇继续。

猜你喜欢

转载自blog.csdn.net/hh680821/article/details/79045502