Spring MVC - Session - Request

I have a Spring MVC (3) controller, and I'm trying to put annotations, but failed. Heres my code out

@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 the session object, I have to declare HttpServletRequest and HttpSession. In any case, we may have a solution with @nnotations.

Thank!

+3
source share
3 answers

If you have not done so, you should look at this documentation on SessionAttributes to see if it applies to you.

+1
source

You can declare HttpSessioneither HttpServletRequestas arguments in your handler, and they will be automatically informed.

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

. .

+2

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

public String myHandler(@RequestParam("id") String id, WebRequest request) {
    request.getAttribute("name", SCOPE_REQUEST);
    ... 
}
+2
source

Source: https://habr.com/ru/post/1769679/


All Articles