Spring Tutorials, Tips & Tricks

Spring Framework is an open source application framework for the Java platform and .NET Framework.

Navigating Spring Security from thick Client to REST Webservice

When a Web Service is protected by Spring security, and we try to access that Web Service, we are presented with a login page to enter our credentials. After we provide our credentials, this information is stored as part of the session and sent back and forth between the browser and the server in a cookie.However this is not the way it works with a ...

Spring @RequestHeader Annotation example

Spring @RequestHeader Annotation example
Let us quickly check how to access http Header information in Spring MVC Controller.Spring @RequestHeader Annotation Spring MVC provides annotation @RequestHeader that can be used to map controller parameter to request header value. Following is the simple usage of spring @RequestHeader annotation.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; //..@Controller public class HelloController {@RequestMapping(value = "/hello.htm") public String hello(@RequestHeader(value="User-Agent") String userAgent)//.. } }In above code snippet we defined a controller method hello() which is mapped ...

Spring MVC Exception Handling using @ControllerAdvice annotation

Spring MVC Exception Handling using @ControllerAdvice annotation
Spring MVC provides a great way to handle exceptions and errors. @ExceptionHandler annotation is core to this feature. For each Spring controller we can simply define a method that automatically gets called if a given exception occurs. For example:import org.springframework.web.bind.annotation.ExceptionHandler;//.. @ExceptionHandler(IOException.class) public String exception(Exception e) { //.. return "error"; }Thus whenever an IOException is raised from any controller method will call the ...

Spring MVC Cookie example

Spring MVC Cookie example
In this post we will see how to access and modify http cookies of a webpage in Spring MVC framework.Read Http Cookie in Spring MVC Spring 3 MVC framework provides a very useful annotation @CookieValue to access data set within any http cookie. This annotation can be leverage to fetch the cookie value without getting ...

Spring MVC Flash Attribute tutorial with example

Spring MVC Flash Attribute tutorial with example
Spring MVC 3.1 version has added a very useful feature Flash attribute which it lacked and is now solving a long time problem of POST/Redirect/GET pattern.In a normal Web based MVC application each form submitted POST the data to the server. A normal spring controller (tagged with annotation @Controller) fetches the data from request and process it further (save or update in database). Once ...

Spring 3 MVC Interceptor tutorial with example

Spring 3 MVC Interceptor tutorial with example
Spring MVC provides a powerful mechanism to intercept an http request. Similar to Servlet Filter concept, Spring MVC provides a way to define special classes called Interceptors that gets called before and after a request is served.Quick OverviewEach interceptor you define must implement org.springframework.web.servlet.HandlerInterceptor interface. There are three methods that need to be implemented.preHandle(..) is called before the actual handler is executed;The ...

Solve:Errors/BindingResult argument declared without preceding model attribute

Sometimes Spring MVC will amaze you with totally unexpected exceptions. You have no idea why that is coming.For instance, recently I wrote a small piece of Spring MVC Controller code, one like below:@Controller public class UserController {@RequestMapping(value = "addUser") public String addUser(@ModelAttribute("userForm") UserForm userForm, ModelMap map, BindingResult results) {if (results.hasErrors()) { return "add_user_form"; }return "add_user_success"; } //... }And while executing the application, I got a strange exception:Throwable occurred: java.lang.IllegalStateException: Errors/BindingResult argument declared ...

Spring MVC Multiple File Upload example

Spring MVC Multiple File Upload example
In this simple tutorial we will see how to implement multiple file upload in a Spring 3 MVC based application.The requirement is simple. We have a form which displays file input component. User selects a file and upload it. Also its possible to add more file input components using Add button. Once the files are selected and uploaded, the file names are displayed on success ...

Spring MVC HashMap Form Integration example

Spring MVC HashMap Form Integration example
Spring MVC provides powerful way to manage form inputs. It also provides form validation functionality which is easy to integrate in any application. But other than the normal form bean mapping, recently I had a requirement to map dynamic values like key-value pairs in an HTML form and retrieve the same in Spring Controller. So basically HashMap came to rescue.Let us see how ...

Spring MVC + FreeMarker (FTL) Integration example

Spring MVC + FreeMarker (FTL) Integration example
Welcome to Freemarker Tutorial Series. In previous post we created Servlet based Hello World Freemarker Template example. We learned few APIs of freemarker and also how to integrate it with Servlet based application. Following are the list of tutorials from Freemarker tutorial series.Today we will create a Spring MVC based application that uses Freemarker FTL as view instead of JSP. This would give you ...
Page 1 of 3123