gpt4 book ai didi

java - Spring MVC中Controller层和Service层如何实现正确的交互

转载 作者:行者123 更新时间:2023-12-01 09:57:06 25 4
gpt4 key购买 nike

我使用 Spring MVC、Spring Security 和 Hibernate 设计了 ​​Web 应用程序。我有与服务层交互的 Controller :

@Controller
@RequestMapping(value="/")
public class InitController {

@Autowired
private UserService userService;


@Autowired
private StudentService studentService;

@InitBinder
public void initBinder(WebDataBinder dataBinder){
dataBinder.registerCustomEditor(String.class, "studentGroup", new StudentNameEditor());
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView getLoginForm(){
return new ModelAndView("login");
}

@RequestMapping(value = "/registration.html",method = RequestMethod.GET)
public ModelAndView getRegistrationForm(){
return new ModelAndView("registration");
}

@RequestMapping(value = "/students.html",method = RequestMethod.GET)
public ModelAndView getGroupForm(){
return new ModelAndView("searchStudents");
}


@RequestMapping(value = "/getStudents.html",method = RequestMethod.POST)
public ModelAndView getStudents(@Valid @ModelAttribute("student") Student student,
BindingResult result){
if(result.hasErrors()){
return new ModelAndView("searchStudents");
}else{
return studentService.getStudentOfGroup(student);
}

}

@RequestMapping(value = "/registrationConfirm.html",method = RequestMethod.POST)
public ModelAndView registration(@Valid @ModelAttribute("user") User user,
BindingResult result){
if(result.hasErrors()){
return new ModelAndView("registration");
}else{
User savedUser = userService.registerUser(user);
if(Objects.isNull(savedUser)){
ModelAndView modelAndView = new ModelAndView("login");
modelAndView.addObject("resultRegistration",
"Success registration!");
return modelAndView;
}else{
ModelAndView modelAndView = new ModelAndView("registration");
modelAndView.addObject("resultRegistration",
"User with the same login or password is registered in system already");
return modelAndView;
}
}
}
}

服务层与DAO层交互:

@Transactional(propagation = Propagation.REQUIRES_NEW )
@Service("userService")
public class UserServiceImpl implements UserService {

@Autowired
private UserDAO userDao;

@Transactional(propagation = Propagation.REQUIRES_NEW )
@Override
public User registerUser(User user) {
User userWithTheSameLogin = userDao.getUserByLogin(user.getUserLogin());
if(!Objects.isNull(userWithTheSameLogin)){
//if user with the same login registered already
return userWithTheSameLogin;
}else{
User userWithTheSamePassword = userDao.getUserByEmail(user.getUserEmail());
if(!Objects.isNull(userWithTheSamePassword)){
//if user with the same email registered already
return userWithTheSamePassword;
}
else{
//if user's credentials are unique
userDao.saveUser(user);
return null;
}
}

}
}

当电子邮件或登录存在时,我从服务层返回用户,当保存成功时返回 null。

我认为简单的返回值 - 它很糟糕,它不是逻辑,也不明显。请告诉我通知 Controller 保存用户结果的最佳方法。它是抛出异常并在 Controller 中捕获它们还是从服务层返回状态代码?请举例说明。我将不胜感激

最佳答案

我的建议是返回一个 boolean 值,其中如果用户可以注册,则 registerUser 为 true,否则为 false:

@Transactional(propagation = Propagation.REQUIRES_NEW )
@Override
public boolean registerUser(User user) {
User userWithTheSameLogin = userDao.getUserByLogin(user.getUserLogin());
if(!Objects.isNull(userWithTheSameLogin)){
//if user with the same login registered already
return false;
}
User userWithTheSamePassword = userDao.getUserByEmail(user.getUserEmail());
if(!Objects.isNull(userWithTheSamePassword)){
//if user with the same email registered already
return false;
}
//if user's credentials are unique
userDao.saveUser(user);
return true;
}

在你的 Controller 中你将拥有:

@RequestMapping(value = "/registrationConfirm.html",method = RequestMethod.POST)
public ModelAndView registration(@Valid @ModelAttribute("user") User user,
BindingResult result){
if(result.hasErrors()){
return new ModelAndView("registration");
}
if(userService.registerUser(user)){
ModelAndView modelAndView = new ModelAndView("login");
modelAndView.addObject("resultRegistration", "Success registration!");
return modelAndView;
}
//If the other cases didn't hold true, it means the registration failed
ModelAndView modelAndView = new ModelAndView("registration");
modelAndView.addObject("resultRegistration",
"User with the same login or password is registered in system already");
return modelAndView;
}

这感觉更具可读性并且摆脱了空检查。希望您也感觉“更好”。

关于java - Spring MVC中Controller层和Service层如何实现正确的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37110771/

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