SpringMVC+Hibernate+MySQL自己开发

本文详细介绍了如何配置 Spring MVC 的 web.xml 文件,包括 Spring IOC 容器的配置、DispatcherServlet 控制器的配置、字符编码过滤器及 REST 风格的支持配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:新建一个动态web项目,目录结构如下:


第二步,定义web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns="http://java.sun.com/xml/ns/javaee"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
        id="WebApp_ID" version="2.5">  
        <display-name>SpringMvcTest</display-name>  
        <welcome-file-list>  
            <welcome-file>index.jsp</welcome-file>  
        </welcome-file-list>  
      
        <!-- 配置Spring IOC 容器 -->  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:WebContent/config/applicationContext.xml</param-value>
        </context-param>  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
        </listener>  
      
        <!-- 配置SpringMVC 的 DispatcherServlet 控制器 -->  
        <servlet>  
            <servlet-name>dispatcherServlet</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置名称 -->  
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath:WebContent/config/springmvc.xml</param-value>
            </init-param>  
            <load-on-startup>1</load-on-startup>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>dispatcherServlet</servlet-name>  
            <url-pattern>/*</url-pattern>  
        </servlet-mapping>  
      
        <!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->  
        <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>  
        </filter>  
        <filter-mapping>  
            <filter-name>characterEncodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  
          
        <!-- 为了使用SpringMVC框架实现REST风格,需要配置  HiddenHttpMethodFilter-->  
        <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>  
对于xmlns的理解如下:xmlns其实是xml namespace,xml的命名空间。之所以使用xmlns是为了防止多个xml文件同时使用时会引起冲突的问题。因此,给多个xml文件贴上标签,在xmlns里标明,就可以防止冲突的发生。首先,使用的语法是xmln:名字=“名字的路径”。如xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"。就是定义了一个路径为http://www.w3.org/2001/XMLSchema-instance,名字为xsi的语句。而bean、web-app这类在xmlns前面的,是一个标签,所有<bean></bean>、<web-app></web-app>,都会被标记在一起。

   xmlns和xmlns:xsi有什么不同?

    xmlns表示默认的Namespace。例如Spring XML文档中的

1
xmlns="http://www.springframework.org/schema/beans"

    这一句表示该文档默认的XML Namespace为http://www.springframwork.org/schema/beans。对于默认的Namespace中的元素,可以不使用前缀。例如Spring XML文档中的

1
2
3
<bean id="xxx" class="xxx.xxx.xxx.Xxx">
  <property name="xxx" value="xxxx"/>
</bean>

  xmlns:xsi表示使用xsi作为前缀的Namespace,当然前缀xsi需要在文档中声明。

xsi:schemaLocation有何作用

xsi:schemaLocation,可由多个url对组成,每个url之间用空格符隔开。它怎么理解呢?举个例子:如xsi:A=“B” “C”。假如前面已经定义过xmlns:xsi="..."。则,那句话就表示,命名空间为xsi的标签A,A的内容为B,路径为C。

所以在看到xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"这样的,也可以理解了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值