获取HttpServletRequest

获取HttpServletRequest

This question already has answers here:
Spring 3 MVC accessing HttpRequest from controller
                                
                                    (3个答案)
                                
                        
                                5年前关闭。
            
                    
我已经编写了一个控制器类和一个动作方法,例如

@Controller
public class Controller
{
    @RequestParam("/test.htm")
    public String action(Model model)
    {
         //Something
    }
}


如何在动作的方法主体中获取HttpServletRequest对象?

最佳答案

您可以通过在操作方法中传递引用来获取HttpServletRequest的实例:

@Controller
public class Controller
{
    @RequestParam("/test.htm")
    public String action(Model model, HttpServletRequest request)
    {
        //Something
        try {
            request.getParameter("user_name"); //write in try-catch block
        } catch(ServletException ex) {
        }
    }
}

07-24 15:36