映射控制器的问题

时间:2021-04-13 22:07:11

标签: spring thymeleaf

我完全坚持将控制器映射到 URL。我已经搜索了很多,但无法弄清楚我做错了什么,除了映射没有发生的事实。所包含的部分是目前唯一不工作的部分。

控制器代码(包含另一个值为 "/course_users" 的控制器方法的更大控制器文件的一部分)

@RequestMapping(value = "/new_users", method = RequestMethod.GET)
public String addStudent(
        Model model,
        Principal principal,
        HttpServletRequest requestID, HttpServletRequest requestName)
{
    Long courseId = Long.parseLong(requestID.getParameter("id"));
    User currentUser = userService.findByEmail(principal.getName());
    Course currentCourse = courseService.findCourseById(courseId);
    model.addAttribute("user", currentUser);
    model.addAttribute("courseId", courseId);

    try{
        if(!currentUser.getRoles().contains(Role.ADMIN) && !currentUser.getRoles().contains(Role.LECTURER)){
            String errorMsg = "Nie masz wystarczających uprawnień, aby dodać kursanta";
            model.addAttribute("errorMsg", errorMsg);
            return "error";
        }

        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        String userName = requestName.getParameter("userName");


        User newStudent = userService.findByEmail(userName);
        courseService.addStudentToCourse(currentCourse, newStudent);
    }
    catch (IllegalArgumentException e){
        String errorMsg = "Podano nieprawidłowe argumenty podczas tworzenia kursu.";
        model.addAttribute("errorMsg", errorMsg);
        return "error";
    }

    return "course_users";
}

HTML 文件代码(文件名为“course_users.html”)

<div class="container pb-3">
<form th:action="@{/new_users}">
    <div class="form-group">
        <label for="userName">Dodawany Student:</label>
        <select class="form-control" id="userName" th:name="userName">
            <option th:each="student : ${users}" th:value="${student.getEmail()}" th:text="${student.getEmail()}"></option>
        </select>
    </div>
    <div class="pt-3">
        <button type="submit" class="btn btn-primary">Dodaj studenta</button>
    </div>
</form>

编辑

UserService的相关部分(课程Service同样标注)

@Service
public class UserService {
    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    //rest of service code
}

错误位于标题中,并试图在 getFirstName() 上调用 user,它似乎没有得到(以及为什么我假设控制器没有被映射).

我在使用第一种方法时遇到了相同的问题(和错误),我将 @PathVriable 换成 HttpServletRequest 解决了问题,遗憾的是这里没有这样的运气。

Method call: Attempted to call method getFirstName() on null context object

我觉得这很可能是由我一直遗漏的很小的事情引起的。

EDIT2

我决定看看如果我(再次)尝试会发生什么
@RequestParam(value = "userName") String userName@PathVariable("courseId") Long courseIdvalue = "/new_users/{courseId}"

在我交换之前大致相同

<option th:each="student : ${users}" th:value="${student.getEmail()}" th:text="${student.getEmail()}"></option>

<option th:each="student : ${course.getEnrolledStudents()}" th:value="${student.getEmail()}" th:text="${student.getEmail()}"></option>

它向我显示了选择列表中的一个条目,并在单击按钮时给了我一个预期的错误页面! (因为我试图第二次添加同一个人)

可能是我在 HTML 语法中搞砸了一些东西,或者使用/添加模型变量 users 错误?

3 个答案:

答案 0 :(得分:0)

您的控制器看起来不错,您可以检查您是否使用 @Autowired 注释对 courseService 和 userService 进行了注释。

@Autowired
private CourseService courseService

如果它不能解决您的问题,请分享您的错误消息。

答案 1 :(得分:0)

我知道通过这种方式您可以访问添加到模型的用户值,并且始终为每个属性创建相应的 getter 和 setter:

<select class="form-control" id="userName" th:name="userName">
    <option th:each="student : ${users}" th:value="${student.email}" 
     th:text="${student.email}"></option>
</select>

答案 2 :(得分:0)

终于找到并回答了! 与其尝试通过 model.addAttribute 添加模型属性,不如像这样添加:

@ModelAttribute("users")
    public List<User> users() {
        return userService.findAll();
    }

我仍然无法告诉您为什么另一种方法不起作用,但至少这里是一种有效的方法。