gpt4 book ai didi

java - 既不是 BindingResult 也不是 bean 的普通目标对象

转载 作者:行者123 更新时间:2023-12-01 22:31:58 25 4
gpt4 key购买 nike

我尝试了很多组合 - 尝试使用 ModelAndView 并返回 mav 并尝试 model.addAttribute 并返回 String,并在 jsp 页面中写入 - 自动选择和 customerGet 并尝试 customerPost,但没有任何一种组合有帮助我根本。

如何更正代码?我只需要一个工作示例来了解如何将代码从 Controller 发送到 jsp,找不到任何有关它的工作示例 - 因为它们不适用于我的 mvc+jsp

@Getter
@Setter
@Entity
@Table(name = "customer")
@NoArgsConstructor
@EqualsAndHashCode
public class Customer implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "address_id")
private Address address;

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;



@Getter
@Setter
@Entity
@Table(name = "address")
@NoArgsConstructor
@EqualsAndHashCode
public class Address implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String country;
private String city;
private String street;
private String flat;

@OneToOne(mappedBy = "address")
private Customer customer;


@Getter
@Setter
@Entity
@Table(name = "users")
@NoArgsConstructor
@EqualsAndHashCode(of = "email")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String email;
private String password;
private boolean enable;

@OneToOne(mappedBy = "user")
private Customer customer;

我的错误代码

    @Controller
@RequestMapping(value = "index")
public class HomeController {

@Autowired
private CustomerService customerService;


// @RequestMapping(method = RequestMethod.GET)
// public ModelAndView getRegistrationForm() {
// Customer customer = new Customer();
// User user = new User();
// Address address = new Address();
// customer.setAddress(address);
// customer.setUser(user);
// ModelAndView modelAndView = new ModelAndView();
// modelAndView.addObject("customer", customer);
//// model.addAttribute("customer", new Customer());
// modelAndView.setViewName("customer");
// return modelAndView;
//// return new ModelAndView("register", "customer", customer);
//// return "index";
// }

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String customer(Model model) {
// ModelAndView modelAndView = new ModelAndView();
// Customer customer = new Customer();
// User user = new User();
// Address address = new Address();
// customer.setAddress(address);
// customer.setUser(user);
// model.addAttribute(user);
// model.addAttribute(address);
// model.addAttribute("customer", customer);
model.addAttribute("customerGet", new Customer());
return "index";
}

// return new ModelAndView("customer", "command", customer);
// }

@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
public String registerCustomer(@Valid @ModelAttribute(value = "customerPost") Customer customer, Model model,
BindingResult result) {
if (result.hasErrors())
return "index";
customerService.createCustomer(customer);
model.addAttribute("registrationSuccess", "Registered Successfully.");
return "index";
}

// @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
// public String registerCustomer
// (@ModelAttribute("customer") Customer customer, Model model,
// BindingResult result) {
// if (result.hasErrors()) return "register";
// customerService.createCustomer(customer);
// model.addAttribute("registrationSuccess", "Registered Successfully.");
// return "redirect:/index";
// }
// @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
// public String addStudent(@ModelAttribute("customer")Customer customer,
// ModelMap model) {
// model.addAttribute("firstName", customer.getFirstName());
// model.addAttribute("lastName", customer.getLastName());
// model.addAttribute("country", customer.getAddress().getCountry());
// model.addAttribute("city", customer.getAddress().getCity());
// model.addAttribute("street", customer.getAddress().getStreet());
// model.addAttribute("flat", customer.getAddress().getFlat());
// model.addAttribute("email", customer.getUser().getEmail());
// model.addAttribute("password", customer.getUser().getPassword());
// customerService.createCustomer(customer);
// return "index";
// }
}

我的application.properties spring-boot

spring.mvc.view.prefix=/WEB-INF/pages/spring.mvc.view.suffix=.jsp

mybad jsp

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>

<c:url var="addAction" value="/index/addCustomer" ></c:url>

<spring:form action="${addAction}" modelAttribute="customerGet">
<table>
<thead>
<tr>
<th>Create Student</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<%-- <spring:input path="id" readonly="true" size="8" disabled="true" />--%>
<spring:hidden path="id" />
</td>
</tr>
<tr>
<td>first name</td>
<td><spring:input path="firstName"/></td>
</tr>
<tr>
<td>last name</td>
<td><spring:input path="lastName"/></td>
</tr>
<tr>
<td>country</td>
<td><spring:input path="address.country"/></td>
</tr>
<tr>
<td>city</td>
<td><spring:input path="address.city"/></td>
</tr>
<tr>
<td>street</td>
<td><spring:input path="address.street"/></td>
</tr>
<tr>
<td>flat</td>
<td><spring:input path="address.flat"/></td>
</tr>
<tr>
<td>email</td>
<td><spring:input path="user.email"/></td>
</tr>
<tr>
<td>password</td>
<td><spring:input path="user.password"/></td>
</tr>
</tbody>
</table>
<spring:button>add customer</spring:button>
</spring:form>

</body>

</html>

我在 http://localhost:8081/index/index 找到页面完成表单并选择按钮后再次出现错误我自动重定向到页面在 http://localhost:8081/index/addCustomer

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Oct 24 16:14:59 UTC 2019
There was an unexpected error (type=Internal Server Error, status=500).
Neither BindingResult nor plain target object for bean name 'customerGet' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'customerGet' available as request attribute

此外,当我看到 mysql 库时 - 我找到了我的实体 - 我有第二个问题=如何正确创建新客户 - 只有在 get methot Controller 中或在 methot 中才需要写下这一行?使用 hibernate-jpa - CRUD 存储库

   Customer customer = new Customer();
User user = new User();
Address address = new Address();
customer.setAddress(address);
customer.setUser(user);

最佳答案

当您期望 View 中存在对象但未将其添加到模型中时,会引发 BindingResult 错误。 (在您的情况下:“customerGet”)对您的代码进行简短分析,您将得到:

<spring:form action="${addAction}" modelAttribute="customerGet">

=> 您必须在返回此 View 的所有 Controller 方法中使用 model.addAttribute("customerGet", new Customer()) 。索引 View 有两种情况返回:

  1. GET/index/index => 我们这里有,一切都很好

  2. POST/index/addCustomer => 您没有这里有它,它将失败并显示绑定(bind)结果。

关于java - 既不是 BindingResult 也不是 bean 的普通目标对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58545157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com