无法将数据保存到 mysql 数据库,在 gradle 项目中,bean 名称 'goal' 的 BindingResult 和普通目标对象都不能用作请求属性
not able to savedata to mysql db, in gradle project, Neither BindingResult nor plain target object for bean name 'goal' available as request attribute
我正在观看教程,我创建了用户登录并验证了用户,当我创建 addGoal 时,我的目标不是进入我的 mysql 数据库,因为我能够登录所以我的数据库连接是正确,我正在使用 thymeleaf 和 javaconfig 作为我的注释,请帮助我,我是新手。我认为我的 addGoal.html 有错误,因为我正在使用 thymeleaf 我认为我没有正确执行,请有人帮我修复它,我认为我没有正确处理 @modelattribute
目标 class 是:
package com.pluralsight.model;
import hello.User;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Range;
import org.springframework.web.bind.annotation.ModelAttribute;
@Entity
@Table(name="goals")
public class Goal {
public static final String FIND_ALL_GOALS = "findALLGoals";
public static final String FIND_GOAL_REPORTS = "findGoalReports";
@Id
@GeneratedValue
@Column(name="GOAL_ID")
private Long id;
@Range(min = 1, max = 120)
@Column(name="MINUTES")
private int minutes;
// @OneToMany(mappedBy="goal",cascade=CascadeType.ALL, fetch=FetchType.LAZY)
// private List<Exercise> exercises = new ArrayList<Exercise>();
@ManyToOne
@JoinColumn(name="USER_NAME")
private User user;
//
// public List<Exercise> getExercises() {
// return exercises;
// }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getId() {
return id;
}
public int getMinutes() {
return minutes;
}
// public void setExercises(List<Exercise> exercises) {
// this.exercises = exercises;
// }
public void setId(Long id) {
this.id = id;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
}
这是我的goalcontroller.java
package com.pluralsight.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.pluralsight.model.Goal;
import com.pluralsight.model.GoalReport;
import com.pluralsight.model.User;
import com.pluralsight.service.GoalService;
@Controller
@SessionAttributes("goal")
public class GoalController {
@Autowired
private GoalService goalService;
@RequestMapping(value = "addGoal", method = RequestMethod.GET)
public String addGoal(Model model, HttpSession session ) {
//Goal goal = new Goal();
Goal goal = (Goal)session.getAttribute("goal");
if(goal == null){
goal = new Goal();
goal.setMinutes(10);
}
model.addAttribute("goal", goal);
return "addGoal";
}
@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute Goal goal, BindingResult result) {
System.out.println("result has errors: " + result.hasErrors());
System.out.println("Goal set: " + goal.getMinutes());
if(result.hasErrors()) {
return "addGoal";
}else{
goalService.save(goal);
}
return "redirect:index.jsp";
}
@RequestMapping(value="getGoals", method= RequestMethod.GET)
public String getGoals(Model model){
List<Goal> goals = goalService.findAllGoals();
model.addAttribute("goals", goals);
return "getGoals";
}
@RequestMapping(value="getGoalReports", method= RequestMethod.GET)
public String getGoalReports(Model model){
List<GoalReport> goalReports = goalService.findAllGoalReports();
model.addAttribute("goalReports", goalReports);
return "getGoalReports";
}
}
这是我的 GoalRepository
package com.pluralsight.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.pluralsight.model.Goal;
@Repository("goalRepository")
public interface GoalRepository extends JpaRepository<Goal, Long>{
}
这是我的 GoalServiceImpl.java
package com.pluralsight.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.pluralsight.model.Goal;
import com.pluralsight.model.GoalReport;
import com.pluralsight.repository.GoalRepository;
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
public List<Goal> findAllGoals() {
return (List<Goal>) goalRepository.findAll();
}
public List<GoalReport> findAllGoalReports() {
return null;
// return goalRepository.findAllGoalReports();
}
}
这是我的 addGoal.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
</head>
<body>
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#"> Add Goal </a>
<ul class="nav"></ul>
</div>
</div>
</div>
<div class="container">
<div>
<h1>Add Goal</h1>
<p>Add your workout goal in minutes for the day.</p>
</div>
<form th:action="@{/addGoal}" method="post">
<div>
<label> Enter Minutes : <input type="text" name="minutes" />
</label>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
<div class="control-group"></div>
</div>
</body>
</html>
当我将 addGoal.html 中的表单更改为
<form action="#" th:action="@{/addGoal}" th:object="${goal}" method="post">
<p> enter Minutes: <input type="text" th:field="*{goal.minutes}"/></p>
<p><input type="submit" value="enter goal minutes"/></p>
</form>
然后我收到错误消息:bean 名称的 BindingResult 和普通目标对象都没有 'goal' 可用作请求属性
让我们退后一步,先关注不安的状态。
对于您的表格,请使用此:
<form th:object="${goal}" th:action="@{/addGoal}" method="post">
<div>
<label> Enter Minutes : <input type="text" th:field="*{minutes}" />
</label>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
下一个变化:
@RequestMapping(value = "addGoal", method = RequestMethod.GET)
public String addGoal(Model model, HttpSession session ) {
Goal goal = new Goal();
Goal sessionGoal = (Goal)session.getAttribute("goal");
if(sessionGoal == null && sessionGoal.getMinutes() == 0){
goal.setMinutes(10);
}
else {
goal.setMinutes(sessionGoal.getMinutes());
}
model.addAttribute("goal", goal);
return "addGoal";
}
你也在做return"redirect:index.jsp";但是您正在使用 Thymeleaf。这是行不通的。
您需要 "redirect:/index" 或 "redirect:/" 取决于您的家庭参数。
编辑 1:
在 addGoal 的 POST 方法中更改以下行:
@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {
我正在观看教程,我创建了用户登录并验证了用户,当我创建 addGoal 时,我的目标不是进入我的 mysql 数据库,因为我能够登录所以我的数据库连接是正确,我正在使用 thymeleaf 和 javaconfig 作为我的注释,请帮助我,我是新手。我认为我的 addGoal.html 有错误,因为我正在使用 thymeleaf 我认为我没有正确执行,请有人帮我修复它,我认为我没有正确处理 @modelattribute
目标 class 是:
package com.pluralsight.model;
import hello.User;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Range;
import org.springframework.web.bind.annotation.ModelAttribute;
@Entity
@Table(name="goals")
public class Goal {
public static final String FIND_ALL_GOALS = "findALLGoals";
public static final String FIND_GOAL_REPORTS = "findGoalReports";
@Id
@GeneratedValue
@Column(name="GOAL_ID")
private Long id;
@Range(min = 1, max = 120)
@Column(name="MINUTES")
private int minutes;
// @OneToMany(mappedBy="goal",cascade=CascadeType.ALL, fetch=FetchType.LAZY)
// private List<Exercise> exercises = new ArrayList<Exercise>();
@ManyToOne
@JoinColumn(name="USER_NAME")
private User user;
//
// public List<Exercise> getExercises() {
// return exercises;
// }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getId() {
return id;
}
public int getMinutes() {
return minutes;
}
// public void setExercises(List<Exercise> exercises) {
// this.exercises = exercises;
// }
public void setId(Long id) {
this.id = id;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
}
这是我的goalcontroller.java
package com.pluralsight.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.pluralsight.model.Goal;
import com.pluralsight.model.GoalReport;
import com.pluralsight.model.User;
import com.pluralsight.service.GoalService;
@Controller
@SessionAttributes("goal")
public class GoalController {
@Autowired
private GoalService goalService;
@RequestMapping(value = "addGoal", method = RequestMethod.GET)
public String addGoal(Model model, HttpSession session ) {
//Goal goal = new Goal();
Goal goal = (Goal)session.getAttribute("goal");
if(goal == null){
goal = new Goal();
goal.setMinutes(10);
}
model.addAttribute("goal", goal);
return "addGoal";
}
@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute Goal goal, BindingResult result) {
System.out.println("result has errors: " + result.hasErrors());
System.out.println("Goal set: " + goal.getMinutes());
if(result.hasErrors()) {
return "addGoal";
}else{
goalService.save(goal);
}
return "redirect:index.jsp";
}
@RequestMapping(value="getGoals", method= RequestMethod.GET)
public String getGoals(Model model){
List<Goal> goals = goalService.findAllGoals();
model.addAttribute("goals", goals);
return "getGoals";
}
@RequestMapping(value="getGoalReports", method= RequestMethod.GET)
public String getGoalReports(Model model){
List<GoalReport> goalReports = goalService.findAllGoalReports();
model.addAttribute("goalReports", goalReports);
return "getGoalReports";
}
}
这是我的 GoalRepository
package com.pluralsight.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.pluralsight.model.Goal;
@Repository("goalRepository")
public interface GoalRepository extends JpaRepository<Goal, Long>{
}
这是我的 GoalServiceImpl.java
package com.pluralsight.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.pluralsight.model.Goal;
import com.pluralsight.model.GoalReport;
import com.pluralsight.repository.GoalRepository;
@Service("goalService")
public class GoalServiceImpl implements GoalService {
@Autowired
private GoalRepository goalRepository;
@Transactional
public Goal save(Goal goal) {
return goalRepository.save(goal);
}
public List<Goal> findAllGoals() {
return (List<Goal>) goalRepository.findAll();
}
public List<GoalReport> findAllGoalReports() {
return null;
// return goalRepository.findAllGoalReports();
}
}
这是我的 addGoal.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
</head>
<body>
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#"> Add Goal </a>
<ul class="nav"></ul>
</div>
</div>
</div>
<div class="container">
<div>
<h1>Add Goal</h1>
<p>Add your workout goal in minutes for the day.</p>
</div>
<form th:action="@{/addGoal}" method="post">
<div>
<label> Enter Minutes : <input type="text" name="minutes" />
</label>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
<div class="control-group"></div>
</div>
</body>
</html>
当我将 addGoal.html 中的表单更改为
<form action="#" th:action="@{/addGoal}" th:object="${goal}" method="post">
<p> enter Minutes: <input type="text" th:field="*{goal.minutes}"/></p>
<p><input type="submit" value="enter goal minutes"/></p>
</form>
然后我收到错误消息:bean 名称的 BindingResult 和普通目标对象都没有 'goal' 可用作请求属性
让我们退后一步,先关注不安的状态。 对于您的表格,请使用此:
<form th:object="${goal}" th:action="@{/addGoal}" method="post">
<div>
<label> Enter Minutes : <input type="text" th:field="*{minutes}" />
</label>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
下一个变化:
@RequestMapping(value = "addGoal", method = RequestMethod.GET)
public String addGoal(Model model, HttpSession session ) {
Goal goal = new Goal();
Goal sessionGoal = (Goal)session.getAttribute("goal");
if(sessionGoal == null && sessionGoal.getMinutes() == 0){
goal.setMinutes(10);
}
else {
goal.setMinutes(sessionGoal.getMinutes());
}
model.addAttribute("goal", goal);
return "addGoal";
}
你也在做return"redirect:index.jsp";但是您正在使用 Thymeleaf。这是行不通的。 您需要 "redirect:/index" 或 "redirect:/" 取决于您的家庭参数。
编辑 1: 在 addGoal 的 POST 方法中更改以下行:
@RequestMapping(value = "addGoal", method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {