MVC:model+View+Controller(数据模型+试图+控制器)
三层架构:Presentation tier +Application tier+ Data tier(展现层+应用层+数据访问层)


实际上mvc只存在三层架构的展现层,M实际上是数据模型,是包含数据对象。在springmvc中,
有一个专门的类叫Model,用来和V之间的交互、传值;V指的是试图页面,包含jsp,freemakrer、Thymeleaf、等;
C指的事控制器(Spring mvc含有 注解@controller的类)


在说三层架构,三层架构事整个应用的架构,是由spring框架负责管理的。一般项目结构中都有Service层、DAO层,
这两个反馈在应用层和数据访问层。
学习目的:

springmvc使我们可以简单地开发出灵活且松散耦合的web项目。基于注解和Java配置的零配置(无xml)的springmvc开发。

一:springmvc项目快速搭建:
基于servlet3.0+无web。xml配置。

1.maven下的依赖包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spring.springmvc</groupId>
	<artifactId>springmvc</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<!-- Generic properties -->
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- Web -->
		<jsp.version>2.2</jsp.version>
		<jstl.version>1.2</jstl.version>
		<servlet.version>3.1.0</servlet.version>
		<!-- Spring -->
		<spring-framework.version>4.1.5.RELEASE</spring-framework.version>
		<!-- Logging -->
		<logback.version>1.0.13</logback.version>
		<slf4j.version>1.7.5</slf4j.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-web-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency>

		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- 其他web依赖 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- 使用SLF4J和LogBack作为日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>${logback.version}</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-access</artifactId>
			<version>${logback.version}</version>
		</dependency>

		<!--对json和xml格式的支持 -->
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.5.3</version>
		</dependency>
		<!-- file upload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- 非必需,可简化IO操作 -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.3</version>
		</dependency>
		<!-- spring 测试 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-framework.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2.日志配置


将org.springframework.web包下类的日志级别设为debugger,springmvc经常出现的参数类相关的4XX错误,设置后,会看到更详细的错误信息

src/main/resources下

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1 seconds">

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <jmxConfigurator/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

      <logger name="org.springframework.web" level="DEBUG"/> <!-- 1 将org.springframework.web包下类的日志级别设为debugger,springmvc经常出现的参数类相关的4XX错误,设置后,会看到更详细的错误信息-->
    <root level="info">
        <appender-ref ref="console"/>
    </root>
</configuration>

3.页面配置


src/main/resources/views/index.jsp //index.jsp


关于index.jsp位置:(遵从)spring boot的页面习惯配置方式是放在src/main/resources下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<pre>
		Welcome to Spring MVC world
	</pre>
</body>
</html>

4.spring mvc配置

viewResolver.setPrefix("/WEB-INF/classes/views/");说明:项目运行时会将页面自动编译到/WEB-INF/classes/views/下,
而不是开发时的路径。

package com.boot.springmvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * 	@Bean
	public InternalResourceViewResolver viewResolver();
 * 注入InternalResourceViewResolver类:
 * 说明:springmvc下有一个接口叫ViewResolver,(我们的viewResolver都实现该接口),实现这个接口要重写
 * resolverName(),这个方法的返回值接口View,而view的职责就是使用model、request、response对象,并
 * 渲染视图(不一定是html、可能是json、xml、pdf等)给浏览器 。
 *
 */
@Configuration
@EnableWebMvc // 1开启默认配置
@EnableScheduling
@ComponentScan("com.boot.springmvc")
public class MyMvcConfig {
	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/classes/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
		return viewResolver;
	}
}

5.使用servlet3.0+ 无web。xml配置。


①WebApplicationInitializer:spring提供用来配置Servlet3.0+配置的接口,从而替代web.xml的配置方式,实现此接口将会自动被springServletContainerInitializer(用来启动Servlet3.0容器)获取到。


②新建WebApplicationContext,注册配置类,并将其和当前ServletContent关联。


③注册spring mvc的DispatcherServlet


package com.boot.springmvc;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInitializer implements WebApplicationInitializer {// 1

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(MyMvcConfig.class);
		ctx.setServletContext(servletContext); // 2

		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); // 3
		servlet.addMapping("/");
		servlet.setLoadOnStartup(1);
		servlet.setAsyncSupported(true);// 1

	}

}

6.spring mvc中控制器配置

package com.boot.springmvc.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller//1声明该类是一个控制器
public class HelloController {
	
	@RequestMapping("/index")//2配置url和方法之间的映射
	public  String hello(){
		
		return "index";//3通过配置的viewResolver,返回index页面(/WEB-INF/classes/views/)
	}

}

测试(基础配置不适用开发,看看就行。)

springmvc-0.0.1-SNAPSHOT.war

springboot 配置mvc端点 springboot mvc架构_spring