Spring Java如何使用控制器

时间:2018-04-21 11:45:02

标签: spring spring-mvc

我是Spring框架的新手。我有一个任务是在春天制作电话簿应用程序。我需要进行注册和授权以及我的电话簿。我有2个控制器,第一个控制授权和注册的UserController

@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
    model.addAttribute("userForm", new User());
    return "registration";
}

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm")
                                       User userForm, BindingResult bindingResult, Model model) {
    userValidator.validate(userForm, bindingResult);

    if (bindingResult.hasErrors()) {
        return "registration";
    }

    userService.save(userForm);

    securityService.autoLogin(userForm.getUsername(), userForm.getConfirmPassword());

    return "redirect:/welcome";
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
    if(error!=null) {
        model.addAttribute("error", "Username or password is incorrect.");
    }

    if (logout!=null) {
        model.addAttribute("message", "logged out successfully");
    }

    return "login";
}

@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public  String welcome(Model model) {
    return "welcome";
}

@RequestMapping(value = "/admin", method = RequestMethod.GET)
public String admin(Model model) {
    return "admin";
}
}

和ContactController控制我的功能(添加,删除,编辑和显示联系人)

@Controller
public class ContactController {

private ContactService contactService;

@Autowired(required = true)
@Qualifier(value = "contactService")
public void setContactService(ContactService contactService) {
    this.contactService = contactService;
}

@RequestMapping(value = {"admin", "welcome"}, method = RequestMethod.GET)
public String listContactsForAdmin(Model model) {
    model.addAttribute("contact", new Contact());
    model.addAttribute("listContacts", this.contactService.listContacts());
    return "admin";
}
@RequestMapping(value = "admin/add", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact") Contact contact) {
    if (contact.getId() == 0) {
        this.contactService.addContact(contact);
    } else {
        this.contactService.updateContact(contact);
    }
    return "redirect:/admin";
}

@RequestMapping("/remove/{id}")
public String removeContact(@PathVariable("id") int id) {
    this.contactService.removeContact(id);

    return "redirect:/admin";
}

@RequestMapping("/edit/{id}")
public String editBook(@PathVariable("id") int id, Model model) {
    model.addAttribute("contact", this.contactService.getContactById(id));
    model.addAttribute("listContacts", this.contactService.listContacts());

    return "admin";
}

@RequestMapping("contactData/{id}")
public String contactData(@PathVariable("id") int id, Model model) {
    model.addAttribute("contact", this.contactService.getContactById(id));

    return "contactData";
}

}

当我尝试进行身份验证或注册时。新用户我有这样的错误:

  

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.IllegalStateException:为HTTP路径'http://localhost:8087/admin'映射的不明确的处理程序方法:{public java.lang.String kz.adilka.springsecurity.app.controller.UserController.admin(org.springframework.ui) .Model),public java.lang.String kz.adilka.springsecurity.app.controller.ContactController.listContactsForAdmin(org.springframework.ui.Model)}

它说我有映射管理页面的问题。但对我来说似乎没问题,或者我错过了smth

1 个答案:

答案 0 :(得分:0)

原因是您没有为控制器设置控制器值,并且它们具有相同的RequestMapping方法

@Controller // do not have identifier here
public class UserController {
      @Autowired
      private UserService userService;
      @Autowired
      private SecurityService securityService;
      @Autowired
      private UserValidator userValidator;


     @RequestMapping(value = "/admin", method = RequestMethod.GET)
     public String admin(Model model) {
       return "admin";
     }
}


@Controller // do not have identifier here
public class ContactController {

  private ContactService contactService;

  @RequestMapping(value = {"admin", "welcome"}, 
          method = RequestMethod.GET)
  public String listContactsForAdmin(Model model) {
    model.addAttribute("contact", new Contact());
    model.addAttribute("listContacts", 
    this.contactService.listContacts());
    return "admin";
  }

}

一种可能的解决方案是每个Controller方法的集合RequestMapping

   @Controller(value = "user")
   public class UserController {
   @Autowired
   private UserService userService;
   @Autowired
   private SecurityService securityService;
   @Autowired
   private UserValidator userValidator;


   @RequestMapping(value = "/admin", method = RequestMethod.GET)
   public String admin(Model model) {
      return "admin";
   }
}


@Controller(value = "contact")
public class ContactController {

private ContactService contactService;

@RequestMapping(value = {"admin", "welcome"}, method = RequestMethod.GET)
public String listContactsForAdmin(Model model) {
    model.addAttribute("contact", new Contact());
    model.addAttribute("listContacts", this.contactService.listContacts());
    return "admin";
}

}