将参数传递给Spring MVC中的控制器方法

时间:2012-06-13 09:29:19

标签: spring controller code-injection

我有一个特殊的实体,用控制器实例化。 Duiring请求应该传递给handler的参数:

@Controller
@Scope("session")
public class HomeController {
    @Autowired
    Context context; // this thing should be passed into request.
    //context field from controller should be passed into request parameter
    //in order to set request attributes properly.
    //The instance of CommonRequest class should have reference to the context during
    //mapping HTTP request into CommonRequest object.
    @RequestMapping(value = "/do_smth", method = RequestMethod.POST)
    public void doSmth(CommonRequest request) {

    }
}

有没有办法做这个伎俩?

1 个答案:

答案 0 :(得分:1)

public void doSmth(CommonRequest request) {
    //context field from controller should be passed into request parameter.
    request.setAttribute("context",context); // if you want to get context back, use Context context = (Context)request.getAttribute("context");
}