728x90
Custom arguments for @RequestMapping methods
So, Spring MVC fans, let's say you have a
@RequestMapping
-annotated method in your controller, but you would like to include a custom argument, in addition to the standard ones like
Model
,
HttpServletRequest
, and
HttpServletResponse
.
The solution is to implement a
The solution is to implement a
WebArgumentResolver
. As an example, we'll create a
CurrentDateWebArgumentResolver
; as its name suggests, this resolver will be able to set any
Date
argument in the controller method to current date.
We have to write
We have to write
12345678910111213public class ContextExtractingWebArgumentResolver
implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
if (Date.class == methodParameter.getParameterType()) {
return new Date();
}
return UNRESOLVED;
}
}
implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
if (Date.class == methodParameter.getParameterType()) {
return new Date();
}
return UNRESOLVED;
}
}
We then wire-in the argument resolver in our Spring application context configuration file:
1234567<bean class="org.springframework.web.servlet.mvc.annotation.
AnnotationMethodHandlerAdapter"
<property name="customArgumentResolver">
<bean class="package.ContextExtractingWebArgumentResolver"/>
</property>
</bean>
AnnotationMethodHandlerAdapter"
<property name="customArgumentResolver">
<bean class="package.ContextExtractingWebArgumentResolver"/>
</property>
</bean>
Once done, we can create a
@RequestMapping
-annotated method in our controller and include the Date argument; that argument will now receive the current date. For example, we can have
12345678@Controller
public class HomeController {
@RequestMapping(value="/index", method=RequestMethod.GET)
public void index(Date date, Model m) {
m.addAttribute("now", date);
}
}
public class HomeController {
@RequestMapping(value="/index", method=RequestMethod.GET)
public void index(Date date, Model m) {
m.addAttribute("now", date);
}
}
The model that will be passed to the view after making a GET request to the
/index
URL will include attribute named
now
; the value of the attribute will be the current time.
Naturally, this is just a simple example and injecting current date is not very enterprisey thing to do, but using the approach I have outlined, you can now set much more useful argument types. Do not forget that the implementation of the
Naturally, this is just a simple example and injecting current date is not very enterprisey thing to do, but using the approach I have outlined, you can now set much more useful argument types. Do not forget that the implementation of the
WebArgumentResolver
is a Spring bean, thus having access to all features available in the Spring application context.
728x90
'Spring' 카테고리의 다른 글
@RequestMapping 핸들러 매핑 (0) | 2015.10.08 |
---|---|
스프링(Spring)에서 no cache 설정하기 (0) | 2015.10.08 |
전자정부 암호화/복호화 Encryption / Decryption (0) | 2015.10.07 |
전자정부표준프레임워크 암호화 aria (0) | 2015.10.07 |
spring 2.5 task scheduler (0) | 2015.10.07 |