Spring Boot Controller
In the previous article, we have created a HellController in the HelloWorld program, which contains the JSON response method. This article will explain it to the Controller.
In the previous article, we used the @ RestController annotation in the Controller, which was introduced by Spring 4.0. View the source code to see that it contains the @ Controller and @ ResponseBody annotations. We can understand it as an enhanced version of @ Controller. Specifically designed to respond to the content-based Controller, the response object can be directly JSON.
@ Controller is used to respond to pages. spring-boot supports multiple template engines, including:
1, FreeMarker
2, Groovy
3. Thymeleaf (this is used on the Spring official website)
4, Velocity
5. JSP (it seems that Spring Boot is not officially recommended. The projects created by STS will have a templates directory under src/main/resources. Here, let's put the template file, then, the webapp directory such as SpringMVC is not generated)
However, this article uses JSP, which everyone is familiar with, for example, because JSP and default supported templates require special processing, it is better to use examples.
The Controller method can receive Parameters Using @ RequestBody, @ RequestParam, @ ModelAttribute, JSONObject, HttpEntity, and other methods, which are the same as Spring.
Next let's take a look at how to use @ Controller to implement the response JSP page (the same as SpringMVC ).
Create a PageController and encode it as follows:
Package org. springboot. sample. controller; import java. util. date; import java. util. map; import org. springframework. beans. factory. annotation. value; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. servlet. modelAndView; @ Controllerpublic class PageController {// from application. pr Operties reads the configuration. If the default Value is not obtained, it is Hello Shanhy @ Value ("$ {application. hell: Hello Shanhy} ") private String hello =" Hello Shanhy ";/*** response page * @ RequestMapping ("/") there is a difference between @ RequestMapping and @ RequestMapping. * if no parameter is specified, the global hosts page is displayed. When you enter the 404 page, this page is automatically accessed. * If the parameter "/" is added, it is regarded as the root page only. ** @ Return * @ author SHANHY * @ create January 5, 2016 */@ RequestMapping (value = {"/", "/index"}) public String index (Map
Model) {// returns a string directly. The framework will go to spring by default. view. in the prefix directory (index splicing spring. view. suffix) page // This example is/WEB-INF/jsp/index. jsp model. put ("time", new Date (); model. put ("message", this. hello); return "index";}/*** responds to the JSP page page1 ** @ return * @ author SHANHY * @ create January 5, 2016 */@ RequestMapping ("/page1 ") public ModelAndView page1 () {// page location/WEB-INF/jsp/page. jsp ModelAndView mav = new ModelAndView ("page/page1"); mav. addObject ("content", hello); return mav;}/*** responds to the JSP page page1 (you can directly use the Model to encapsulate the content and directly return the page string) ** @ return * @ author SHANHY * @ create January 5, 2016 */@ RequestMapping ("/page2") public String page2 (Model model) {// page locations/WEB-INF/jsp/page. jsp model. addAttribute ("content", hello + "(Type 2)"); return "page/page1 ";}}
Add dependency in pom. xml:
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
provided
javax.servlet
jstl
As mentioned above, JSP is not recommended for spring-boot. To use JSP, You need to configure application. properties.
Add src/main/resources/application. properties:
# Default page prefix directory spring. mvc. view. prefix =/WEB-INF/jsp/# response page default suffix spring. mvc. view. suffix =. jsp # custom attributes, which can be read from the Controller. hello = Hello Shanhy
Create the webapp/WEB-INF/jsp directory under src/main to store our jsp page.
Index. jsp
<%@ page="" language="java" pageencoding="UTF-8">
Time: ${time} Message: ${message}
Page1.jsp
<%@ page="" language="java" pageencoding="UTF-8"><%@ page="" language="java" pageencoding="UTF-8">
${content }
To enable spring-boot to support JSP, compress the project into a war package.
Modify the pom. xml file and change the jar file to war.
Then start the spring-boot service.
View the results on the access page:
Http: // localhost: 8080
Http: // localhost: 8080/page1
Http: // localhost: 8080/page2
The attached project structure is as follows:
If you want to use FreeMarker, follow the instructions below to modify the settings:
1. Remove the spring. mvc. view. prefix and spring. mvc. view. suffix configurations in application. properties.
2. Place the. ftl template page in resources/templates (as shown in the project above)
Here, error. ftl is the default system error page with custom content.
3. Modify pom. xml as follows:
<%@ page="" language="java" pageencoding="UTF-8"><%@ page="" language="java" pageencoding="UTF-8">
4.0.0
org.springboot.sample
spring-boot-sample
0.0.1-SNAPSHOT
jar
spring-boot-sample
Spring Boot Sample Web Application
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-maven-plugin
<% @ Page = "" language = "java" pageencoding = "UTF-8"> <% @ page = "" language = "java" pageencoding = "UTF-8">