我正在使用spring 2.5和注释来配置我的spring-mvc web上下文.不幸的是,我无法让以下工作.我不确定这是一个错误(似乎是这样),或者是否存在对注释和接口实现子类化如何工作的基本误解.
例如,
@Controller
@RequestMapping("url-mapping-here")
public class Foo {
@RequestMapping(method=RequestMethod.GET)
public void showForm() {
...
}
@RequestMapping(method=RequestMethod.POST)
public String processForm() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
工作良好.当上下文启动时,会发现此处理程序处理的URL,并且一切都很好.
然而,这不是:
@Controller
@RequestMapping("url-mapping-here")
public class Foo implements Bar {
@RequestMapping(method=RequestMethod.GET)
public void showForm() {
...
}
@RequestMapping(method=RequestMethod.POST)
public String processForm() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试拉出网址时,我得到以下令人讨厌的堆栈跟踪:
javax.servlet.ServletException: No adapter for handler [com.shaneleopard.web.controller.RegistrationController@e973e3]: Does your handler implement a supported interface like Controller?
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1091)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:874)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
Run Code Online (Sandbox Code Playgroud)
但是,如果我将Bar更改为抽象超类并让Foo扩展它,那么它再次起作用.
@Controller
@RequestMapping("url-mapping-here")
public class Foo extends Bar …Run Code Online (Sandbox Code Playgroud) 我正在使用spring 2.5,并使用注释来配置我的控制器.如果我没有实现任何其他接口,我的控制器工作正常,但是当我添加接口实现时,spring容器无法识别控制器/请求映射.
我无法弄清楚为什么添加接口实现会混淆控制器的配置和请求映射.有任何想法吗?
所以,这有效:
package com.shaneleopard.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.shaneleopard.model.User;
import com.shaneleopard.service.UserService;
import com.shaneleopard.validator.RegistrationValidator;
import com.shaneleopard.web.command.RegisterCommand;
@Controller
public class RegistrationController {
@Autowired
private UserService userService;
@Autowired
private Md5PasswordEncoder passwordEncoder;
@Autowired
private RegistrationValidator registrationValidator;
@RequestMapping( method = RequestMethod.GET, value = "/register.html" )
public void registerForm(@ModelAttribute RegisterCommand registerCommand) {
// no op
}
@RequestMapping( method = RequestMethod.POST, value = "/register.html" )
public String registerNewUser( @ModelAttribute RegisterCommand command,
Errors …Run Code Online (Sandbox Code Playgroud)