views:

56

answers:

3

I have a Spring MVC (3) Controller and I trying to put in the annotations but failed Heres my code outline

@Controller
public class SpringController{

@RequestMapping("/welcome")
public String myHandler(@RequestParam("id" String id)){

//My RequestParm is able to do the job of request.getParameter("id") 

  HttpSession session = request.getSession();

  session.setAttribute("name","Mike") ;

   return "myFirstJsp";

    }
@RequestMapping("/process")
public String processHandler(@RequestParam("processId" String processId)){

      //do stuff
      String someName = session.getAttribute("name");
      return "result";
        }

  }

Just for the sake of session object I have to declare HttpServletRequest and HttpSession. Is there anyway we can have a solution with @nnotations.

Thanks!

+1  A: 

You can declare HttpSession or HttpServletRequest as arguments in your handler and they'll be automatically informed.

public String myHandler(@RequestParam("id") String id, HttpServletRequest request) { ... }

There are a lot of different arguments and results for handlers. You can see them here.

Sinuhe
Thanks for your response. Yeah I see 'em but I am still unable to comprehend as to how to get the session object from the HttpServletRequest using annotations. If anyone can elucidate this that would be great!
sv1
You don't NEED annotations if you do it this way, why do you WANT them? @SessionAttributes, as @Raghuram says, or @Scope("session") are annotations related to web session, but they are probably unsuitable for what you want to do.
Sinuhe
A: 

In case you haven't, you should look at this documentation on SessionAttributes, to see if it is applicable for you.

Raghuram
A: 

If you don't like using HttpSession and want something managed by Spring which also has more scope control you can use org.springframework.web.context.request.WebRequest:

public String myHandler(@RequestParam("id") String id, WebRequest request) {
    request.getAttribute("name", SCOPE_REQUEST);
    ... 
}
Daniel Alexiuc