使用SpringMVC开发,Controller层如何获取Interceptor层放入的参数
时间: 2024-04-29 10:25:31 浏览: 15
在Interceptor中,可以通过HandlerInterceptor的preHandle方法将参数放入HttpServletRequest中的attribute中,然后在Controller中通过HttpServletRequest的getAttribute方法获取:
Interceptor中:
```java
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("myParam", "ParamValue");
return true;
}
}
```
Controller中:
```java
@Controller
public class MyController {
@RequestMapping("/test")
public String test(HttpServletRequest request) {
String myParam = (String) request.getAttribute("myParam");
System.out.println(myParam); // 输出 "ParamValue"
return "test";
}
}
```