Posts

Showing posts with the label Spring

Spring MVC 3.1 - How to Get HttpServletRequest and HttpServletResponse objects in Controller Method

When Spring MVC 3.1 dispatcher introspect the parameters lists of user defined controller method that is to be executed. It identifies it from URL pattern. Using reflections it introspect the types of parameters and pass the required object to that controller method. For example, there is a method hello() which expect HttpServletRequest and HttpServletResponse object binded or associated with current under-process request, the hello() method can be declared like this: @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String hello(HttpServletRequest request, HttpServletResponse response) { // Use request and response object as you in JSP or Servlets } So, the Spring MVC 3.1 would automatically pass the defined parameters.

Spring MVC 3.1 - How to Get HttpSession in Controller Method

In Spring MVC 3.1, there is great improvement on ways how we get frequently used object during request processing life cycle. You may need to have access to following classes instances frequently in different controllers e.g. HttpServletRequest, HttpServletResponse, HttpSession, etc. When Spring MVC 3.1 dispatcher receive the raw request, it introspect the parameters lists of user defined controller methods which is supposed to be executed (from URL pattern). Using reflections it introspect the types of parameters and pass the required object to that controller method. For example, there is a method names index which expect HttpSession, so that it could perform something related to session. the index() method can be declared just like this: @Controller @RequestMapping("/home") public class HomeController { @RequestMapping(method = RequestMethod.GET) public String index(HttpSession session) { // Use session object here, as you like } The Spring MVC 3.1 would automatic...

How to Get Spring Managed Service Instance Outside the Action Class in Struts2

In Struts 2, we can configure Spring to inject service instanced using Dependency Injection into Action classes. But sometime we need these service class instances in other components who are are Spring aware. For example you need AccountsService instance in StudentService class. For such purpose I have createds util class which can return me reference to any service instance at run time, in any class. I call is SpringContext, here is its definition: package com.bitspedia.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContext implements ApplicationContextAware { private static ApplicationContext CONTEXT; public void setApplicationContext(ApplicationContext context) throws BeansException { CONTEXT = context; } public static Object getBean(String beanName) { return CONTEXT.getBean(beanName); } } Now getting the r...