This article introduces an example of how to use JSP in SpringBoot. I will share it with you. The details are as follows:
rely:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Sample code:
@RequestMapping(value = "/register", method = RequestMethod.GET) @ResponseBody public String register(){ return "user register"; } /** @GetMapping is a new feature of Spring 4.3*/ @GetMapping("getUser") @ResponseBody public String getUser(){ return "user get"; } /** @PostMapping is also a new feature of Spring 4.3*/ @PostMapping("createUser") @ResponseBody public String createUser(){ return "user create"; } /** * @RequestParam Receives submitted parameters, the parameters are required by default * @RequestParam(value = "password", required = false) required = false, which may not be required * */ @PostMapping("buildUser") @ResponseBody public String buildUser(@RequestParam("username") String username, @RequestParam(value = "password", required = false) String password){ return "Submitted parameters: username" + username + " password:" + password; }Using JSP in SpringBoot
SpringBoot does not support JSP by default, and you need to add related dependencies to the project.
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> <artifactId>ecj</artifactId> <version>4.6.1</version> <scope>provided</scope> </dependency>
Add configuration items to the configuration file:
spring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.jsp
Login.java
@Controllerpublic class LoginController { @PostMapping("login") public String login(String username, String password){ if (username.equals(password)){ return "list"; } return "login"; } @GetMapping("form") public String from(Model model){ model.addAttribute("username", "tomcat"); return "form"; }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.