SpringBoot MVC, Thymeleaf - spring-boot

Stack: SpringBoot, Spring MVC and Thymeleaf
I followed https://spring.io/guides/gs/serving-web-content/
I would like now to do a loop for which each iteration updates the ${name} in the view. (without calling multiple time the controller).
I would say I want to make the page/view dynamic? But I miss the vocabulary of it and therefor I can't find a proper way to do it.
Anyone knows how ?
See you thanks.

Send the list from your controller to the requested html page and then use th:each on your html page to populate the values.

Related

How to return a html in spring boot controller?

I'm really new to spring boot, this is my first time using it, I don't want to, but I have to, and I'm stuck. I have a spring boot app that displays elements of a database
#GetMapping
public List<Movie> getMovies(){
return movieService.getMovies();
}
I have this GetMapping, and when I load the page, it displays the element of the database, in JSON format, like this:
[{"id":1,"title":"Django","releaseDate":"2012-12-12","rating":8.4},{"id":2,"title":"Pulp Fiction","releaseDate":"1994-04-13","rating":8.9}]
How should I edit the GetMapping to returnl a html file, in which I display this data? Also, how should I use the html to use the getMovies function?
"Don't want to"? It's a great framework. You'll learn something.
The controller is mapped to a URL with an HTTP request and it returns an HTTP response. Looks like your HTTP client asked for the response as JSON, but it could also ask for plain text or XML.
Your result is correct - this is exactly what Spring controller should be doing.
You need to read a little bit more about Spring web MVC. You can create an HTML page template to render the data in the way you wish.
You could also create a React or Angular front end app that calls the Spring controller and renders the JSON that you got in an HTML page.
Spring web MVC would mean that the page is coupled with the controller as one app.
A React/Angular front end would keep it separate from the controller. You'd have two deployments instead of one.
I recommend using the Thymeleaf view engine for HTML pages if building a Spring MVC application. https://www.thymeleaf.org/documentation.html
Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Gradle dependency:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
You can put HTML pages/templates under main/resources/templates. In your controller you can return a ModelAndView (org.springframework.web.servlet.ModelAndView) object. See the doc for available methods.
I highly recommend Spring Boot for web applications, application backends, etc. Saves the tedious configuration of Spring and provides the tools needed to build an enterprise application adhering to industry best practices.

Spring Boot, do I even need a model with vuejs?

I'm having trouble understanding and then deciding how I want setup the frontend of my spring boot application. My question is do I even need thymeleaf or rather do I lose something if I only use vuejs?
First things first I am mostly into backend stuff and barely know a lot about current frontend technologies etc. However I tried out vuejs and liked it a lot.
I used to use thymeleaf and serve static html pages with thymeleaf markup. After playing around a bit with vuejs I want to use that instead.
So far I've managed to set it up two ways:
I keep thymeleaf and use it together with vuejs. This allows me to keep the model that I can pass in my view controllers.
I only use vuejs and get all information through REST from the spring boot app. However I am asking myself if there isnt a way to access the model from here? If that would be the case I wouldnt need thymeleaf right?
I really think that I'm having some kind of architectural misunderstanding and would appreciate it if I could get it cleared up.
Thank you in advance

Spring Web MVC and recurring dynamic page elements

I'm new to java and web apps and after trying out a few things I went with a set up of Spring webmvc using annotations and velocity as templating engine. It's not that hard to do simple #RequestMapping annotations to controller methods and returning ModelAndView instances filled with data, however I was wondering how things are done when you have data you need in the model that occurs on every page, for example "latest 5 news items" or something similar. You could of course always fill the model with such data in every method that is handled by a #RequestMapping, but I'm quite sure that that is not the way to do it.
What is the correct way of filling the model with recurring data, without poluting your controller methods with calls to the same method for this recurring data.
Any help is appreciated.
You could use a servlet filter or a Spring interceptor, and get the recurring data from this filter or interceptor and place it in a request attribute.
Another solution is to let the page call more than one controller, for example by using multiple ajax requests. Then one controller could is responsible for the specific page and another is responsible for "latest 5 news items", see related question.
Good question dude. In my current app i am using sessions to store my username which is appearing on all the rest of the app.
#JB Nizet thanks for the link.. now ill go for spring interceptor

Spring render many many view for one page

Hello I am using spring mvc and my page consist of 2 columns left and right. I want that left side is rendered from one controller and right side is rendered another one. How to accomplish that. Fore example in the right side here is login form which renders from LoginController but the left side changes all time. How to accomplish it with spring mvc framework.
thanks in advance.
Spring MVC is not inherently a component-oriented framework, like you seem to want. Some different approaches that might suit you
Client-side aggregation. Load your different "components" (left, right) using AJAX
Using some more template/component-oriented rendering framework. I like Apache Tiles and breaking out logic in Preparers (analogous to Controllers in Tiles). Or you can use JSF 2. I wrote a bootstrap/tutorial on Spring MVC 3 and JSF 2 a while back that might be interesting for you.
Go full-on with a component-oriented framework like JSF 2, Tapestry or Wicket.
There are various ways to implement this. What are your exact requirements? You can try:
Portlets - render two portlets, each is rendered separately. Probably an overkill if this is needed only in this one place.
AJAX - Render an empty page first and fetch both views separately using AJAX. See $.load().
<iframe/> - embed iframes with two different target URLs.
Use component-oriented framework (I personally like Wicket).
None of these approaches are tied Spring MVC or even Java.

Spring MVC 3 and Ajax library advice

I'm developing a webapp with Hibernate+Spring 3 (Spring MVC, JSP): I'd like to create some divs with AJAX style (i.e. no need to refresh all the page, independent update of each div).
I'd like a good advice about which AJAX library to use (in conjunction with Spring 3 MVC + JSP) and, if possible, where to find some code snippets.
I know very little of AJAX libraries, JSONs and how to integrate them, but I have good knowledge of Javascript and Spring (and how callbacks work). I'd like to write as less code as possible, particularly in the jsps.
My Webapp will display an updated (every 5 minutes) POJO in a div and perform some operations between different domain objects in the other div when user press a button.
This is correct use JQuery
Here
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
you can find working examples to use Jquery+JSON+Spring MVC.
and this question can help you with server side configuration:
JQuery, Spring MVC #RequestBody and JSON - making it work together
I would suggest jQuery. It is very easy to use and has very good ajax support.
In addition to that it has quite a lot of plugins and components.

Resources