Multiple View resolvers in spring mvc - spring

I want to use multiple view resolvers in my web app based on spring mvc
Can anyone tell me how do I achieve that.
I want to use both JSP and freemarker in my app.
Please suggest some approaches or links or examples..
All help is appreciated.
Adhir

You can add as many view resolvers as you want. You can specify the order in which the view resolvers need to be checked. Spring will take the first view resolver which can successfully resolve the view.
ex:
Since you have JSP and freemarker add the view resolvers for both and give the order property 1 for JSP and 2 for freemarker.
If your view is /freemarker/hello.ftl then the JSP resoplver will fails since it will not be able to find th file /freemarker/hello.ftl, then the freemarker resolver will handle this view. But if the JSP resolver is able to find the file and resolve it then freemaker resolver will not be used to resolve that view
Refer: Chaining ViewResolvers

Related

Is there a way to retrieve data from redis in my jsp pages?

I am creating a spring boot server, and I would like the initial page (index.jsp) to be a datatable with information coming from the redis database. Anyone know how to do this with javascript or inside jsp pages?
First of all JSPs are outdated technology so I recommend you to go for thymeleaf which is a perfect choice with spring boot instead of JSP.
JAVA approach :
Just create a mapping with / base url and fetch Redis data using normal approach and use it in UI.
GetMapping("/")
public String index(){
//Fetch Redis data here add into model attributes
return "index";
}
JavaScript/inside JSP approach :
You can create a mapping method with some url like above and make an AJAX call on page load from the JSP page and get the JSON format data and use it.
Don't forget to put #ResponseBody annotation on top of the method which will return the data(JSON) instead of the page.
Refer call ajax to spring controller

Spring MVC insert URL to another endpoint in the view

I'm migrating a Play! 1.2 web application and moving to Spring Boot + Spring MVC. Some views contain URLs to other endpoints. For example, I display the book title on the page and next to it I want to add the URL to go the book's details page (e.g. localhost/books/{id}).
In Play! 1.2 the controllers are static, and there is also a Router which can create the full URL for a method belonging to another controller (Router.getFullUrl("BookController.bookDetails", args)), but how do I achieve this with Spring MVC?
Best regards,
Cristian.
If you are trying to get the app/deployed name automatically in .jsp files to make the urls, then please make use of context path. An example below :
<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>
From your requirement "admin.myapp.com","admin-test.myapp.com" are server names right? Something like http://admin.myapp.com/book/{bookId},http://admin-test.myapp.com/book/{bookId}. In Spring app, relative path in jsp can be accessed using pageContext.request.contextPath
I also found the UriComponentsBuilder and ServletUriComponentsBuilder. They are similar to the Play! Framework router and provide methods for building URI's, handling parameters and the query etc. We chose to annotate the controllers' methods using constants and then use the same constants with the UriComponentsBuilder to build back the path and create the request query for GET requests.

Is it mandatory to return the view name from controller in spring mvc?

I went through the basic Spring mvc code from this link
http://springinpractice.com/2008/05/05/build-a-shopping-cart-with-spring-web-flow-2-part-1/
Here I see that the controller method is blank & the request name & the jsp name is same. So automatically the jsp is rendered even when the controller is not returning the name. I don't understand this. Generally the controller is supposed to return the view name.
Can anyone explain this? Thanks
It simply an option that Spring offers. Instead of having to explicitely tell it that the view name is "home", you have the option of not telling what the view name is. If so, by convention, Spring will look for a view that has a name deduced from the request mapping.
See the documentation for details.

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

Spring MVC Load page automatically

I have a quick question about how to use the Spring MVC.
I just started with it and i was used to have pages that always had their data loaded calling some controller that you setted ON THE PAGE.. calling some methods on controllers to load objects at the page at load time.
Is it the same in Spring MVC? Cuz what im seeing so far is that if you want to load a page with data, you always have to use a modelandview object which is loaded by a method that you must call before... and them you return the modelandview object with its destination to the view that you want.
What im trying to know is if there is a way that the page requests some data from the controller before it gets loaded... automatically...
Dont know if im making my self clear... thanks anyway for the help!
in spring mvc, using Controllers, it is always Request->Controller(populates model and chooses view)->Response.
you could use ajax to load data from any resource (even Controllers) or jsp:useBean to explicitly call some business logic or jsp:include to include a certain fragment or a custom jsp tag.
I also face the same situation.
check the below link for getting the data from the controller before view(JSP) load.
how to load the data into index page

Resources