sv1
sv1

Reputation: 79

Spring MVC - Session - Request

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!

Upvotes: 3

Views: 6940

Answers (3)

Daniel Alexiuc
Daniel Alexiuc

Reputation: 13240

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);
    ... 
}

Upvotes: 2

Raghuram
Raghuram

Reputation: 52635

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

Upvotes: 1

sinuhepop
sinuhepop

Reputation: 20297

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.

Upvotes: 2

Related Questions