2008-09-29 21 views
5

我使用的是spring 2.5,并且使用注释来配置我的控制器。如果我没有实现任何其他接口,我的控制器工作正常,但是当我添加接口实现时,spring容器不能识别控制器/请求映射。当控制器扩展接口时不能识别注释的Spring-MVC控制器

我不明白为什么添加接口实现会混淆控制器和请求映射的配置。有任何想法吗?

所以,这个工程:

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 errors) { 
     String returnView = "redirect:index.html"; 

     if (errors.hasErrors()) { 
      returnView = "register"; 
     } else { 
      User newUser = new User(); 
      newUser.setUsername(command.getUsername()); 
      newUser.setPassword(passwordEncoder.encodePassword(command 
        .getPassword(), null)); 
      newUser.setEmailAddress(command.getEmailAddress()); 
      newUser.setFirstName(command.getFirstName()); 
      newUser.setLastName(command.getLastName()); 

      userService.registerNewUser(newUser); 
     } 
     return returnView; 

    } 

    public Validator getValidator() { 
     return registrationValidator; 
    } 
} 

但这并不:

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 extends ValidatingController { 

    @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 errors) { 
     String returnView = "redirect:index.html"; 

     if (errors.hasErrors()) { 
      returnView = "register"; 
     } else { 
      User newUser = new User(); 
      newUser.setUsername(command.getUsername()); 
      newUser.setPassword(passwordEncoder.encodePassword(command 
        .getPassword(), null)); 
      newUser.setEmailAddress(command.getEmailAddress()); 
      newUser.setFirstName(command.getFirstName()); 
      newUser.setLastName(command.getLastName()); 

      userService.registerNewUser(newUser); 
     } 
     return returnView; 

    } 

    public Validator getValidator() { 
     return registrationValidator; 
    } 
} 
+0

我应该添加控制器在我的spring上下文配置中注册以下内容: 并使用DefaultAnnotationHandlerMapping和AnnotationMethodHa ndlerAdapter – layne 2008-09-29 23:23:35

回答

0

我想你会发现,这个问题是继承和使用注释做,他们不拌匀。

您是否尝试过使用继承和SimpleFormController来实现上述应用程序上下文中配置的所有其他细节?这至少会将问题缩小到注释和继承问题。

3

莱恩,您所描述的问题,因为当你的控制器类实现一个接口发生的事情,但你所提供的代码示例中,当你的控制器类继承另一个类的你,ValidatingController出现问题。

也许父类也定义了一些Spring注解,并且Spring容器首先注意到它们,并将控制器类分类为该类型的管理对象,并且不费心检查也在子类中定义的@Controller注释。只是一个猜测,但如果出现这种情况,我建议将它报告给Spring团队,因为它听起来像一个bug。

0

默认情况下,JDK代理使用的界面,如果控制器实现一个接口作为targetClass不被使用

在你的servlet上下文的配置添加这个RequestMapping注释被忽略创建:

<aop:aspectj-autoproxy proxy-target-class="true"/>