表单未提交或对象未创建-无法弄清楚

时间:2018-11-08 15:21:38

标签: java spring thymeleaf

我正在使用Spring应用程序监视锻炼。昨天,我的锻炼创建工作正常,但是进行了一些小改动之后,它停止工作并显示。我尝试还原所做的更改,由于某种原因,它仍然无法正常工作。页面导航仍然运行良好,我已经检查了数据库,并且该数据库仍处于启动状态。我认为这可能是我的锻炼控制器出现的问题:

@Controller
public class WorkoutController {

@Autowired
private WorkoutService workoutService = new WorkoutService();

@RequestMapping(value = "/workout/createWorkout")
public String goToCreateWorkout(){
    return "createWorkout";
}

@RequestMapping(value = "/workout/createWorkout/workoutCreated")
public String goToWorkoutCreated(){
    return "workoutCreated";
}

@RequestMapping(value = "/workout/viewWorkout")
public String goToViewWorkout(){
    return "viewWorkout";
}

@RequestMapping(value = "/workout/deleteWorkout")
public String goToDeleteWorkout(){
    return "deleteWorkout";
}

@GetMapping("/workout/createWorkout")
public String workoutForm(Model model) {
    model.addAttribute("workout", new Workout());
    return "createWorkout";
}

@PostMapping("/workout/createWorkout")
public String submitWorkout(@ModelAttribute Workout workout,
                            HttpServletRequest request){
    Account account = (Account) request.getSession().getAttribute("loggedInUser");
    workout.setAccountName(account.getAccountName());
    workoutService.createWorkout(workout);
    return "workoutCreated";
}

@GetMapping("/workout/viewWorkout")
public String viewWorkouts(Model model){
    model.addAttribute("workouts", workoutService.getWorkouts());
    return "viewWorkout";
}
 }

或者我提交用户创建锻炼的表单:

            <form action="#" th:action="@{/workout/createWorkout/workoutCreated}" th:object="${workout}" method="post">
            Workout name:<br>
            <input type="text" class="formInput" name="workout-name" size="50"
                   th:field="*{workoutName}"><br>
            <div class="divider"></div>
            Description:<br>
            <input type="text" class="formInput" name="workout-desc" size="50"
                   th:field="*{description}"><br>
            <div class="divider"></div>
            Date:<br>
            <input type="text" class="formInput" name="workout-date" size="50"
                   th:field="*{workoutDate}"><br>
            <br><br>
            <input type="submit" class="input-box">
        </form>

我无法解决此问题,因为我没有抛出任何错误,所以我不知道问题出在哪里。任何建议表示赞赏。谢谢。

edit:问题绝对是我的executionService.createWorkout(workout);没有被调用。我只是不知道为什么不是。

0 个答案:

没有答案