实现Spring MVC控制器
本教程描述了实现springmvc控制器的不同方法并给出了示例。
在我之前的教程中,使用STS创建简单的springweb应用程序,我向我们展示了如何构建一个引入控制器的springboot应用程序。本教程在上一个教程的基础上进行了扩展。
在开始实现之前,让我们快速概述一下控制器是如何参与MVC工作流的。
Spring MVC架构工作流
来自客户端的传入请求由DispatcherServlet解释
DispatcherServlet通过解析请求属性并使对象对处理程序可用来完成初始处理
确定并调用适当的处理程序以进一步处理请求。在适当的控制器上确定适当的方法
控制器处理请求并返回
ModelAndView
的实例
ModelAndView
由DispatcherServlet进一步处理,以将响应发送到客户端
在Spring Boot应用中启用JSP
如果要启用JSP,必须执行以下操作:
在
pom.xml
文件添加以下依赖项:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
在
src/main/resources/application.properties
把这两行加起来
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
创建文件夹
src/main/resources/META-INF/resources/WEB-INF/jsp/
把你的JSP文件放进去
实现Spring控制器返回JSP页面
下面的示例演示如何在Spring控制器方法中返回JSP页面。注意
@Controller
注释和
@RequestMapping
注释。如果我们想返回一个JSP页面,我们不使用
@ResponseBody
注释(如第二个示例所示)。
package net.theitroad.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@SpringBootApplication
public class ControllerExampleJSP {
@RequestMapping("/hellojsp")
String helloJSP() {
return("index");
}
public static void main(String[] args) {
SpringApplication.run(ControllerExampleJSP.class, args);
}
}
这个
@RequestMapping
注释指向URLhttp://localhost:8080/hellojsp到控制器的方法
helloJSP()
. 此方法返回
index.jsp
使用Spring控制器呈现JSP页面
带响应盒的执行控制器
与前面的示例不同,这个示例将返回由方法生成的字符串,而不是JSP页面。我们唯一需要改变的是
@ResponseBody
控制器方法的注释
package net.theitroad.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
public class ControllerResponseBodyExample {
@RequestMapping("/helloresponsebody")
@ResponseBody
String helloResponseBody() {
return("Hello World. This is produced by a method annotated with ResponseBody");
}
public static void main(String[] args) {
SpringApplication.run(ControllerResponseBodyExample.class, args);
}
}
呼叫http://localhost:8080/helloresponsebody在浏览器中将生成以下输出:
使用弹簧控制器和响应盒输出
实现Spring RestController
这个
@RestController
注释被用作方便的注释来表示注释,例如
@Controller
和
@ResponseBody
. 当在类级别使用时,控制器可以为restapi请求提供服务。
package net.theitroad.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class RestControllerExample {
@RequestMapping("/hellorest")
String helloRest() {
return("Hello World. This is produced by the rest conntroller method");
}
public static void main(String[] args) {
SpringApplication.run(RestControllerExample.class, args);
}
}
呼叫http://localhost:8080/hellorest将在浏览器中生成以下输出:
使用Spring RestController输出
在方法和类级别使用@RequestMapping注释
Spring4.3引入了注释,比如
@GetMapping
,
@PostMapping
,
@PutMapping
以此类推,为常见的HTTP方法类型(如GET、POST和PUT)指定映射。这些注释增强了代码的可读性。
下面的例子演示了如何在方法和类级别使用映射注释。
package net.theitroad.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user/*")
@SpringBootApplication
public class MethodAndClassLevelAnnotations {
@RequestMapping
String login() {
return("Login method called");
}
@GetMapping("/logout")
String logout() {
return("Logout method called");
}
public static void main(String[] args) {
SpringApplication.run(MethodAndClassLevelAnnotations.class, args);
}
}
向以下Url发出请求http://localhost:8080/用户/将调用
login()
方法。注意
@RequestMapping
注释
login()
方法没有参数。
这个
@RequestMapping("/user/*")
在类级别使用的注释作为catch all方法来处理由“/*”表示的具有不同路径的所有请求。
请求http://localhost:8080/user/logout将调用
logout()
方法。这个
@GetMapping
注释是一个组合注释,它的作用是
@RequestMapping(method = RequestMethod.GET)

