Spring MVC + Thymeleaf - correct context path handling - java

I'm developing web application using Spring MVC and Thymeleaf. What is the correct way to make links of pages work regardless of where the application deployed? For example, if I write
Link
and deploy my application not to /, but in /app/, link will not work.
I thought about getting current path in every Controller method like this: request.getContextPath() and than using it in every link on the page, but maybe there is a way to do it better?

If you are using Thymeleaf, you should be able to use <a th:href="#{/about}">Link</a> assuming that <html xmlns:th="http://www.thymeleaf.org"> has been declared. I think this does require using SpringTemplateEngine.

Related

Spring equivalent of tilde (~) for routing from ASP.NET?

Sorry in advance if this is something trivial
In ASP.NET I remember you could direct your routes to
Login
and you didn't have to worry about anything URL related (if your project is on localhost:1234 or localhost:1234/myproject/ or whatever), ASP.NET would do the job for you.
Is there an equivalent for that in Spring. Currently when I start up my project, using GlassFish, it starts up at url localhost:8080/myproject and the resources (css, js...) aren't even being loaded until I add another / in the end (localhost:8080/myproject/).
All my routes are on the first URL parameter and my GET variables aren't even RESTful, simply because if I go one / more, my routing is going to go wrong, ie.
Home //will go to localhost:8080/home (not the project scope)
Home //is fine until I go to another / in url...
//...(/foo/bar), then it goes all the way up
//to localhost:8080/foo/home
I tried to google this, but every time I try to google something equivalent to ASP.NET, I just get a lot of ASP.NET tutorials (nothing Spring related).
So.. is there any way to keep the url consistent, something in the lines of:
Login
or
Login
How is it normally done in (commercial) applications? What's the best practice in this?
You need to use the Spring Tag library.
First import the tag library in your JSP:
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
Now, instead of writing a raw URL, you use <spring:url>, for example:
Login
Or you can assign the value to a variable and reference it:
<spring:url value="user/login" var="login"/>
Login
Which you prefer is a matter of preference.
The tag will then work out the web application context, etc, and replace <spring:url ... with the full URL.

Portlet jsp + jquery datatable +servlet implementation

I have created a liferay portlet using mvc (jsp ) and i'm using JQuery for datatable.
I have succeeded to bring a list and display it on a table using Jquery.
Now, I'm trying to make it editable to use CRUD functions.
The problem that i'm using java not php and i couldn't assign a servlet function (doget).i didn't understand it well (how to use it) and they set the url of this class to jquery.
If i'm not wrong this would be configured in web.xml (servlet mapping). So i wonder how to properly configured and use it?
Also, i don't know jquery but it seems that the buttons needs to be manually included. is there any method to include them directly? (with jquery looks and pop up).
Servlet comes in 2.4 version i had troubles changing it to 3.0 using eclipse.
Best Regards
If you use web.xml and servlets, you are out of liferay and the implementation for user login, themeDisplay, permissions and other features. This way is dificult and improductive.
Better choice is override the method "serveResource" of your portlet, in this method you have all control like in servlets and you can use other features of liferay.
In this question you have an example and more information:
respond to http request with json object in portlet

How to override AutoConfiguration in Spring?

I have followed the Spring example Serving Mobile Web Content with Spring MVC and got it working.
Now I would like to replace greeting.html with a simple static page (and replace Thymeleaf with a simpler view handler).
What is the easiest approach in this particular case?
I am new to Java, the probable solutions in web use web.xml, whereas this example does not use web.xml and it does not seem to be the recommended approach in Spring Boot documentation either. So, use #EnableAutoConfiguration with some overriding? How?
All you have to do is put the static HTML file in a place where Spring Boot will automatically look for static resources. This part of the documentation provides all the details.
So for example you can place your greeting.html under /src/main/resources/static/ and you will be able to access it at http://localhost:8080/greeting.html (that's if you have not configured a different port of the root path of the servlet context)

Get current page apache tiles within jsp

I have spring mvc application using apache tiles.Main file is template.jsp which includes header.jsp, then got place for potential messages and footer.jsp included.
Is there a way to check within jsp page ( using JSTL ) on which page I'm inside header.jsp code ?
Normal way of getting page URL are not good because
${pageContext.request.servletPath}
Always get's me the page I supposed to be so , template:
/WEB-INF/tiles/template.jsp
Is there a way to do it somehow , without adding anything to model from within spring?
(Because this is one of way to do this )
You can access the page URL (as it would appear in the browser) using the following EL in your JSP:
${requestScope['javax.servlet.forward.request_uri']}

Web application URL access java

I am trying to understand how the requests works. Unfortunately I was thrown at coding first and only then at understanding.
I wrote some really basic webapplication in java few years ago and it did work as expected. On its main web-page(.jsp) I had following as one of the menu buttons:
<p>test</p>
I am currently writing new webapp and forgot a lot. This time I am doing it with Spring MVC and properly. I can't really understand why this snippet no longer brings me to the home.jsp in current webapplication and why at first I did use it in old app.
Apache gives: The requested resource () is not available.
It is not that I need that sort of direct interaction, it is just I am trying to understand whether resources are accessible via URL? Does Spring MVC brings me extra security, where only servlet handled requests can result in a view? Am I missing something really trivial?
Moreover in that same old web app menu I had direct link to the servlet, but currently I can't make such direct reference to the servlet in the new webapp. I can make relevant request which will be captured by the servlet, but not by the name of it.
Apache gives: The requested resource () is not available.
Reference to servlet from menu:
<% if((String) session.getAttribute("passengerFound") != null){ %>
<img style="border:0" src="menuButtons/My Trips.png" alt="My Trips"/> <%} %>
Thanks, I bet it is really simple. I really want to understand, please help.
I know that it has something to do with Front Controller(dispatcherServlet), but I can't form logical and firm explanation in my head.
it is just I am trying to understand whether resources are accessible
via URL
In short, no. The default behavior and recommended configuration when using Spring MVC is to map the Spring DispatcherServlet to the / url pattern, meaning ALL requests are sent to the DispatcherServlet. Out of the box, the dispatcher-servlet will NOT service any requests for static resources. If this is desired, the two main options are
Map the DispatcherServlet to another pattern than root, effectivly isolating the Spring MVC portion to a sub-context
Add a resource-mapping to your spring context (your applicationContext.xml).
<mvc:resources mapping="/res/**" location="/res/" />
This above would tell spring mvc to treat all request to /res/** as requests for static resources (like images etc) and that those resources are physically located in the /res/ folder in the application root.
You might just be missing a "/" as in "/home.jsp" instead of "home.jsp"

Categories