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";
}
}
Related
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.
I have a spring boot project where ‘/’ will be redirected to ‘/home’ in HomeController bcz of the setting in WebConfig class and from there the return string is “home” so the thymeleaf template engine will look for home.html inside the templates folder but instead of it is looking for mainLayout.html which is inside the layout folder which is inside the template folder.
I have seen some blogs on how to use thymleaf with spring but didn't get the solution.
This is the link for complete project:
https://github.com/sivaprasadreddy/jcart
Go to jcart-site then look for WebConfig class then look for HomeController class and then look for templates folder there check mainLayout.html
Do this in your index.html file
<script>
window.location="/home";
</script>
it will redirect to your homecontroller.
I am pretty new to Spring Boot, and I saw that if I start a Spring Boot project and put an index.html file in the folder src/main/resources/static, then when I open my browser and go to address "localhost:8080", I'll see this file displayed in the browser.
Now, I have another html file (let's call it 'hello.html'), and I also placed it in src/main/resources/static. I wrote the following code:
#Configuration
#Controller
#EnableAutoConfiguration
#ComponentScan
public class TopicController {
#RequestMapping("/hello")
public String hello() {
return "hello.html";
}
}
As you can understand, I want that when I go to localhost:8000/hello, I'll see the display of 'hello.html' file.
However, I get the following error in the console:
javax.servlet.ServletException: Circular view path [hello.html]: would
dispatch back to the current handler URL [/hello.html] again. Check your
ViewResolver setup! (Hint: This may be the result of an unspecified
view, due to default view name generation.)
Do you know what I can do so I can get the hello.html file in the browser for accessing "localhost:8080/hello" ?
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
}
}
Can I use EL to specify my application context path on my Spring Controller using something like #RequestMapping("${pageContext.request.contextPath}")? Actually, I get an 404 error. So my question is how to map controller on context root?
I presume #RequestMapping("/") cannot do the job when app is not alone on the server, and url looks like:
http://localhost:8080/myapp/mypage
#Controller
#RequestMapping("${pageContext.request.contextPath}")
public class MainController {
#RequestMapping("/")
public ModelAndView index() {
}
}
PS: I've already added the app path prefix into every single url on my JSPs, like <form class="form-inline" role="form" action="${pageContext.request.contextPath}/search" method="post">. Now I need to make the same trick for my Spring Controllers to make it work when... wheather app deployed as ROOT app or as "just another app on this server".
you can add in the application.properties config file the root context path. That root context path will be the prefix path to all the controller paths.
application.properties
server.contextPath=/myapp/mypage
controller
#RequestMapping("/test")
Result:
http://localhost:8080/myapp/mypage/test