我尝试了很多很多的组合-尝试使用ModelAndView并返回mav并尝试model.addAttribute并返回String,并在jsp页面中编写-自动选择和customerGet并尝试customerPost,但没有任何一种组合帮助我。
如何纠正代码?我只需要一个工作示例来了解如何将代码从控制器发送到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.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 base时-我找到了我的实体-还有第二个问题=如何正确创建新客户-只有在get methot控制器或methot中才需要写新内容,所以这行吗?使用hibernate-jpa-Crud存储库
Customer customer = new Customer();
User user = new User();
Address address = new Address();
customer.setAddress(address);
customer.setUser(user);
答案 0 :(得分:0)
BindingResult错误,但该错误不会添加到模型中。 (在您的情况下:“ customerGet”) 简要分析您的代码,您可以:
<spring:form action="${addAction}" modelAttribute="customerGet">
=>返回该视图的所有控制器方法中都必须具有model.addAttribute(“ customerGet”,new Customer())。 在两种情况下返回索引视图:
获取/ index / index =>我们在这里,一切都很好
POST / index / addCustomer =>您不在此处放置它,它将失败并显示绑定结果。
答案 1 :(得分:0)
由于没有返回放置在模型和视图中的BindingResult
对象,所以出现了CustomerGet
错误,只是返回了字符串“ index”,而是将用户发送到index.jsp
(不带模型属性)可填充表单。您的客户方法需要从您的ModelAndView
方法中返回一个customer
对象。如果您将customer
方法更改为如下所示,则它应该可以工作:
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView customer(Model model) {
ModelAndView modelAndView = new ModelAndView("index");
Customer customer = new Customer();
customer.setAddress(new Address());
customer.setUser(new User());
model.addAttribute("customerGet", customer);
return modelAndView;
}
这将创建一个ModelAndView
对象,该对象将“ index”指定为该视图的jsp页面,然后使用用户和地址填充新的Customer对象,并将其放在模型和视图上。要真正看到用值填充的for,显然您必须在Customer对象的属性上为firstName,lastName等设置值。请注意,我将方法上的@RequestMapping
更改为“ /”,以便页面将在localhost:8081/index
而不是localhost:8081/index/index
上提供。