gpt4 book ai didi

spring-mvc - Controller 扩展接口(interface)时无法识别带注释的 Spring-MVC Controller

转载 作者:行者123 更新时间:2023-12-04 21:01:14 26 4
gpt4 key购买 nike

我正在使用 spring 2.5,并且正在使用注释来配置我的 Controller 。如果我不实现任何其他接口(interface),我的 Controller 工作正常,但是当我添加接口(interface)实现时,spring 容器无法识别 Controller /请求映射。

我不明白为什么添加接口(interface)实现会弄乱 Controller 的配置和请求映射。有任何想法吗?

所以,这有效:

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;
}
}

最佳答案

莱恩,您将问题描述为在您的 Controller 类实现接口(interface)时发生,但在您提供的代码示例中,当您的 Controller 类扩展您的另一个类时会出现问题,ValidatingController .

可能父类也定义了一些 Spring 注解,Spring 容器首先注意到它们并将 Controller 类归类为该类型的托管对象,并没有费心去检查 @Controller。您还在子类中定义的注释。只是一个猜测,但如果成功,我建议将其报告给 Spring 团队,因为这听起来像是一个错误。

关于spring-mvc - Controller 扩展接口(interface)时无法识别带注释的 Spring-MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/151152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com