2008-09-29 55 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"/>