Can anyone tell how to change the default view from index.jsp to any other file present inside a folder in spring webmvc - spring

I have created a Spring MVC web project I am trying to set the default view/root view from index.jsp to any other file with a different name with is present inside of folder instead of webapps. I have used java-based configuration instead of xml-based configuration. I have set the prefix and the suffix in WebMvcConfigurerAdapter also. after running the program, it is showing "No mapping found for HTTP request with URI [/AAAService/] in DispatcherServlet with name 'dispatcher'". Can anyone help me with it.
I have set the prefix and the suffix in WebMvcConfigurerAdapter

Related

Session cookie custom path

I have an spring boot application and want to deploy it to wildfly12. What I'm trying to achieve is that to set a custom path for JSESSIONID cookie. But after all, my efforts haven't had any results.
I have tried to use this property in my application.properties file:
server.servlet.session.cookie.path=/
When I run the application with the embedded tomcat, everything works fine; But when I deploy my app to wildfly, regardless of the value of that property, it always sets the cookie path to the "context-path" of the application.
I have also tried to use this property also:
server.servlet.context-path=/
but no success so far!
There is also this tag inside the standalone.xml file:
<session-cookie http-only="true" secure="true"/>
but it seems that it has nothing to do with the cookie path, as it doesn't have any property regarding that.
The configuration you are doing is for the embedded server of spring boot application.
Embedded server settings present in application properties (can be check here the section # EMBEDDED SERVER CONFIGURATION and the namespace server.servlet.session.cookie.*).
To modify cookie related configuration on external servers, you have to create CookieSerializer bean which can be used to customize cookie configuration. e.g.
#Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
You can refer spring guide for more information.

Thymeleaf +Spring Boot can't find files (FileNotFoundException)

This is a bit of a silly and frustrating one:
The #Configuration is taken from a tutorial website or forum and in it a
ServletContextTemplateResolver thymeleafTemplateResolver
is created using the ServletContext provided by spring boot.
When requested, a FileNotFoundException is thrown, despite the file being in the configured resources folder.
How do I get it to find the file / load it from the resources?
For thymeleaf to resolve the classpath resources, you need to configure a ClassLoaderTemplateResolver. (You were using a ServletContextTemplateResolver)
Also check that setPrefix is set to the correct folder, eg. "/thymeleaf/" if your documents are in resources/thymeleaf/ and that setSuffix is set to ".html" (or whatever your preferred file suffix is)
To also serve static content, you can extend WebMvcConfigurer and override addResourceHandlers, to then do e.g.
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
assuming a static folder in your resources.
(Spring controllers take precedence here)

Why my springboot app with freemarker doesn't work

I tried a springboot web application with freemarker.
In the bootstrap class there's a request-handling method:
#RequestMapping("/showAddPage")
String showAddPage(){
return "showAdd";
}
And i had my template, named "showAdd.ftl" ,lying in the directory of "resources/templates".
I also added freemarker's starter of springboot in pom.xml.
But when i request "localhost:8080/showAddPage", it retured a String, "showAdd", instead of the rendered content of the template "showAdd.ftl".
It doesn't render my showAdd.ftl.
Why could this happen?
I think you must add Servlet Mapping to your DispatcherServlet ;
There is one sample :
https://www.leveluplunch.com/java/tutorials/011-add-servlet-mapping-to-dispatcherservlet-spring-boot/
It would help you

Serving static contents in Spring Boot

I am developing a new poc for web application from Spring Boot. The packaging type of my application in war. In this all i want is to display some contents on a jsp. For that i have created a small jsp, and requierd css/images/js files i have put in resources/static folder. So my static folder contains css/images/js folders. I've added following code in my configuration file. My configuration extends from WebMvcConfigurerAdapter
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String[] pathPatterns = {"/components/**", "/images/**", "/scripts/**", "/styles/**"};
String[] resourceLocations = {"classpath:/static/components/", "classpath:/static/images/", "classpath:/static/scripts/, classpath:/static/styles/"};
registry.addResourceHandler(pathPatterns).addResourceLocations(resourceLocations);
}
However, my jsp does not get the reference of these file.
JSP Code
How to solve above problem..
Second concern, as per the spring boot reference documentation, it serves the static content which are located in static folder. that means i should be able to access files from my static folder directly in below way
http://localhost:8080/styles/main.css
But this is also not working
Third Issue - static contents are served by default servlet ..is this true that Default servlet in enabled by default in Spring Boot application
Please Help
Putting the static resources inside src/main/resources/static folder works for me without any addResourceHandlers configuration. For example, I have a css file at
src/main/resources/static/public/css/styles.css
which I refer from my JSP like this:
<link href="/public/css/styles.css" rel="stylesheet">
You should have put your JSPs inside src/main/webapp/WEB-INF, and set the packaging to war rather than jar, due to the limitations of having JSPs in Spring Boot.

Fail to serve .jsp and .html file via default servlet handler using spring mvc <mvc:default-servlet-handler />

The title should explain the biggest part :)
I should be able for example to access http://www.someurl.com:8080/index.jsp but instead I get HTTP Status 404 - /index.jsp
Now why do I assume I should be able to serve static content (ie not be redirected to custom controller but to default servlet handler in stead.)?
Because I have added the following element to my mvc dispatcher servlet config:
<mvc:default-servlet-handler />
I have read that in some case the name of the default server cannot be guessed and I have looked it up in the file: ̣*~/tomcat7/conf/web.xml .*
The default servlet is specified by a name "default". So I tried adding:
<mvc:default-servlet-handler default-servlet-name="default"/>
But to no avail...
I have one spring dispachter servlet mapped to '/'.
I have two controllers mapped to, one controller is mapped to '/' and one is mapped to '/todo'
The controllers work fine.
I thought the controller mapped to '/' could be the culprit so I removed that controller but to no avail.
Anybody an idea? And is it possible to have a controller mapped to '/' and still serve a page like /index.jsp??
Arf, I had put my static resources under the webapp/WEB-INF folder instead of the webapp folder. Now it seems te be working fine ... :)

Resources