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

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
}
}

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.

Automatically finding Thymeleaf templates with Spring Boot

How can I get Spring Boot and Thymeleaf to automatically find and map template files to be processed when accessed by the browser?
src/main/resources/templates/index.xhtml
src/main/resources/templates/bar.xhtml
src/main/resources/application.properties contains spring.thymeleaf.suffix=.xhtml
FooController.java contains #RequestMapping("/foo") and a #PostMapping method that returns bar
If I enter http://localhost:8080/ in the browser, Thymeleaf processes and displays the index.xhtml page with no extra configuration needed. But http://localhost:8080/index, http://localhost:8080/index.xhtml, and http://localhost:8080/index.html all result in 404 Not Found.
My index view does a POST to foo; FooController is activated and returns bar; and Thymeleaf processes and shows bar.xhtml, even though bar.xhtml isn't mapped anywhere in the configuration. Yet accessing http://localhost:8080/bar, http://localhost:8080/bar.xhtml, and http://localhost:8080/bar.html in a browser all result in 404 Not Found.
Why does GET http://localhost:8080/ process the index.xhtml template, but GET http://localhost:8080/index does not?
How can Thymleaf use bar as a view, but I cannot access http://localhost:8080/bar directly?
How can I configure Thymeleaf so that I can add src/main/resources/templates/example.xhtml and have it processed automatically as a template that I can access via http://localhost:8080/example in the browser, with no explicit configuration specifically for the example.xhtml file?
If I absolutely have to configure controllers (see my answer below), is there a way that I can at least do this in some declarative file, outside of my code?
As noted in Spring in Action, Fifth Edition, I can do something like this in a #Configuration class that implements WebMvcConfigurer
#Override
public void addViewControllers(final ViewControllerRegistry registry) {
registry.addViewController("/bar");
}
That will allow me to process bar.xhtml automatically. (I presume there is some default configuration registry.addViewController("/").setViewName("index"), which is why my index.xhtml file is getting processed by accessing the root path.
And I can even use the following to automatically pick up any template:
#Override
public void addViewControllers(final ViewControllerRegistry registry) {
registry.addViewController("/**");
}
Unfortunately this removes the mapping from / to /index, and also prevents accessing any static resources from src/main/resources. I'm not sure how to tell Thymeleaf to use a template if it can, and fall back to a static file if not.

Angular + Spring on same port

I deploy my angular project and add to resources/static in spring project, next I create simple controller:
#Controller
class IndexController {
#RequestMapping(value = "/")
fun index(): String {
return "index"
}
}
And now when I run tomcat and set url to localhost:8080/ everything work well I can click on buttons and browser redirect to proper site, But now if I refresh site or set diffrent url, my angular app stop working and I get 404 or spring endpoints. Question is, how I can set angular as default to load when I type diffrent url than this from controller
You need to separate your API endpoints context. For example /api/**. And then, define an endpoint with a regex, something like this ^/api (all other than /api) to resource path.

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.

Configure index.html location in Spring initializr project

I created a Spring boot project using Spring initializr.
The project structure is below. /resources/client is the folder I added manually.
After I ran DemoApplication, I hit localhost:8080 and I saw the home page was pointed to /resources/index.html. I wanted to set the home page to be /resources/client/build/index.html, so I added something in application.properties:
spring.mvc.view.prefix=/resources/client/build/ ### also tried /client/build/
spring.mvc.view.suffix=.html.
However, it did not work and the home page was still pointed to /resources/index.html.
Also, The application is using dispatcherServlet but I did not find a dispatcherServlet file.
Is there any way I can use custom index.html location? Thanks.
Maybe you can try to add a HomeController using java code as following:
#Controller
public class HomeController {
#RequestMapping("/")
public String index() {
return "client/build/index.html";
}
}

Resources