[英]add custom parameters to spring mvc controller
I have a custom dispatcher servlet which extends the default DispatcherServlet to do some validation for all requests. 我有一个自定义的调度程序Servlet,它扩展了默认的DispatcherServlet以对所有请求进行一些验证。
Since I will get all the parameters from a request(getInputStream()->Map) to do some validation, I want to pass the params to controller or add the params to the context where I can get them again from the cotroller. 由于我将从请求(getInputStream()-> Map)中获取所有参数以进行一些验证,因此我想将参数传递给控制器或将参数添加到上下文中,以便可以从cotroller再次获取它们。
Now I just put all the params to a global Map, but I wonder if there are some simple ways. 现在,我只是将所有参数放到了全局Map中,但是我想知道是否有一些简单的方法。
public class CustomDispatcherServlet extends DispatcherServlet {
private static final long serialVersionUID = 7250693017796274410L;
@Override
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
doFilter(request, response);
super.doDispatch(request, response);
}
...
private void doFilter(HttpServletRequest request, HttpServletResponse response) {
WLNResponse<String> error = null;
try {
boolean isSignValid = checkSignValidity(request);
...
private boolean checkSignValidity(HttpServletRequest request) throws IOException {
// pass this params to controller or somewhere I can get from controller
Map<String, Object> params = WebUtils.readParams(request);
...
The way I would go at validating params in the controller itself. 我在验证控制器本身中的参数时所采用的方法。 for instance
例如
@Controller
public ControllerClass
{
@RequestMapping(value = "/someurl", method = RequestMethod.GET, params = {
"requestParam"})
public void someMethod(@RequestParam(value = "requestParam") String requestParam)
{
System.out.println("This is the value of the RequestParam requestParam " + requestParam);
}
}
This way you can do your validation within the controller. 这样,您可以在控制器内进行验证。
The only thing this doesn't solve for is if the request being made is not resolved to a valid controller. 唯一不能解决的是,是否没有将发出的请求解析为有效的控制器。 For that I would use the annotation
@controllerAdvice
. 为此,我将使用注释
@controllerAdvice
。
当前,我只使用request.setAttribute()将参数放入属性中并从控制器获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.