Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security

To partially update an entity, you should use @SessionAttributes to store the model in session between requests. You could use hidden form fields, but session is more secure. To use P/R/G with validation, use flashAttributes To secure fields use webDataBinder.setAllowedFields(“field1″,”field2”,…) or create a class specific to the form then copy values to your entity. Entities … Read more

Spring 3 MVC accessing HttpRequest from controller

Spring MVC will give you the HttpRequest if you just add it to your controller method signature: For instance: /** * Generate a PDF report… */ @RequestMapping(value = “/report/{objectId}”, method = RequestMethod.GET) public @ResponseBody void generateReport( @PathVariable(“objectId”) Long objectId, HttpServletRequest request, HttpServletResponse response) { // … // Here you can use the request and response … Read more

Neither BindingResult nor plain target object for bean name available as request attribute [duplicate]

In the controller, you need to add the login object as an attribute of the model: model.addAttribute(“login”, new Login()); Like this: @RequestMapping(value = “https://stackoverflow.com/”, method = RequestMethod.GET) public String displayLogin(Model model) { model.addAttribute(“login”, new Login()); return “login”; }

Spring MVC: How to return image in @ResponseBody?

if you are using Spring version of 3.1 or newer you can specify “produces” in @RequestMapping annotation. Example below works for me out of box. No need of register converter or anything else if you have web mvc enabled (@EnableWebMvc). @ResponseBody @RequestMapping(value = “/photo2”, method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public byte[] testphoto() throws IOException … Read more

Setting Precedence of Multiple @ControllerAdvice @ExceptionHandlers

Is this how one would expect Spring MVC to behave? As of Spring 4.3.7, here’s how Spring MVC behaves: it uses HandlerExceptionResolver instances to handle exceptions thrown by handler methods. By default, the web MVC configuration registers a single HandlerExceptionResolver bean, a HandlerExceptionResolverComposite, which delegates to a list of other HandlerExceptionResolvers. Those other resolvers are … Read more

How to use Session attributes in Spring-mvc

If you want to delete object after each response you don’t need session, If you want keep object during user session , There are some ways: directly add one attribute to session: @RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute(“cart”,value); return “testJsp”; } and you can get it from controller like this … Read more

Serving static web resources in Spring Boot & Spring Security application

There are a couple of things to be aware of: The Ant matchers match against the request path and not the path of the resource on the filesystem. Resources placed in src/main/resources/public will be served from the root of your application. For example src/main/resources/public/hello.jpg would be served from http://localhost:8080/hello.jpg This is why your current matcher … Read more

How to handle static content in Spring MVC?

Since I spent a lot of time on this issue, I thought I’d share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/> (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site’s root. In order … Read more