转载

Springboot-springmvc4.x

M:数据模型,是包含数据的对象。
V:视图页面,包含JSP、Thymeleaf
C:控制器(SpringMVC的注解@Controller的类)

快速搭建SpringMVC项目

一、介绍

SpringMVC提供了一个DispatcherServlet来开发Web应用,在SpringMVC中实现了WebApplicationInitializer接口就相当于实现了web.xml配置。

二、示例

1.pom文件

<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.eleven</groupId>
	<artifactId>springmvc4</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<!-- 定义变量 -->
	<properties>
		<java.version>1.7</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>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		<!-- 其它依赖 -->
		<!--配置servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<!--配置jsp jstl的支持 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		<!-- 使用SLF4J和LogBack作为日志 -->
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
		<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>
		<!-- 添加jackson依赖,获得对象和json或xml之间的转换 -->
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.5.3</version>
		</dependency>
		<!-- 实际开发中,没必要同时支持xml和json,只用json就可以 -->
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.3.5</version>
		</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.日志文件

在src/main/resources目录下,新建logback.xml用来配置日志

<?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>Logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>
    <!-- 配置级别为DEBUG,我们可以看到更详细的错误信息 -->
	<logger name="org.springframework.web" level="DEBUG"/>
	<root level="info">
		<appender-ref ref="console"/>
	</root>
</configuration>

复制代码

3.演示页面

在src/main/resources下建立views目录,并在此目录下建立index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<pre>
		MVC
	</pre>
</body>
</html>
复制代码

4.SpringMVC配置

为什么要放到/WEB-INF/classes/views/下面,因为在springboot中,将会使用Thymeleaf作为模板,因此不需要这样配置。

package com.eleven.config;

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

@Configuration
@EnableWebMvc
@ComponentScan("com.eleven.config")
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.Web配置

package com.eleven.config;

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 {// 替代web.xml

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(MyMvcConfig.class);
		ctx.setServletContext(servletContext); // 新建WebApplicationContext,注册配置类,并将其和当前servletContext关联
		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); // 注册SpringMVC的DispatcherServlet
		servlet.addMapping("/");
		servlet.setLoadOnStartup(1);
	}

}

复制代码

6.简单控制器

package com.eleven.config;

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

@Controller // 声明是一个控制器
public class HeloController {

	@RequestMapping("/index") // 利用该注解配置URL和方法之间的映射
	public String hello() {
		return "index"; // 说明我们的页面放置的路径为/WEB-INF/classes/views/index.jsp
	}

}

复制代码

7.运行

Springboot-springmvc4.x

常用的注解

一、@Controller

表明这个类是SpringMVC里的Controller,将其声明一个Bean,然后DispatcherServlet会自动扫描,并将Web请求映射到注解了@RequestMapping方法上。

二、@RequestMapping

映射Web请求(访问路径和参数),利用该注解配置URL和方法之间的映射

三、@ResponseBody

将java对象转为json格式的数据。

四、@PathVariable

接收请求路径中占位符的值

五、@RestController

组合了@Controller和@ResponseBody。

六、示例

1.添加jackson依赖,或得对象和json之间的转换

<!-- 添加jackson依赖,获得对象和json或xml之间的转换 -->
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.5.3</version>
		</dependency>
		<!-- 实际开发中,没必要同时支持xml和json,只用json就可以 -->
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.3.5</version>
		</dependency>
复制代码

2.Domain(对象)

用来演示获取request对象参数和返回次对象到response

package com.eleven.domain;

public class DemoObj {
	private Long id;
	private String name;

    // jackson:对象和json做转换时,一定需要空构造。
	public DemoObj() {
		super();
	}

	public DemoObj(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

复制代码

3.注解演示控制器

package com.eleven.controller;

import javax.servlet.http.HttpServletRequest;

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

import com.eleven.domain.DemoObj;

@Controller // 表示此类是一个控制器
@RequestMapping("/anno") // 访问路径是/anno
public class DemoAnnoController {

	/**
	 * @ResponseBody:将java对象转为json格式的数据。
	 * @param request
	 * @return
	 */
	@RequestMapping(produces = "text/plain;charset=UTF-8") // 返回的是媒体类型和字符集,需要的返回值是json对象
	public @ResponseBody String index(HttpServletRequest request) {
		return "url:" + request.getRequestURI() + " can access";

	}

	/**
	 * @PathVariable:接收请求路径中占位符的值
	 * @param str
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")
	public String demoPathVar(@PathVariable String str, HttpServletRequest request) {
		return "url:" + request.getRequestURI() + "can access,str" + str;
	}

	/**
	 * 表示request参数获取,访问路径为/anno/requestParam?id=1
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8")
	public @ResponseBody String passRequestParam(Long id, HttpServletRequest request) {
		return "url:" + request.getRequestURI() + "can access,id:" + id;
	}

	/**
	 * 参数到对象,访问路径为/anno/obj?id=1&name=xx
	 * 
	 * @param obj
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String passObj(DemoObj obj, HttpServletRequest request) {
		return "url:" + request.getRequestURI() + "can access,obj ud:" + obj.getId() + "obj name" + obj.getName();
	}
	
	/**
	 * 不同路径到相同的方法,路径为/anno/name1或/anno/name2
	 * @param request
	 * @return
	 */
	@RequestMapping(value= {"/name1","/name2"},produces="text/plain;charset=UTF-8")
	public @ResponseBody String remove(HttpServletRequest request) {
		return "url:"+request.getRequestURI()+"can access";
	}
	
}

复制代码
原文  https://juejin.im/post/5de904416fb9a01648715eff
正文到此结束
Loading...