gpt4 book ai didi

java - 如何在 Controller 中制作删除按钮

转载 作者:行者123 更新时间:2023-11-30 06:43:02 25 4
gpt4 key购买 nike

尝试在我的 spring-boot/hibernate/thymeleaf 设置中创建删除按钮,但没有成功。我不确定这是否是我在 Controller 或 View 中做错的事情。不确定我做错了什么。感谢您的任何见解。

型号:

@Entity
@Table(schema = "toner_buyer")
public class Buyer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "BUYER_ID")
private Long buyerId;

@Column(name = "FIRST_NAME")
private String firstName;

@Column(name = "LAST_NAME")
private String lastName;

@Column(name = "BUYER_ADDRESS")
private String buyerAddress;

@OneToMany(targetEntity = OrderForm.class,cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "buyer")
private List<OrderForm> orders;

public Buyer(){}

public Buyer(String firstName, String lastName, String buyerAddress, Set<Toner> buyerToner) {
this.firstName = firstName;
this.lastName = lastName;
this.buyerAddress = buyerAddress;
}

public Long getBuyerId() {
return buyerId;
}

public void setBuyerId(Long buyerId) {
this.buyerId = buyerId;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getBuyerAddress() {
return buyerAddress;
}

public void setBuyerAddress(String buyerAddress) {
this.buyerAddress = buyerAddress;
}

}

Controller :

@Controller
public class BuyerController {

private BuyerService buyerService;

@Autowired
public void setBuyerService(BuyerService buyerService){
this.buyerService = buyerService;
}

@RequestMapping("/add-buyer")
public String showBuyerPager(Model model){
List<Buyer> buyers = buyerService.findAllBuyers();
model.addAttribute("buyers", buyers);
model.addAttribute("buyer", new Buyer());
return "add-buyer";
}

@GetMapping("/showBuyerForm")
public String addBuyerForm(Model model){
model.addAttribute("buyer", new Buyer());
return "add-buyer";
}

@PostMapping("/addBuyer")
public String postBuyerForm(@ModelAttribute("buyer") Buyer buyer, Model model){
buyerService.saveBuyer(buyer);
model.addAttribute("buyer", new Buyer());
return "redirect:/";
}

@PostMapping("/deleteBuyer")
public String deleteBuyer(@ModelAttribute("buyer") Buyer buyer, Model model){
model.addAttribute("buyer", new Buyer());
buyerService.deleteBuyer(buyer);
return "redirect:/";
}
}

thymeleaf

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
<link href="styles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<header> Welcome to Toner Stock </header>
<h1>Add Buyer</h1>
<div id="mynav" align="center">
<ul>
<li><a href="/">Home</a></li>
<li><a href="add-buyer">Add Buyer</a></li>
<li><a href="add-manager">Add Manager</a></li>
<li><a href="current-stock">Current Stock</a></li>
<li><a href="transactions">Transactions</a></li>
<li><a href="orders">Order Form</a></li>
</ul>
</div>

<div id="display-table" align="center">
<form th:action="@{/addBuyer}" th:object="${buyer}" style="width:100%" method="post">
<table>
<td><label>First Name: </label></td>
<td><input type="text" th:field="*{firstName}"/></td>
<td><label>Last Name: </label></td>
<td><input type="text" th:field="*{lastName}"/></td>
<td><label>Enter Address: </label></td>
<td><input type="text" th:field="*{buyerAddress}"/></td>
<td><input type="submit" value="save"/></td>
</table>
</form>
</div>
<div>
<div>
<table id="info-table" align="center" border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
<tr th:each="buyer : ${buyers}">
<td th:text="${buyer.firstName}"></td>
<td th:text="${buyer.lastName}"></td>
<td th:text="${buyer.buyerAddress}"></td>
<td>
<form th:action="@{/deleteBuyer}" th:object="${buyer}" method="post">
<input type="hidden" th:field="${buyer}"/>
<input type="submit" value="Delete"/>
</form>
</td>
</tr>
</table>
</div>
</div>
<div>
<select>
<option th:each="buyer : ${buyers}"
th:text="${buyer.firstName}"
th:value="${buyer.buyerId}"
></option>
</select>
</div>
<div>
<div>
</div>
</div>

</body>
</html>

打印跟踪:

2017-05-21 12:07:11.791  INFO 562 --- [           main] com.ronone.Application                   : Starting Application on Ronalds-MacBook-Pro.local with PID 562 (/Users/ronaldpitt/Desktop/TonerStock/target/classes started by ronaldpitt in /Users/ronaldpitt/Desktop/TonerStock)
2017-05-21 12:07:11.794 INFO 562 --- [ main] com.ronone.Application : No active profile set, falling back to default profiles: default
2017-05-21 12:07:12.055 INFO 562 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77be656f: startup date [Sun May 21 12:07:12 EDT 2017]; root of context hierarchy
2017-05-21 12:07:13.702 INFO 562 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$23cb376d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-05-21 12:07:14.133 INFO 562 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-05-21 12:07:14.154 INFO 562 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-05-21 12:07:14.155 INFO 562 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-05-21 12:07:14.295 INFO 562 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-05-21 12:07:14.295 INFO 562 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2245 ms
2017-05-21 12:07:14.503 INFO 562 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-05-21 12:07:14.509 INFO 562 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-05-21 12:07:14.510 INFO 562 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-05-21 12:07:14.510 INFO 562 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-05-21 12:07:14.510 INFO 562 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-05-21 12:07:15.412 INFO 562 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-05-21 12:07:15.448 INFO 562 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-05-21 12:07:15.576 INFO 562 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.9.Final}
2017-05-21 12:07:15.578 INFO 562 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-05-21 12:07:15.769 INFO 562 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-05-21 12:07:15.902 INFO 562 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-05-21 12:07:17.162 INFO 562 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-05-21 12:07:18.093 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77be656f: startup date [Sun May 21 12:07:12 EDT 2017]; root of context hierarchy
2017-05-21 12:07:18.234 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/add-buyer]}" onto public java.lang.String com.ronone.controller.BuyerController.showBuyerPager(org.springframework.ui.Model)
2017-05-21 12:07:18.235 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/showBuyerForm],methods=[GET]}" onto public java.lang.String com.ronone.controller.BuyerController.addBuyerForm(org.springframework.ui.Model)
2017-05-21 12:07:18.235 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/addBuyer],methods=[POST]}" onto public java.lang.String com.ronone.controller.BuyerController.postBuyerForm(com.ronone.entities.Buyer,org.springframework.ui.Model)
2017-05-21 12:07:18.236 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/deleteBuyer],methods=[POST]}" onto public java.lang.String com.ronone.controller.BuyerController.deleteBuyer(java.lang.Long,org.springframework.ui.Model)
2017-05-21 12:07:18.236 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.ronone.controller.MainController.homepage()
2017-05-21 12:07:18.238 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/add-manager]}" onto public java.lang.String com.ronone.controller.ManagerController.showManagerPage(org.springframework.ui.Model)
2017-05-21 12:07:18.238 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/showManagerForm],methods=[GET]}" onto public java.lang.String com.ronone.controller.ManagerController.addManagerForm(org.springframework.ui.Model)
2017-05-21 12:07:18.238 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/saveManager],methods=[POST]}" onto public java.lang.String com.ronone.controller.ManagerController.postManagerForm(com.ronone.entities.Manager,org.springframework.ui.Model)
2017-05-21 12:07:18.239 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/deleteManager],methods=[POST]}" onto public java.lang.String com.ronone.controller.ManagerController.deleteManager(com.ronone.entities.Manager,org.springframework.ui.Model)
2017-05-21 12:07:18.241 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/orders]}" onto public java.lang.String com.ronone.controller.OrderController.orderFormPage(org.springframework.ui.Model)
2017-05-21 12:07:18.241 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/orders],methods=[GET]}" onto public java.lang.String com.ronone.controller.OrderController.selectOrder(org.springframework.ui.Model)
2017-05-21 12:07:18.241 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/saveOrder],methods=[POST]}" onto public java.lang.String com.ronone.controller.OrderController.sendOrderToORM(com.ronone.entities.Toner,com.ronone.entities.Buyer,org.springframework.ui.Model)
2017-05-21 12:07:18.243 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/current-stock]}" onto public java.lang.String com.ronone.controller.StockController.showStockPage(org.springframework.ui.Model)
2017-05-21 12:07:18.244 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/showStockItem],methods=[GET]}" onto public java.lang.String com.ronone.controller.StockController.addStockItem(com.ronone.entities.Toner,org.springframework.ui.Model)
2017-05-21 12:07:18.244 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/saveToner],methods=[POST]}" onto public java.lang.String com.ronone.controller.StockController.saveStockItem(com.ronone.entities.Toner,org.springframework.ui.Model)
2017-05-21 12:07:18.245 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/transactions]}" onto public java.lang.String com.ronone.controller.TransController.showTransPage(org.springframework.ui.Model)
2017-05-21 12:07:18.249 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-05-21 12:07:18.249 INFO 562 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-05-21 12:07:18.327 INFO 562 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-05-21 12:07:18.327 INFO 562 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-05-21 12:07:18.401 INFO 562 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-05-21 12:07:19.222 INFO 562 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-05-21 12:07:19.307 INFO 562 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-05-21 12:07:19.314 INFO 562 --- [ main] com.ronone.Application : Started Application in 18.059 seconds (JVM running for 18.911)
2017-05-21 12:07:25.767 INFO 562 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-05-21 12:07:25.768 INFO 562 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-05-21 12:07:25.801 INFO 562 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 33 ms
2017-05-21 12:07:27.623 INFO 562 --- [nio-8080-exec-2] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2017-05-21 12:07:29.064 ERROR 562 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!] with root cause

java.lang.IllegalArgumentException: The given id must not be null!
at org.springframework.util.Assert.notNull(Assert.java:134) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.delete(SimpleJpaRepository.java:148) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at com.sun.proxy.$Proxy82.delete(Unknown Source) ~[na:na]
at com.ronone.service.BuyerService.deleteBuyer(BuyerService.java:26) ~[classes/:na]
at com.ronone.controller.BuyerController.deleteBuyer(BuyerController.java:48) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

最佳答案

通常,您只需要一个 ID 即可删除一个实体,而不是整个对象(并且您通常无法在单个 POST 参数中传递整个对象)。

假设您的 Buyer 类具有 id 属性(即一对 getId()/setId(long) 方法,你可以尝试放入

<input type="hidden" th:field="*{id}"/>

而不是

<input type="hidden" th:field="${buyer}"/>

此外,您还必须像这样更改处理程序方法:

@PostMapping("/deleteBuyer")
public String deleteBuyer(@RequestParameter("id") long buyerId) {
buyerService.deleteBuyer(buyerId);
return "redirect:/";
}

并且还更改您的 BuyerService#deleteBuyer() 方法以接受 long (买家 ID)而不是 Buyer 实例,并使用该 ID。

关于java - 如何在 Controller 中制作删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44096952/

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