SpringMVC工程的web.xml以及其他配置文件

<!-- 当前的这个web.xml是maven为我们自动生成的,在web-app2.3下我们的jsp页面会默认的将EL表达式关闭;所以我们希望将这个东西替换掉,使用我们的2.4以上的版本 -->
<!-- 2.3 -->
<!--  -->
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>


<!-- 2.4 默认支持EL-->
<!--  
<?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/j2ee"
        xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "
        version="2.4">
-->
  <display-name>HelloSpringMVC</display-name>
  
  <!-- DispatcherServlet有一些参数配置,默认情况下我们不需要指定,如果需要可通过<servlet>的<init-param>指定
  1)namespace:DispatcherServlet对应的命名空间,默认为$servlet-name$-servlet,用于构造spring配置文件的路径;指定该属性后,配置文件对应的路径就变成了WEB-INF路径下namespace.xml文件
 
  2)contextConfigLocation:如果DispatcherServlet上下文对应的spring配置文件有多个,则可使用该属性按照spring资源路径的方式指定,它和namespace有一样的功效
 
  3)publishContext:一个boolean类型的属性,默认值是true,DispatcherServlet会根据该属性的值决定是否将webapplicationtext发布到servletContext属性列表中,以便调用者可借由servletContext找到webApplicationContext实例
 
  4)publishEvents:一个boolean类型的属性,决定当DispatcherServlet处理完一个请求之后,是否需要向容器发布一个servletRequestHandleEvent事件,默认为true;如果容器中没有任何事件监听器,可以将此属性设定为false,以便提高运行性能
  
  -->
  <!-- ①业务层和持久层Spring配置文件,这些配置文件被父Spring容器所应用 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <!-- 若有多个配置文件,可以使用逗号分隔 -->
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- ②声明DispatcherServlet -->
  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- 指定了springmvc配置文件所在的位置 -->
          <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
</init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <!-- ③处理所有的http请求 ,由于拦截不同的dong可以有多个dispatcherServlet-->
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  

</web-app>







<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 
    <!-- 本配置文件是供名为dispatcher的DispatcherServlet使用,提供其相关的Spring MVC配置 --> 
    
<!-- 启用Spring基于annotation的DI,使用户可以再Spring MVC中使用Spring的强大功能
激活@Required @Autowired等注解 -->
<context:annotation-config/>

<!-- DispatcherServlet上下文,只搜索@Controller标注类 不搜索其他标注的类 -->
    <context:component-scan base-package="com.test">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


    <!--扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    <mvc:annotation-driven />
    
    <!-- 静态资源处理,css,js,images -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    
    <!--配置ViewResolver,可以有多个ViewResolver,使用order属性排序;InternalResourceViewResolver放在最后-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

</beans>







<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:annotation-config />

<!-- 不需要管理Controller了 -->
<context:component-scan base-package="com.test">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

</beans>








<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>HelloSpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloSpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
   <properties>
      <spring.version>4.1.1.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloSpringMVC</finalName>
  </build>
</project>

猜你喜欢

转载自blog.csdn.net/xhf852963/article/details/80792418