I cannot load my templates in the Spring Boot 2 - java

I cannot access the templates I wrote under the templates folder. It will just redirect me to the Whitelabel Error Page. My structure is like this:my controller is under the src/main/java/com.project/controller/IndexController.java while my templates are under the src/main/resources/templates/home.html.My code in the controller is like this:
#Controller
public class IndexController {
#RequestMapping(path = {"/vm"}, method = {RequestMethod.GET})
public String template(Model model) {
return "home";
}
#RequestMapping(path = {"/index"}, method = {RequestMethod.GET})
#ResponseBody
public String index() {
return "Hello world";
}
}
And my home.html is like this:
<html>
<body>
<pre>
Hello World!
</pre>
</body>
</html>

I've solved the problem myself. The thing is the dependency. I did not import the spring-boot-starter-thymeleaf from org.springframework.boot. The dependency is really hard to deal with in Spring.

The default context path of the app would be "/" and there is neither index.html file is present nor a controller method for that path which is why you are seeing that White label Error Page. If you don't want to see that error, add the below property in your application.properties file then you would the HTTP 404 Error.
server.error.whitelabel.enabled=false
For the below method that you have in controller:
Annotation that indicates a method return value should be bound to the web response body.
#RequestMapping(path = {"/vm"}, method = {RequestMethod.GET})
public String template(Model model) {
return "home";
}
Go to http://localhost:8080/vm this would load the home.html file that you have in templates folder.
Whereas for this one:
#RequestMapping(path = {"/index"}, method = {RequestMethod.GET})
#ResponseBody
public String index() {
return "Hello world";
}
You have used the annotation #ResponseBody which would indicate a method that the return value should be bound to the web response body, so you would see the text Hello world in the browser when you hit the http://localhost:8080/index

Related

return web page contents not the web page name spring boot

I want to display the contents of the HTML page. But this returns the name of the web page. I tried using #controller and #Responsebody also using #RestController. Neither gives the contents of the page. Please help me to solve this problem. I'm trying to display vendors.html, but prints the name "vendors".
This is my code.
#Controller
#RequestMapping("/vendors")
public class VendorController {
#Autowired
private VendorService vendorService;
#RequestMapping("/indexVendor")
#ResponseBody
public String viewIndexVendor2(Model model) throws Exception {
List<Vendor> listVendor;
listVendor = vendorService.listAll();
model.addAttribute("listVendor", listVendor);
return "vendors";
}
}
thanks :)
#ResponseBody return the response of the method, in your case a String in the value is "vendors", so your request "/vendors/indexVendor" return "vendors". It is quite normal who should display "vendors".
To display the content of your "vendors.html" page, start by removing the #ResponseBody annotation and rename your page to "vendors.jsp" and configure a templating engine to handle jsp files or use themleaf.

i want to know in spring boot web app when we redirect from one jsp to another jsp

As you can see my hellow in request mapping is returning "welcome" jsp page but if I want to use "a href" in welcome jsp page to call view jsp page should I call the controller requestmapping ie "/products" or should I call viewtable jsp page directly?
welcome.jsp
< a href="??"</ahref>
viewtable.jsp
<h1>hellow<h1>
controller
#RequestMapping(value="/hellow",method = RequestMethod.GET) public String
abc(ModelMap model) { return "welcome"; }
#RequestMapping(value="/products", method= RequestMethod.GET)
public String getAllProducts(ModelMap model)
{
System.out.println("in controller");
//return pls.productListAllRecords();
List display;
display=productservice.listAllRecords();
System.out.println(display);
model.addAttribute("records",display);
// return "viewtable";
return "viewtable";
}
If you would like to direct a user to the respective page, you could include a href property within an "< a >" tag with the requestMapping path included.
The following would direct to the above two paths when selected.
Hellow
Products

Spring-boot restapi page call

I got a simple spring boot application. I am trying load the html file through a rest controller. Below is my folder structure. I am unable to load test.html (just as helloworld code inside it)
#RequestMapping("/show1")
public ModelAndView showpage1() {
System.out.println("## show1");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
return modelAndView;
}
My test.html content
<h1> hello World </h1>
Try doing this :
#RequestMapping("/show1")
public String showpage1() {
return "test";
}
where test.html is your page
You just need to return the string with the name of the view.
#RequestMapping("/showpage")
public String showpage1() {
return "test";
}

Path Varaible error after call the controller with path variable and then other controller

I have problem in using pathvariable in spring mvc application.
I have two controller like this
#RequestMapping(#RequestMapping(value = { "BuyPackage/{company_code}" }, method = RequestMethod.GET)
public String controller1(#PathVariable("company_code")int company_code){
............
}
#RequestMapping(#RequestMapping(value = { "Preview" }, method = RequestMethod.POST)
public String controller2(){
............
}
when the first controller called the url is localhost:8080/myapp/BuyPackage/1
and then when the second controller called the url turn to localhost:8080/myapp/BuyPackage/Preview and the error occur.
the url should be like this :localhost:8080/myapp/Preview
Can anyone help me to deal with this problem.?
You can use jstl. Try to use this with your link <c:url value='/Preview' />
Example: Link to Preview

Set visible URL with Spring

Is there a way to set the displayed url of a page using Spring MVC ?
Let me be clearer with an example : I have the following controller :
#Controller
public class Display{
#RequestMapping(value = "myPage")
public ModelAndView display() {
ModelAndView result = new ModelAndView(Uris.MY_PAGE);
return result;
}
#RequestMapping(value = "myPage/revisited")
public ModelAndView accountManagement() {
ModelAndView result = new ModelAndView(Uris.ACCOUNT);
return display();
}
}
If I go on myPage/Revisited, I'll get the JSP associated to myPage. However, in my browser, the url will stay the same (myPage/revisited). How could I prevent that ?
To be more precise on what sam said, you can use UrlRewriteFilter, the installation process is explained in the link, then set a rule in the file urlrewrite.xml:
<rule match-type="wildcard">
<from>/myPage/revisited/redirect</from>
<to type="redirect">%{context-path}/myPage</to>
</rule>
And in your controller, just use what Emanuele said, i.e.
response.sendRedirect("redirect");

Categories

Resources