Spring MVC(全配置模式)

spring mvc 整体架构及工作流程

核心组件:

  • DispatcherServlet (前端控制器, 处理请求的入口,spring mvc的核心,一般在服务器启动时初始化)

  • HandlerMapping (映射器对象, 用于管理url与对应controller的映射关系)

  • Interceptors(拦截器,实现请求响应的共性处理)

  • Controller (后端控制器, 负责处理请求的控制逻辑)

  • ViewResolver(视图解析器,解析对应的视图关系:前缀+view+后缀)

基于xml方式创建Maven WEB 项目

1. 创建maven web项目

1.1 生成web.xml

1.2 配置Tomcat

1.3 设置编译版本 

2. 添加Spring MVC 项目依赖及配置文件

2.1 配置pom.xml文件

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.3.9.RELEASE</version>
	</dependency>
</dependencies>

2.2 在项目的resources的目录中添加核心配置文件spring-configs.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
	xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
	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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">

</beans>

3. 配置前端控制器

打开web.xml,配置DispatcherServlet对象

<?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"
	version="2.5">
	<display-name>Spring-MVC-01</display-name>
	<servlet>
		<servlet-name>frontController</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- init-param 中的参数名不能变(此名字在DispatcherServlet父类中定义) -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-configs.xml</param-value>
		</init-param>
		<!-- 让tomcat启动时即加载时便初始化servlet -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>frontController</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

4. 创建后端控制器

后端控制器编写时可以实现Controller接口,然后重写handleRequest方法处理请求

package com.jt.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class XmlHelloController implements Controller {
	/** 通过此方法处理客户端请求 */
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "hello cgb1805");
		return mv;
	}

}

5. ​​​​​​​创建JSP页面对象

在项目的WEB-INF/pages文件夹下创建hello.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>first spring mvc example</h1>
	<h1>${msg}</h1>
</body>
</html>

6. 配置后端控制及视图解析

在spring mvc 核心配置文件spring-configs.xml中添配置

<!-- 配置 后端 controller对象 -->
<bean id="helloController" class="com.jk.controller.XmlHelloController" />
<!-- 配置HandlerMapping(定义url到controller之间的映射) -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="UrlMap">
		<map>
			<entry key="/hello.do" value="helloController" />
		</map>
	</property>
</bean>
<!-- 配置视图解析器 /WEB-INF/pages/hello.jsp -->
<bean id="viewResolver"
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/pages/" />
	<property name="suffix" value=".jsp" />
</bean>

猜你喜欢

转载自blog.csdn.net/qq_40725867/article/details/81748554
今日推荐