Spring MVC - Double Slash in RequestMapping causes bugs in CSS link - java

I have a controller, and i put double slash on it.
#RequestMapping(value="/vehicles/add", method = RequestMethod.GET)
public String addVehicle(Model model) {
model.addAttribute("vehicle", new Vehicle());
return "newvehicle";
}
And the CSS link:
<link rel="stylesheet" type="text/css" href="css/main.css"/>
What happened is when I have double slash, the css link of the html was not working. So I tried to just use single slash
RequestMapping(value="/add", method = RequestMethod.GET)
and it worked. But I want to have a double slash. What do you think is the problem. I think the directory is not the problem because it worked with single slash.

Related

"Absolute paths not recommended in JSPs" ? - Is this also apply to the #RequestMapping()?

I got warnings from IntelliJ that "Absolute paths not recommended in JSPs"
I understand that, for internal files locations, like "styles.css or script.js", we should use relative path, something like this -
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/styles.css">
But how about the #RequestMapping(), #GetMapping, #PostMapping, ... , and etc
For example, I have a form here:
<form method="post" action="/file/upload-submitted" enctype="multipart/form-data">
...
</form>
and I also have:
#PostMapping(value = "/file/upload-submitted")
IntelliJ also give me warning "Absolute paths not recommended in JSPs" at "/file/upload"
Is it the correct warning?
***The reason that I put the absolute path there because there are multiple paths to get to the form.
#GetMapping(value = {"/file", "/file/upload"})

Thymeleaf 3 URL resolving not working with Spring Boot 4 + Spring Security

After having configured Thymeleaf 3 in Spring Boot 4 using the Gradle configuration
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.thymeleaf:thymeleaf:3.0.3.RELEASE')
and
ext['thymeleaf.version'] = '3.0.3.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.1.2'
URL resolvers do not properly resolve the URLs in Thymeleaf views:
<link rel="stylesheet" href="/css/login.css" data-th-href="#{~/css/login.css}">
simply becomes
<link rel="stylesheet" href="/css/login.css">
I have done some debugging and first of all, during a request I noticed that SaveToSessionResponseWrapper (sub of SaveContextOnUpdateOrErrorWrapper) gets initialized like this:
public SaveContextOnUpdateOrErrorResponseWrapper(HttpServletResponse response,
boolean disableUrlRewriting) {
super(response);
this.disableUrlRewriting = disableUrlRewriting;
}
The arguments passed are a FireWalledResponse and false. The latter results in the following method completely disabling the forwarding of URLs:
#Override
public final String encodeURL(String url) {
if (this.disableUrlRewriting) {
return url;
}
return super.encodeURL(url);
}
Now, if I put a breakpoint in the constructor and force disableUrlRewriting to be true, it eventually executes HttpServletResponseImpl.isEncodeable which then fails here:
} else if(hreq.isRequestedSessionIdFromCookie()) {
return false;
At this point I'm not sure what is wrong. I'm unable to find anyone with this error and it works with neither starter-tomcat nor starter-undertow but I haven't done as thorough debugging in Tomcat yet.
#{~/css/login.css} is a server-relative URL in Thymeleaf.
If you want a context-relative URL, omit the tilde (~) character:
<link rel="stylesheet" href="/css/login.css" data-th-href="#{/css/login.css}">
See Standard URL Syntax article for details.
You should use
<link rel="stylesheet" href="/css/login.css" th:href="#{/css/login.css}">
Without data-th-href

Spring dynamic url access to css/js files

I have a simple Spring boot application. There URL that would be used here is going to be like:
sub.domain.com/variable1/variable2
and variable1 and variable2 can be anything for example:
sub.domain.com/kfc/foo
sub.domain.com/subway/boo
and I capture those in my controller as below:
#Controller
public class IndexController {
#RequestMapping(value="/{var1}/{var2}", method = RequestMethod.GET)
public String Index(#PathVariable(value="var1") String variable1, #PathVariable(value="var2") String variable2) {
return "/index.html";
}
}
which will redirect to my html page which is located at src/main/webapp/index.html
so the index page is loaded under the dynamic url but then none of the scrips or stylesheets are found because it's trying to find them under variable1 path.
for example if I have
sub.domain.com/kfc/foo
and my link to css in the index.html is
<link rel="stylesheet" href="styles/index.css" />
it will try to find the index.css under
sub.domain.com/kfc/styles/index.css
how can I tell the app to look into the right place for the styles or js files?
index.html is trying to load styles from a relative path.
In order for index.html to load styles under sub.domain.com/styles/index.css, add a / in link tag **/**styles/index.css <link rel="stylesheet" href="/styles/index.css" />

Spark Java isn't recognizing static files and serving them

This was working before.
I have a request for 2 things.
<link rel="icon" href="/favicon.png">
and a style,
background-image: background.jpg;
but it doesn't recognize and returns with 404 not found.
I have set
Spark.staticFileLocation("/public/");
and made sure that it is correct
src/main/resources/public/*.jpg
How can I fix?
Try to remove terminating slash
staticFileLocation("/public");

solve the path to css in jsp

I'm using Spring MVC 3.0 to construct a rest-style url.
Here is part of my code:
#RequestMapping(value = {"/", "/posts"}, method = RequestMethod.GET)
public String getNewestPosts(Model model, HttpServletRequest request)
throws DataAccessException {
return getPostsByPage(1, model, request);
}
#RequestMapping(value = "/posts/page/{page}", method = RequestMethod.GET)
public String getPostsByPage(#PathVariable long page, Model model,
HttpServletRequest request) throws DataAccessException {
// ... get the posts by page number
}
I wrote two methods. One handles the request from url "/posts" which means retrieving the first page of the posts , and the other one handles the request from url "/posts/page/{page}" which means retrieving the posts according to the path variable {page}.
And the problem is that all the two methods above point to the same view which is a jsp file, but they're in different paths("/posts, "/posts/page/xxx"). The css path (../style.css) can not adapt both of them.
I try to solve it by using absolute css path(/style.css), which means the web application works only if the application is deployed on a root path("/").
I would appreciate it if you could help me.
Use the <c:url> tag, which prepends the context path to an absolute URL.
<link rel="stylesheet" type="text/css" href="<c:url value="/style.css" />" />
or
<c:url var="cssUrl" value="/style.css" />
<link rel="stylesheet" type="text/css" href="${cssUrl}" />

Categories