SpringMVC学习一:SpringMVC的配置

SpringMVC的配置主要分为两部分:

1、xml文件配置

2、注解的配置

 SpringMVC配置的步骤如下:

1、在将SpringMVC的jar包导入到web项目中后,先配置web.xml 文件。配置分发器使得请求通过Spring来控制分发

<servlet>
      <servlet-name>springMVC</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath*:config/spring-servlet.xml</param-value> 
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springMVC</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

<param-value>:这个选项是配置Spring配置文件的路劲以及名字。这里文件的位置是在src目录下的config目录下 如果这样写/WEB-INF/config/spring-servlet.xml则Spring的配置文件就是在WEB-INF目录下的config目录中的spring-servlet.xml文件。

<url-pattern>标识Spring拦截的请求的样式

2、配置好web.xml以后在进行Spring配置了。Spring配置有两种一种是配置xml形式的,另一种是配置注解形式

2.1xml形式的配置:

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
   
    <bean name="/test1/helloworld" class="com.HelloWorldContrller" />
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 </beans> 

bean的name就是controller的访问路径,class便是controller类的完整路径。每个Controller类都要实现Controller接口并重写其方法handleRequest。默认都是执行该方法。

在Spring MVC中,当Controller将请求处理结果放入到ModelAndView中以后,DispatcherServlet会根据 ModelAndView选择合适的视图进行渲染。那么在Spring MVC中是如何选择合适的View呢?View对象是是如何创建的呢?答案就在ViewResolver中,ViewResolver接口定义了 resolverViewName方法,根据viewName创建合适类型的View实现。

 2.2 注解方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
      
	<!-- 默认扫描的包路劲 -->
	<context:component-scan base-package="com" />  
	 <!-- 添加注解驱动 -->  
    <mvc:annotation-driven />  
    <!-- 定义跳转的文件的前后缀   -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
	
 </beans>  

 配置Spring使用注解的方式时需要添加Spring扫描的包路径以及添加注解驱动。配置完成之后,便可以使用注解的方式了。

原文地址:https://www.cnblogs.com/fsh1542115262/p/3798304.html