Spring MVC的 - 实施WebArgumentResolver的 [英] Spring mvc - implementation of WebArgumentResolver
问题描述
我想创建自定义的控制方法参数的注释。
开始关注此问题<一href=\"http://stackoverflow.com/questions/3621266/how-to-pass-a-session-attribute-as-method-argument-parameter-with-spring-mvc\">How通过会话属性作为方法参数(参数)与Spring MVC的并以下@Bozho建议,我有这样的事情:
我的解析器
公共类SessionAttributeAnnotationResolver实现WebArgumentResolver { 公共对象resolveArgument(MethodParameter参数,
NativeWebRequest要求)抛出异常{
的System.out.println(我在这里);
注释[] = parameterAnnotations parameter.getParameterAnnotations();
类&LT;&GT;参数类型= parameter.getParameterType(); 对于(译注parameterAnnotation:parameterAnnotations){
如果(SessionAttribute.class.isInstance(parameterAnnotation)){
SessionAttribute sessionAttribute =(SessionAttribute)parameterAnnotation;
字符串参数名称= sessionAttribute.value();
布尔所需= sessionAttribute.required();
HttpServletRequest的HTT prequest =(HttpServletRequest的)要求
.getNativeRequest();
HttpSession的会议= HTT prequest.getSession(假);
对象result = NULL;
如果(会话!= NULL){
结果= session.getAttribute(参数名称);
}
如果(结果== NULL和放大器;&放大器;要求和放大器;&安培;会议== NULL)
raiseSessionRequiredException(参数名称,参数类型);
如果(结果== NULL和放大器;&安培;需要)
raiseMissingParameterException(参数名称,参数类型);
返回结果;
}
}
返回WebArgumentResolver.UNRESOLVED;
} 保护无效raiseMissingParameterException(字符串paramName配置,
类&LT;&GT; paramType)抛出异常{
抛出新IllegalStateException异常(缺少参数'+ paramName配置
+[+ paramType.getName()+])类型的;
} 保护无效raiseSessionRequiredException(字符串paramName配置,
类&LT;&GT; paramType)抛出异常{
抛出新HttpSessionRequiredException(
没有找到的HttpSession解决参数'+ paramName配置
+[+ paramType.getName()+])类型的;
}
}
注释
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
公共@interface SessionAttribute {
字符串值();
布尔需要()默认为true;
}
简单的控制器来检验一切
@Controller
@RequestMapping(/测试)
公共类{的TestController @RequestMapping(方法= RequestMethod.GET)
公共串T(@SessionAttribute(userEntity)UserEntity E2,型号model,HttpServletRequest的REQ){
的System.out.println(req.getSession()的getId());
UserEntity E =(UserEntity)req.getSession()的getAttribute(userEntity);
的System.out.println(e.getName());
的System.out.println(e2.getName());
返回登录;
}
}
最后,Spring配置
&LT;豆类:bean类=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&GT;
&LT;豆类:属性名=customArgumentResolverREF =sessionAttributeAnnotationResolver/&GT;
&LT; /豆:豆&GT;
&LT;豆类:豆类ID =sessionAttributeAnnotationResolver级=pl.meble.taboret.utils.SessionAttributeAnnotationResolver/&GT;
现在,一切似乎都为了我,但可能有一些愚蠢的错误,我做的,因为在执行控制器后,我得到
F0B282C93B74F8FA3F21A51F46D4D4D5
用户名
空值
使用Spring 3.1.0的ArgumentResolver现已更改为 HandlerMethodArgumentResolver
- 在此之前,它曾经是 WebArgumentResolver
- 相关答案是<一个href=\"http://stackoverflow.com/questions/12357817/upgrading-to-spring-3-1-seems-to-break-my-customwebargumentresolver/12358370#12358370\">here
一旦你已经写了一个新的HandlerMethodArgumentResolver这是不是从你当前的实现非常不同,你可以用这种方式进行注册:
&LT; MVC:注解驱动&GT;
&LT; MVC:参数-解析器&GT;
&LT;豆的id =sessionAttributeAnnotationResolver级=.. SessionAttributeAnnotationResolver&GT;
&LT; /豆&GT;
&LT; / MVC:参数-解析器&GT;
&LT; / MVC:注解驱动&GT;
I wanted to create custom controller method argument annotation.
Following this question How to pass a session attribute as method argument (parameter) with Spring MVC and following @Bozho advice I have something like this:
my resolver
public class SessionAttributeAnnotationResolver implements WebArgumentResolver {
public Object resolveArgument(MethodParameter parameter,
NativeWebRequest request) throws Exception {
System.out.println("I am here");
Annotation[] parameterAnnotations = parameter.getParameterAnnotations();
Class<?> parameterType = parameter.getParameterType();
for (Annotation parameterAnnotation : parameterAnnotations) {
if (SessionAttribute.class.isInstance(parameterAnnotation)) {
SessionAttribute sessionAttribute = (SessionAttribute) parameterAnnotation;
String parameterName = sessionAttribute.value();
boolean required = sessionAttribute.required();
HttpServletRequest httprequest = (HttpServletRequest) request
.getNativeRequest();
HttpSession session = httprequest.getSession(false);
Object result = null;
if (session != null) {
result = session.getAttribute(parameterName);
}
if (result == null && required && session == null)
raiseSessionRequiredException(parameterName, parameterType);
if (result == null && required)
raiseMissingParameterException(parameterName, parameterType);
return result;
}
}
return WebArgumentResolver.UNRESOLVED;
}
protected void raiseMissingParameterException(String paramName,
Class<?> paramType) throws Exception {
throw new IllegalStateException("Missing parameter '" + paramName
+ "' of type [" + paramType.getName() + "]");
}
protected void raiseSessionRequiredException(String paramName,
Class<?> paramType) throws Exception {
throw new HttpSessionRequiredException(
"No HttpSession found for resolving parameter '" + paramName
+ "' of type [" + paramType.getName() + "]");
}
}
the annotation
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface SessionAttribute {
String value();
boolean required() default true;
}
simple controller to test everything
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(method= RequestMethod.GET)
public String t(@SessionAttribute("userEntity") UserEntity e2,Model model,HttpServletRequest req){
System.out.println(req.getSession().getId());
UserEntity e=(UserEntity) req.getSession().getAttribute("userEntity");
System.out.println(e.getName());
System.out.println(e2.getName());
return "login";
}
}
and finally, Spring configuration
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="customArgumentResolver" ref="sessionAttributeAnnotationResolver"/>
</beans:bean>
<beans:bean id="sessionAttributeAnnotationResolver" class="pl.meble.taboret.utils.SessionAttributeAnnotationResolver"/>
now, everything seems in order to me, but there is probably some silly mistake that I done, because when controller is executed, I am getting
F0B282C93B74F8FA3F21A51F46D4D4D5
username
null
With Spring 3.1.0 the ArgumentResolver has now changed to HandlerMethodArgumentResolver
- prior to that it used to be WebArgumentResolver
- a related answer is here
Once you have written a new HandlerMethodArgumentResolver which is not very different from your current implementation you can register it this way:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean id="sessionAttributeAnnotationResolver" class="..SessionAttributeAnnotationResolver ">
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
这篇关于Spring MVC的 - 实施WebArgumentResolver的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!