Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize web template rendering in SpringBoot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to achieve web template rendering in SpringBoot", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve web template rendering in SpringBoot" article.

Template

The essence of developing a Web site is to generate HTML code and return it to the browser (output) according to the request (input) initiated by the browser. In the previous study, we have stored it in a file rather than generating HTML code directly in Java code. On the other hand, blog sites are dynamic, meaning that different requests may return different content. However, for the same type of request, such as visiting two articles with id 1 and 2, the corresponding URL is / blogs/1 and / blogs/2, respectively, and the structure of the HTML code snippet returned by them is almost the same:

Cum sociis (blog title) Feb. 3, 2015 tag: Web Development

.. (here is the content of the blog)

Th:text= "${title}" tells the template engine to use the title variable as the content of the tag (the same is true of createdTime,content).

Note that in order to show the creation time of the blog, we put the time in a tag to distinguish it from other text.

Model

In order for the template engine to know the values of these variables, we need to do some more work on @ Controller:

@ RequestMapping ("/ index/ {id}") public String getIndex (@ PathVariable ("id") int id, Model model) {/ / return "index"; / / some data model.addAttribute ("title", "This is a blog with id =" + id); model.addAttribute ("CreatedTime", "2017-11-13"); model.addAttribute ("content", "This is content"); return "index" }

In the above code, the index () method adds a parameter of type Model. With the Model provided by the Spring MVC framework, you can call its addAttribute method so that Thymeleaf can access variables in Model for template rendering. As you can see in the above example, the value of the title variable is determined based on @ PathVariable in URL, which is simple, but it is already a dynamic page.

In Servlet programming, if you want to render information dynamically in a page, you generally need to add attributes to HTTPRequest and then get them in JSP. In fact, the properties of Model are actually placed in the properties of HttpRequest, but Spring MVC provides a higher-level abstraction to shield you from HttpRequest. All you see is M (that is, Model) directly in MVC.

If you still want to use native Servlet API objects such as HttpRequest,HttpResponse and HttpSession, add parameters of the corresponding type to the Controller method, you can use it directly in the method, and Spring MVC will pass you the correct object.

Running result:

Add objects to Model

In the above example, we have made the page of a single article dynamic, but this dynamic is just an example, and when we actually have hundreds of blog posts, we will add (or delete, update). Obviously, you can't populate Model like this directly in the @ Controller method, and it obviously won't work if you need to render the article list.

To solve this problem, we need to use the Blog class provided in the reference code JAR package:

Package Entity;import java.util.Date;public class Blog {private int id; private String title; private String content; private Date createdTime; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title } public String getContent () {return content;} public void setContent (String content) {this.content = content;} public Date getCreatedTime () {return createdTime;} public void setCreatedTime (Date createdTime) {this.createdTime = createdTime;}}

In a single article page, the Model.addAttribute () method needs to be called once for each attribute, and it will be inconvenient if there are many properties. Now that we have the Blog object, we can put it into Model:

@ RequestMapping ("/ index/ {id}") public String getIndex (@ PathVariable ("id") int id, Model model) {/ / return "index"; / / simulate some data here / / model.addAttribute ("title", "This is a blog with id =" + id); / / model.addAttribute ("CreatedTime", "2017-11-13"); / / model.addAttribute ("content", "This is content") Blog blog = new Blog (); blog.setId (1); blog.setTitle ("This is a blog with id =" + id); blog.setContent ("This is content"); blog.setCreatedTime (new Date ()); model.addAttribute ("blog", blog); return "index";}

Obtain the corresponding Blog object according to the id in URL, and then send it to the template engine to render the blog. The corresponding variable expression in the template will also be changed:

Cum sociis (blog title) Feb. 3, 2015 tag: Web Development

.. (here is the content of the blog)

Running result:

Improvement: there are two ways to add objects to Model:

Model.addAttribute ("blog", blog)

Model.addAttribute (blog)

When using the second method, the naming of the object in Model defaults to the lowercase form of the class name, and only one such "anonymous" object can exist for the same type at any time.

Date formatting

After the article page is rendered by the template, there is still a small problem: the date format. The rendering of ${blog.createdTime} is now Mon Nov 13 16:18:08 GMT+08:00 2017, because ${blog.createdTime} is a Date object, and the template engine calls its toString () method directly when rendering. Formatting dates is a very common task, for which Thymeleaf provides built-in support:

February 3, 2015 tag: Web Development

# dates is a built-in utility class for Thymeleaf, and the format () method specifies the format of the date.

Running result:

The above is about the content of this article on "how to achieve web template rendering in SpringBoot". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report