Spring MVC配置

来源:互联网 发布:linux jdk 安装 编辑:程序博客网 时间:2024/06/03 20:38

web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC0107</display-name>
  
  <!-- 使用ContextLoaderListener配置时,需要告诉它Spring配置文件的位置 -->  
    <!-- 如果没有指定,上下文载入器会在/WEB-INF/applicationContext.xml中找Spring配置文件 -->  
    <!-- 我们可以通过在Servlet上下文中设置contextConfigLocation参数,来为上下文载入器指定一个或多个Spring配置文件 -->  
    <!-- 注意:contextConfigLocation参数是一个用逗号分隔的路径列表,其路径是相对于Web系统的根路径的 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/springmvc-servlet.xml, classpath:applicationContext-*.xml</param-value>  
    </context-param>
  
  <!-- 添加spring3控制器及映射规则 -->
  <!-- SpringMVC的前端控制器(即总的控制中心) -->  
    <!-- 当DispatcherServlet载入后,它将从一个XML文件中载入Spring的应用上下文,该XML文件的名字取决于<servlet-name> -->  
    <!-- 这里DispatcherServlet将试图从一个叫做springmvc-servlet.xml的文件中载入应用上下文,其默认位于WEB-INF目录下 -->  
    <!-- 所以ContextLoaderListener参数值也可写成<param-value>classpath:applicationContext-*.xml</param-value> -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- servlet映射,将所有已.action结尾的请求都通过控制中心 -->
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <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>
</web-app>

====================================================================================================

====================================================================================================


springmvc-servlet.xml

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <!-- 【配置映射处理器】 -->
    <!-- SpringMVC自带的映射处理器均实现了Spring的Ordered接口,这意味着我们可以在应用系统中声明多个处理器映射 -->  
    <!-- 并且可以设置它们的优先级,这主要体现在order属性上,其值越小,则对应的该映射处理器的优先级越高 -->  
    <!-- 本例中,SimpleUrlHandlerMapping的order值比BeanNameUrlHandlerMapping的小 -->  
    <!-- 这意味着DispatcherServlet在映射URL的时候,首先会咨询SimpleUrlHandlerMapping -->  
    <!-- 只有在SimpleUrlHandlerMapping无法返回结果时,才咨询BeanNameUrlHandlerMapping -->  
<!--     <bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">  
        <property name="order" value="1"/>  
    </bean>   -->
    <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
        <property name="order" value="0"/>  
        <property name="mappings">  
            <props>  
                <prop key="/hello.action">helloController</prop>  
                <prop key="/start.action">startController</prop>  
            </props>  
        </property>  
    </bean>
    
    <!-- 这里的id="startController"对应的是<bean id="simpleUrlMapping">中的<prop>里面的value -->  
    
    <bean id="startController" class="com.test.StartController"/>
    <bean id="helloController" class="com.test.HelloController"/> 
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>
0 0
原创粉丝点击