哪里可以保存重复值(MVC)

时间:2017-12-09 17:57:21

标签: java spring-mvc

在我的Spring MVC项目中,我有以下jsp表单

1)Login
2)View with all groups
3) Clicking to group you will be redirected to student list of this group
4)In student view I can add new student to group

我的问题是:点击组后我将组ID传递给控制器​​并在模型中保存组ID

@RequestMapping(method = RequestMethod.GET)
    public String getStudents(@SessionAttribute("admin") final Admins admin,
            @RequestParam(name = "id") final int groupId,
            final Model model) {
        final List<Students> students = studentService.getStudentsByGroupid(groupId);
        model.addAttribute("students", students);
        model.addAttribute("groupId", groupId);
        return "students";
    }

如果要保存新用户,我将组ID作为隐藏参数传递给控制器​​

@RequestMapping(value = "doCreate", method = RequestMethod.POST)
    public String createStudent(@Valid @ModelAttribute("studentData") final Students student,
            final BindingResult bindingResult,
            @RequestParam("groupId") final int groupId,
            Model model) {
        model.addAttribute("groupId", groupId);
        if (bindingResult.hasErrors()) {
            model.addAttribute("message", bindingResult.getFieldError().getDefaultMessage());
            return "create_student";
        }
        final Groups group = groupService.getById(groupId);
        student.setGroupId(group);
        final boolean isSavedSeccessfully = studentService.save(student);
        if (!isSavedSeccessfully) {
            model.addAttribute("message", "No unique login");
            return "create_student";
        }
        return getStudents(admin, groupId, model);
    }

但是在这种情况下我应该将组ID传递给与学生CRUD相关的每个控制器以及模型中的控制器存储组ID。在这种情况下如何避免代码酝酿?我想在会话中保存组ID,但据我所知,会话属性应仅与用户信息相关

1 个答案:

答案 0 :(得分:0)

设置会话变量并将对象存储在其中。使用注释

@SessionAttributes("name")

如果将它存储在会话中,则可以。