Showing 404 in spring mvc - java

HTTP Status 404 - /SpringMVCHibernate/WEB-INF/views/user/userHome.jsp
shown in the webpage as error, even the page userHome.jsp is present in the correct package.
Thanking in Advance

You need to use the path given in #RequestMapping annotation on a controller method.Check for the controller method which returns userHome.jsp view.
Refer spring controller documentation
https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-controller

The error message says for 404 that it's looking for userHome.jsp. And indeed, your controller returns the following view name: " userHome". But in your case shows that your file is naled userHome.jsp. The case matters.
Try returning the properly cased view name which matches the JSP userHome.jsp from the controller. Currently the controller returns Hello while the JSP is named userHome.jsp, causing the 404
#Controller
public class UserHomeController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
model.addAttribute("greeting", "Hello");
//return "Hello";
return "userhome";
}
}

Related

Redirect to external URL from request mapping method in spring

Below is the request mapping method:
#GetMapping("/redirect")
public ResponseEntity<Void> redirect() {
String url = "http://yahoo.com";
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(url))
.build();
}
When I hit the URL http://somehost:8080/redirect in the browser I see that it takes me to yahoo.com, but when the /redirect is called from the UI(reactjs) the 302 Found httpstatus value is returned in the browser console but the page on the browser is blank. I was expecting to see the yahoo.com page. Seems it is not redirecting.
I referred this link: Redirect to an external URL from controller action in Spring MVC
reactjs code:
yield globalAxios.get(http://somehost:8080/redirect)
Below image when the http://somehost:8080/redirect gets called from the UI
Below image is when we the /redirect redirects to the link: yahoo.com
Is it because of the 405 method not allowed error as seen in the above image
Just in case if someone run into something like this in the future.
I end up using this code getting rid of 405 method not allowed while I am doing PUT-REDIRECT-GET pattern.
Notice it is #Controller and not #RestContorller. Otherwise it won't work.
If this is to be implemented in an existing rest controller you may want to add #ResponseBody over the other methods but not on these.
#Controller
#RequestMapping("/redirect")
public class RedirectController {
#PutMapping()
public String redirect() {
return "redirect:/redirect";
}
#GetMapping()
public String redirectPost() {
return "redirect:https://www.google.com";
}
}

404 error when using spring to display jsp

the url that is being clicked in the home is
Edit
the method its accessing in the controller is
#GetMapping("/edit")
public ModelAndView editBrewery(#RequestParam("id") int id) {
return new ModelAndView("/editBrewery", "brewery", service.getBrewereriesID(id));
}
its not getting into the return in the #GetMapping method in the controller, i put a break point at the return and it goes into the 404 before it gets to the breakpoint
the utput that the tomcat console is giving is
WARNING: No mapping found for HTTP request with URI [/Assignment2/breweries/edit] in DispatcherServlet with name 'dispatcher'
Try just using Edit (without the previous url parts).

How to get variables from one Spring MVC Controller to Another

I m novice to spring mvc. Could you please help me to design below.
I have newly written Controller A which is bind with model attribute “attribA” And new jsp is customerInformation.
Now I have existing Controller B which is bind with model attribute “attribB” and jsp is existing customerSummary which I will modify as per requirement where I want to display contents set by the user on previous jsp in model attribA.
How can I pass content or attributeA from new Controller A in an existing controller Controller B to show on second jsp customerSummary
Thank You
you can do this by using "RedirectAttributes"
#PostMapping("/userWiseNewMenuPermission")
public ModelAndView setUserWiseNewMenuPermission(ModelAndView modelAndView,RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("comfmMassg","Menu Permission Save Successful!");
modelAndView.setViewName("redirect:/userList?page=1");
return modelAndView;
}
in this Post controller redirect to a get controller with a attribute name "comfmMassg" ......
#GetMapping("/userList")
public ModelAndView getMenu(ModelAndView modelAndView,#ModelAttribute("comfmMassg") String masg) {
modelAndView.addObject("masg", masg);
modelAndView.setViewName("userList");
return modelAndView;
}
this example work in spring boot ............

Can I configure both HTML and Spring Data REST JSON on the same URI?

I am writing a web application with Spring MVC.
Keeping with REST principles, I am wanting to use consistent URIs in my application for the same resources and to use content negotation to select whether to return HTML or JSON. Visiting /people/bob in a browser should show his profile page, and getting it with curl should give me a JSON representation.
However, because of a semantic limitation (bug?) in the RequestMappingHandlerMapping, I can't "fall through" to the generic Spring Data REST mappings if I define any explicit controller, such as an HTML controller, on a matching URI. Besides manually implementing a #RestRepositoryController for every matching HTML controller, is there any other simple way to make Spring MVC content-negotiate between HTML and JSON mappings?
Example mapping that doesn't work:
#BasePathAwareController
class PersonHtmlController {
#GetMapping(path = '/people/{id}', produces = 'text/html')
ModelAndView person(#PathVariable Person id) {
new ModelAndView('person', [person: id])
}
}
This produces the expected HTML output but returns a 406 Not Acceptable when I ask for JSON.
You can go for contentnegotiatingviewresolver. Following is an example.
#RequestMapping(value = "/users", method = RequestMethod.GET)
public ModelAndView userDetails() {
ModelAndView modelAndView = new ModelAndView();
List userDetails = userService.getUserDetails();
modelAndView.addObject("users", userDetails);
modelAndView.setViewName("userDetails");
return modelAndView;
}
So whenever a request comes with /users then html page will be served and when the request comes as /users.json then a JSON response will be generated.
You can configure it in following way by overriding following method by extending WebMvcConfigurerAdapter provided by Spring.
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/", ".jsp").viewClass(JstlView.class);
registry.enableContentNegotiation(
new MappingJackson2JsonView()
);
}
Hope this helps.
Reference:
contentnegotiating view resolver example

Single Spring mvc 4 controller with no #RequestMapping returns strips css from my html view

I have a simple controller in a spring boot mvc app packaged as a .war (for aws deployment). I have Thymeleaf on the build path and bootstrap 3.3.6 in resources/static folder. I use #EnableAutoConfiguration in the main.
This controller displays a view just fine.
#Controller
#RequestMapping(value = "/handeware")
public class HomeController {
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String viewHome() {
return "home";
}
}
However, when I remove the #RequestMapping part like this
#Controller
public class HomeController {
#RequestMapping(method = RequestMethod.GET)
public String viewHome() {
return "home";
}
}
It appears that the view gets returned with the Bootstrap CSS styling stripped.
Essentially, I have a domain name www.blah.com with the CNAME pointing to my spring mvc app hosted in aws elastic beanstalk. The idea is that I want someone to be able to type www.blah.com into the browser and be able to see my home page. In order to do this, I removed the #RequestMapping's from my controller, and it works. People can now visit my site at www.blah.com and see my home.html. However, the CSS styling is not showing up now. If I add the #RequestMapping back, the CSS shows back up. Isn't that odd? I confirmed in my local as well as in aws that this is the case. Not sure what to make of it.
How do you reference your css files from your html? You may need to update those references to match the new path.

Categories