SpringMVC搭建(注解和xml配置两种方式)

SpringMVC执行流程

SpringMVC搭建(注解和xml配置两种方式)_第1张图片

SpringMVC搭建(注解和xml配置两种方式)_第2张图片

方式一 (xml配置,不推荐):

web.xml配置的内容:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>spring-11-mvcdisplay-name>
	
	<servlet>
		
		<servlet-name>springmvcservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet
		servlet-class>
		
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc-servletdemo.xmlparam-value>
		init-param>
		
		<load-on-startup>1load-on-startup>
	servlet>
	
	
	<servlet-mapping>
		<servlet-name>springmvcservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
web-app>

springmvc.xml的配置


<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">
	
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
	
	<bean
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">bean>
	
		
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="InternalResourceViewResolver">
		<property name="prefix" value="/">property>
		<property name="suffix" value=".jsp">property>
		bean>
		
		
		bean>
beans>


方式二(注解实现):

web.xml中的必要配置:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	
	
	
	<servlet>
		<servlet-name>SpringMVCservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc-servlet.xmlparam-value>
		init-param>
	servlet>
	<servlet-mapping>
		<servlet-name>SpringMVCservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
 	
web-app>

springmvc.xml中的配置:


<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:component-scan base-package="com.kuang.controller"/>
	
	
	<mvc:default-servlet-handler/>
	
	
	
	<mvc:annotation-driven/>
 	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/">property>
		<property name="suffix" value=".jsp">property>
		bean>
		 
beans>


  • SpringMVC搭建(注解和xml配置两种方式)_第3张图片
    SpringMVC搭建(注解和xml配置两种方式)_第4张图片
    SpringMVC搭建(注解和xml配置两种方式)_第5张图片

你可能感兴趣的:(spring,spring,mvc,web.xml,后端)