Spring boot web app with jsp file extension in the url - spring-boot

We have a legacy web app that uses the url below:
https://example.com/forms/forms.jsp
We have rewritten the legacy code to use spring boot web app with the url https://example.com/forms without .jsp extension in the url.
In order to keep the old url, which is a business requirement, I followed this Spring MVC #PathVariable with a dot (.) gets truncated. However, it only works when there is a / https://example.com/forms/forms.jsp/ in the url.
I googled it, found some suggestions in stack overflow and tried a couple of different approaches. But not quite working.
UPDATES:
I just found this post on jsp in the url. I updated from #GetMapping("/forms/{jspName:.+}") to #GetMapping(path = "/forms/{jspName:.+}"). This made it possible to check the pathVariable in the if statement. However, the return statement does not return the expected forms.jsp file as before; instead it gives 404 error.
url : http://localhost:8080/forms/forms.jsp
#GetMapping(path = "/forms/{jspName:.+}")
public String getForms(#PathVariable("jspName") String jspName) {
if (jspName.equalsIgnoreCase("forms.jsp")) {
log.debug("JSP file is {}", jspName);
return "forms";
}
return "index";
}
Console:
[nio-8080-exec-1] c.n.e.c.EthicsEmailFormController : JSP file is forms.jsp
404 Error:
No webpage was found for the web address: http://localhost:8080/forms/forms.jsp
HTTP ERROR 404
application.properties - The forms.jsp is stored under /WEB-INF/views/page/forms.jsp.
spring.mvc.view.prefix=/WEB-INF/views/page/
spring.mvc.view.suffix=.jsp
For comparison:
Below works when the url is https://example.com/forms/forms.jsp/ with a / at the end of the url. But the existing url (that is specified in the requirement) does not contain a / at the end of the url. It has to be https://example.com/forms/forms.jsp.
#GetMapping("/forms/{jspName:.+}")
public String getForms(#PathVariable("jspName") String jspName) {
log.debug("Jsp file name = {} ", jspName);
return "forms";
}
I saw some suggestion is to remove below dependency. I also do NOT have below dependency in pom.xml.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Any suggestions? Thanks a lot!

Since this app is for training only, I moved the jsp file to webapp/forms.forms.jsp folder. This way, the jsp file can be accessed directly from the url below:
https://example.com/forms/forms.jsp
There is no need to go through a controller. Thanks to my friend Barry for his suggestion. Thanks!

Related

No webpage was found for the web address: http://localhost:8080/ in Spring generated by codegen with swagger

I am learning swagger using the example pet store in the swagger editor: https://editor.swagger.io/
The code for spring is generated and I does not change anything. However, everytime I run it on http://localhost:8080, it gives the error message
This localhost page can’t be found
No webpage was found for the web address: http://localhost:8080/
HTTP ERROR 404
But I believe I am supposed to see something like in the following website:
https://petstore.swagger.io/
May I ask how to solve this issue? Many thanks.
I follow the URL in the controller class HomeController.
#Controller
public class HomeController {
#RequestMapping(value = "/")
public String index() {
System.out.println("swagger-ui.html");
return "redirect:swagger-ui.html";
}
}
You might be visiting the wrong URL. Try out http://localhost:8080/<base-url>/swagger-ui.html
Additionally, you could refer this link for a basic setup.
It appears the context path is v2.
You should be able to access the demo at:
http://localhost:8080/v2/<endoint>
Try accessing:
http://localhost:8080/v2/swagger-ui.html
The context path is defined in a configuration file titled application.properties located under src/main/resource. The file contains the following:
springfox.documentation.swagger.v2.path=/api-docs
server.contextPath=/v2
server.port=8080
spring.jackson.date-format=io.swagger.RFC3339DateFormat
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
The context path is defined under server.contextPath.

Thymeleaf: can't load js and css when direct access the page

Updated: to describe the question more clearly
I create a web applicaiton with spring boot and thymeleaf, everything works fine if I open the login page, then login, then head for the management module or reports module sequently.
The proleam occurs when I type the url locahost:8080/send/kf/index(needs to authenticate, but I have open access to all in customized filter) in the browser, the page loads without js and css. In debug mode, I saw /send/kf was unexpectly put into the path like the following. I can get the resource if I access localhost:8080/assets/avatars/avatar.png.
The following is the folder structure I put my static resources. How could /send/kf automatically added into the url, please help me solve this problem. Thanks!
you can use spring.resources.static-locations in your application.properties file
spring.resources.static-locations=classpath:/resources/css/
spring.mvc.static-path-pattern=/resources/**
this is taken form documentation
Note:Static resources, like JavaScript or CSS, can easily be served from your Spring Boot application just be dropping them into the right place in the source code. By default Spring Boot serves static content from resources in the classpath at "/static" (or "/public")
Using /assets/ instead of assets/ fixes the issue, as otherwise it's a relative url that's supposed to get added to the end of existing url.
I find a solution for this question. I don't know how /send/kf is put into the url for the static resources, but I think if I put the controller mapping under the root path, this problem should avoid. As I think, the url is correct and resources can load successfully.
#Controller
public class ManualMessageController {
#Autowired
private MsgTemplateRepository msgTemplateRepository;
#RequestMapping("/manualMsg")
public String manualMsg(Model model){
model.addAttribute("msgTemplateList", msgTemplateRepository.findByStatus(1));
return "manualMessage";
}
}
updated:
The right solution is to use absolute path rather than relative path in the project. So change assets/css to /assets/css works.

Url Binding in SpringBoot

i have been strugling since 4 days but did not found the solution for how to bind the url in Spring Boot with gradle.
I have a url as, http://loalhost:8080/blog/post.html?pid=2&ptitle=abc
I want this url to be shown as below with html and (?),
http://localhost:8080/blog/post/2/abc
Is there any way to do that. I don't want to use tucky urlrewriter. All my html files are placed in webapp folder.
Thanks in Advance.
if your have a POST method in your #RestController to create a post blog:
#PostMapping("/blog/post/{pid}/{ptitle}")
public void create(#PathVariable("pid") String pid, #PathVariable("ptitle") String ptitle) {
// a repo save call
}
(I supposed that you use last spring boot version)

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

Spring MVC doesn't find views after changing port 8080 to 80

I have a tomcat8-spring project. The project works good in HTTP or HTTPS.
To get it working with HTTPS, I had to change in servers.xml the port from 8080 to 80. Now, HTML/JSP pages that were found before are now not being found, and Spring MVC throws 404.
My JSP's are now in src\main\webapp\WEB-INF\views. I tried adding methods in the Controller that return the name of the JSP, but it didn't work. I tried Changing WEB-INF to WebContent and that also didn't work.
Where should my JSP's be? What address should I use to access them?
If it interest those who reads - You can tell tomcat to display webpages.
In your #RequestMapping in the controller, simply return the name and address of the HTML/JSP/JS/... page you want to display.
This is the most general folder structure of the Maven Project if you are using it for Spring MVC.
This is how you generally map to a view through a controller . Try doing the forward slash and see if anything changes. F12 Developer tool in your browser can be of great help for debugging
#Controller
public class HelloController {
#RequestMapping("/hello.htm")
public String handleIndexGet() {
return "/pages/hello"; // forward to view hello.jsp
}
}

Resources