J'utilise spring 2.5 et j'utilise des annotations pour configurer mes contrôleurs. Mon contrôleur fonctionne correctement si je n'implémente aucune interface supplémentaire, mais le conteneur de ressort ne reconnaît pas le mappage contrôleur/requête lorsque j'ajoute des implémentations d'interface.Le contrôleur Spring-MVC annoté n'est pas reconnu lorsque le contrôleur étend l'interface
Je n'arrive pas à comprendre pourquoi l'ajout d'une implémentation d'interface remet en cause la configuration du contrôleur et les mappages de requêtes. Des idées?
Alors, cela fonctionne:
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;
}
}
mais cela ne veut pas:
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;
}
}
je devrais ajouter que les contrôleurs sont enregistrés avec les éléments suivants dans mon contexte de printemps config: et en utilisant le DefaultAnnotationHandlerMapping et AnnotationMethodHa ndlerAdapter –
layne
Il ne semble pas que vous importiez ValidatingController - quel est le nom de classe complet? –
Je l'importe. C'est dans le même paquet que l'exemple de contrôleur ci-dessus. Je devrais ajouter le code ci-dessus fonctionne quand j'étend la classe de base ValidatingController, mais quand je code ValidatingController en tant qu'interface et que le RegistrationController l'implémente, alors il casse. – layne