一、SpringMVC组件说明
DispatcherServlet(前端控制器):相当于MVC中的controller,dispatcherServlet是整个流程控制的中心,由他调用其他组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性。
HandlerMapping(处理器映射器):负责根据用户的请求找到相应的Handler(处理器),springMVC提供了不同的映射器实现了不同的映射方式,例如:配置文件方式,注解方式。
Handler(处理器):在DispatcherServlet的控制下,handler对具体的用户请求进行处理。
HandlerAdapter(处理器适配器):适配器模式,通过扩展适配器,可以对更多类型的handler进行控制。
ViewResolver(视图解析器):负责将处理结果生成视图(View),SpringMVC提供了多种视图类型,即:jstlView、freemarkerView、pdfView等。
二、MainApplication
package demo.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
三、MainApplication下的Controller
package demo.boot.controller;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import demo.boot.entity.User;
@Controller
@RequestMapping("test") //访问test下的方法,地址栏:http://127.0.0.1:8080/test/t1
public class TestController {
@RequestMapping("t1") //访问名
@ResponseBody
public String test1(){
return "Hello World";
}
}
四、pom.xml配置
使用时需修改:
<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>
<!-- Begin##01、添加SpringBoot的parent支持##Begin -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- End##01、添加SpringBoot的parent支持##End -->
<groupId>demos.springboot</groupId>
<artifactId>hello_boot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<!-- Begin##02、添加SpringBoot的Web支持##Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- End##02、添加SpringBoot的Web支持##End -->
<!-- Begin##添加JSP支持##Begin -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- End##添加JSP支持##Begin -->
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--跨服务器上传 -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 设置jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Begin##03、添加SpringBoot的插件支持##Begin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- End##03、添加SpringBoot的插件支持##End -->
</plugins>
</build>
</project>