Posts Tagged ‘Spring’

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 ...

How to access Static Fields in Freemarker FTL

If you defined static fields in Java class and want to use the same in Freemarker (FTL) it is not possible to do so as FTL is not aware of Java objects during rendering. In order to make static fields available to FTL, we need to expose them.One way of exposing static Java fields to freemarker template is to use them through Model. But if ...

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 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 ...

Change spring-servlet.xml Filename (Spring Web Contenxt Configuration Filename)

The first thing that we do when we want to implement Spring MVC in our project is to add DispatcherServlets entry in deployment descriptor (web.xml). Also we write a spring web configuration xxxx-servlet.xml which contains all the MVC mappings and data.By default the name of file must be XXX-servlet.xml where XXX is the name ...

Introducing Cache support in Spring 3.1 M1

Introducing Cache support in Spring 3.1 M1
Spring 3.1 M1 is out with some very useful features. One of the coolest feature in the latest release is comprehensive Caching support!Spring Framework provides support for transparently adding caching into an existing Spring application. Similar to the transaction support, the caching abstraction allows consistent use of various caching solutions with minimal impact ...
Page 1 of 3123