映射控制器时出现问题



我完全无法将控制器映射到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
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
//rest of service code
}

错误位于标头中,并试图在user上调用getFirstName(),但它似乎没有得到(这也是为什么我假设控制器没有得到映射(。

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

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

我觉得这很可能是由我一直错过的一些非常小的事情引起的。

EDIT2

我决定看看如果我(再次(使用value = "/new_users/{courseId}"尝试
@RequestParam(value = "userName") String userName@PathVariable("courseId") Long 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

你的控制器看起来很好,你能检查一下你是否用@Autowired注释注释了courseService和userService吗。

@Autowired
private CourseService courseService

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

我知道,通过这种方式,您可以访问添加到模型中的用户值,并且始终为每个属性创建相应的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>

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

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

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

最新更新