Springboot jump method introduction

The whole is divided into two categories:

1. The data is returned directly after the request is completed.

No need to configure template dependencies in pom.xml

Just return the data directly in the controller, for example:

package org.xueqi.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xueqi.demo.service.DeptService;

@Controller
@RequestMapping("jpa")
public class JpaDemoController {

	@Autowired
	private DeptDao deptDao;

	@RequestMapping("savdept")
	public DeptEntiy saveDept () {
		DeptEntiy dept = new DeptEntiy();
		dept.setName("Information Center");
		System.out.println("----------------------");
		return DeptEntiy;
	}
}

2. Jump to the page after the request is completed.

Template dependencies need to be configured in pom.xml, which is supported in springboot

  1. FreeMarker
  2. Groovy
  3. Thymeleaf
  4. Velocity (1.4 no longer supported)
  5. Mustache

The configuration information that each template depends on is different. Here is an example of Thymeleaf, and the following information is added to the xml

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		    <version>1.5.11.RELEASE</version>
		</dependency>

Then create your html file under the src/resource/templates file, for example: there is an index.html under src/resource/templates

Then edit it like this in the controller:

	@RequestMapping("html")
	public String goHtml() {
		return "index";
	}

Here the system will directly jump to index.html under the src/resource/templates file. Note that the string in return "index"; must be the same as the html file name, otherwise it will report 404 that the corresponding page cannot be found mistake

3. Use page templates and return data

Here is the same except that the controller is different from the above. The code of this controller is as follows:

	@RequestMapping("html")
	public ModelAndView goHtml() {
		List<DeptEntiy> lst = deptDao.findAll();
		ModelAndView model = new ModelAndView("users");
		model.addObject("deptLst",lst);
		return model;
	}

The returned type has changed from the original String to ModelAndView, and a new ModelAndView has been created to store our data and views, where

The users in new ModelAndView("users") corresponds to our html file name

mode.addObject("deptlst",lst) corresponds to the data we return to the page, return model;

The code for the page is as follows:

<!DOCTYPE html>  
<html xmlns:th="http://www.thymeleaf.org">  
<head>  
<meta charset="utf-8" />  
<meta name="viewport" content="width=device-width, initial-scale=1.0" />  
<title>springboot学习</title>  
</head>  
<body >
	<!-- view -->
	This is the HTML view
	hahha
	<div th:each="item : ${deptLst}">  
        //item is a single entity object in the list collection, item.name is the attribute name in the entity
        <p><a th:text="${{item.name}}"></a></p>  
    </div>  
</body>
</html>


4. These are not the main points I want to make, what I want to say is a special case:

When I was learning springboot, I created a project myself. At that time, thymeleaf dependency library was not introduced, but I can also use and access html. How to do this?

First: Modify the templates under the project src/resource/ path and rename it to public

Second: create an index.html under public

The disadvantage of this is that the controller cannot use return to jump to the page in the future, but requires us to use the ajax access technology in the page to reload the page. (mainly refers to the use of angular.js for server access and value transfer)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326460401&siteId=291194637