如果你不想使用
@RequestMapping(value = "/add")
public void addMember(@RequestParam(value = "username") String username, ...) {
Member member = new Member(username, ...)
// ...
}
可以實現HandlerMethodArgumentResolver:
public class MemberHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return Member.class.equals(parameter.getParameterType());
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String username = webRequest.getParameter("username");
...
return new Member(username, ...);
}
}
而且在配置:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean id="memberResolver" class="com.company ... MemberHandlerMethodArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
或JavaConfi G:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
...
@Override
public void addArgumentResolvers(List<Handlermethodargumentresolver> argumentResolvers) {
argumentResolvers.add(new MemberHandlerMethodArgumentResolver());
}
}
你不能至少在沒有實現自己的'DataBinder'。 – 2014-11-21 13:02:21