在利用spring的mvc开发过程中,需要将User对象从session中取出来使用。参照网上的做法,我利用 了AnnotationMethodHandlerAdapter来解决这个问题。下面是XML代码,放到web-inf下面的springMVC配 置文件中:
<bean id="userArgumentResolver" class="com.greatwall.module.yhqxgl.interceptor.UserArgumentResolver" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="customArgumentResolver"> <ref bean="userArgumentResolver"/> </property> </bean>
下面是java代码:
package com.greatwall.module.yhqxgl.interceptor; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebArgumentResolver; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestAttributes; import com.greatwall.module.yhqxgl.YhqxglConstants; import com.greatwall.module.yhqxgl.domain.User; public class UserArgumentResolver implements WebArgumentResolver { public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception { if (methodParameter.getParameterType().equals(User.class)) { return webRequest.getAttribute (YhqxglConstants.GREATWALL_SESSION_USER, RequestAttributes.SCOPE_SESSION); } return UNRESOLVED; } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索springmvc
, springmvc+mysql
, springmvc 高并发
, import
, springframework
, unresolved import
, requestattribute
, Spring session
, springmvc controller
, 提示出现unresolved
Unresolved
spring注入session、controller 注入bean、controller 注入、controller无法注入、controller注入失败,以便于您获取更多的相关知识。