SpringMVC framework | SpringMVC built environment


First, set up the environment SpringMVC

1. Import jar package

Since seamless SpringMVC title Spring, directly into the jar package prior to the Spring Framework.

Here Insert Picture Description

4. The front-end controller

  • *.doAnd *.action: the request url to .do end or .action, SpringMVC will be parsed frame.
  • / : All requests will be parsed pringmvc, can cause static resources can not access the support RestFul development style.
  • /* : Intercepts all requests, springmvc JSP will be resolved, resulting in can not access the JSP Do not use this approach.

If you do not use a browser to access the front controller configuration of xx.do, DispatcherServlet will not be intercepted, the equivalent of no use SpringMVC framework, will direct 404.

<?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>SpringMVC01</display-name>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载SpringMVC配置文件 -->
		<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

3. Configure SpringMVC

  • Processor (handwriting).
  • Processor mapper. (The annotation mode, the processor and the mapper @Controller annotations match; @RequestMapper match the request url.)
  • Processor adapter. (Paired processor adapter processor mapper.)
  • View resolver.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd ">
	
	<!-- 处理器(手写) -->
	<bean class="com.gql.springmvc.UserController"></bean>
	
	<!-- 处理器映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	
	
	<!-- 处理器适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>		
	
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
	
</beans>

4. The processor handwritten

SpringMVC syntax compared with the servlet

SpringMVC servlet
mv.addObject("msg","hello world"); request.setAttribute("msg","hello word!");
mv.setViewName("index.jsp"); request.getRequestDispatcher(路径).forward(request,response);
  • @RequestMapping ( "/ hello"): is matched url path.
package com.gql.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 类说明:
 *		处理器
 * @guoqianliang1998.
 */
@Controller
public class UserController {
	
	@RequestMapping("/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello world!");
		mv.setViewName("index.jsp");
		return mv;
	}
}

5. Jump page

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
	<meta http-equive="content-type" content="text/html,charset=utf-8"/>
  </head>
  
  <body>
    ${msg}
  </body>
</html>

After the success of open access tomcat:
Here Insert Picture Description
The first test was successful SpringMVC program.

Second, few details

1. The front controller loads the default path

<?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>SpringMVC01</display-name>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载SpringMVC配置文件 -->
		<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

The above front controller using a <init-param>标签specified path (classpath: springmvc.xml) loading springmvc.xmlfiles. If not specified, SpringMVC will go to look for a file called the WEB-INF directory "servlet-name"+"-servlet.xml"of the file. The springMVC in the configuration file on it.

2. Simplified configuration SpringMVC

SpringMVC configuration can be abbreviated , which requires increased schema constraint, then use <mvc:annotation-driven />the 处理器映射器and 处理器适配器merge.
Further, the use of prefix and suffix view resolver mating processor may also be such that more standardized Java code.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
	
	<!-- 处理器(手写) -->
	<context:component-scan base-package="com.gql.springmvc02"></context:component-scan>
	
	<!-- 代替处理器映射器和处理器适配器 -->
	<mvc:annotation-driven />
 
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 
			prefix	前缀
			suffix	后缀
			物理视图地址 = prefix + 逻辑视图名(handler的返回值) + suffix 
		 -->
		<property name="prefix" value="/WEB-INF/user/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
Published 418 original articles · won praise 1088 · Views 240,000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/104340387