从前一个控制器重定向时未调用 Post 方法
Posted
技术标签:
【中文标题】从前一个控制器重定向时未调用 Post 方法【英文标题】:Post method not called when redirected from previous controller 【发布时间】:2019-01-10 01:25:59 【问题描述】:所以我有 3 个 jsp 表单。第一个带有一般信息。在完成第一个表单时,它应该基于下拉列表的值返回表单 2 或表单 3。到目前为止,对象已正确写入数据库,执行检查并显示正确的 jsp-form,但是当用户填写第二个表单时,什么也没有发生,读取第二个表单的 postmethod 不会被调用。 任何有关如何解决此问题的提示将不胜感激。 我已经绞尽脑汁在互联网上搜索了好几天,但找不到一个直接的答案。
通用控制器:
@Controller
public class GeneralController
@Autowired
private GeneralRepo repo;
@GetMapping("new")
public String getNew(Map<String, Object> model)
WrapperClass wrapper = new WrapperClass();
model.put("wrapperForm", wrapper);
return "newRecord";
@PostMapping("new")
public String handlePost(@ModelAttribute("wrapperForm") WrapperClass wrapper)
Address address = new Address(wrapper.getNameOrg(), wrapper.getStreet(), wrapper.getNumber(), wrapper.getZip(),
wrapper.getCity(), wrapper.getTelephone(), wrapper.getEmail());
General general = new General(wrapper.getLanguage(), wrapper.getApplication(), wrapper.getNameKbo(), wrapper.getKboNumber(),
address, wrapper.getApplicationType(), wrapper.getNotes(), null, null, null, null);
String url;
if (general.getApplicationType().equalsIgnoreCase("voortzetting"))
url = "invoices";
else
url = "employee";
repo.save(general);
return "redirect:/" + url;
一般形式:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Record Toevoegen</title>
<link rel="stylesheet" href="$pageContext.request.contextPath/css/general.css">
<link href="$pageContext.request.contextPath/css/bootstrap.min.css" rel="stylesheet">
<link href="$pageContext.request.contextPath/css/modern-business.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="$pageContext.request.contextPath/css/customUtilities.css" rel="stylesheet">
<link href="$pageContext.request.contextPath/css/general.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
<link rel="icon" href="$pageContext.request.contextPath/images/vigilis.jpg" type="image/x-icon">
</head>
<body>
<%@include file="navigation.jsp" %>
<%--@elvariable id="wrapperForm" type=""--%>
<form:form method="post" commandName="wrapperForm">
<table align="center" id="forms">
<tr>
<td>
<label>Taal:</label>
</td>
<td>
<form:select path="language" id="language" name="language">
<form:option value="Nederlands"/>
<form:option value="Français"/>
<form:option value="Deutsch"/>
<form:option value="English"/>
</form:select>
</td>
<td>
<label>Type Aanvraag: </label>
</td>
<td>
<form:select path="applicationType" id="type" name="type">
<form:option value="Nieuwe aanvraag"/>
<form:option value="Voortzetting"/>
<form:option value="Vrijwillige ontbinding"/>
<form:option value="Faillisement"/>
</form:select>
</td>
</tr>
<tr>
<td>
<label for="nameKbo">KBO-Benaming</label>
</td>
<td colspan="3">
<form:input path="nameKbo" id="nameKbo" type="text" name="nameKbo" required="required" size="100%"/>
</td>
<tr>
<td>
<label for="kboNumber">KBO-Nummer:</label>
</td>
<td>
<form:input path="kboNumber" id="kboNumber" type="text" name="kboNumber"/>
</td>
</tr>
<tr>
<td>
<label for="nameOrg">HandelsBenaming:</label>
</td>
<td colspan="3">
<form:input path="nameOrg" id="nameOrg" type="text" name="nameOrg" size="100%"/>
</td>
</tr>
<tr>
<td>
<label>Adres:</label>
</td>
<td colspan="2">
<form:input path="street" id="street" name="street" type="text" placeholder="Straatnaam" size="100%"/>
</td>
<td>
<form:input path="number" id="number" name="number" type="text" size="5"/>
</td>
</tr>
<tr>
<td></td>
<td>
<form:input path="zip" id="zip" name="zip" type="text" placeholder="Zip" size="5"/>
</td>
<td >
<form:input path="city" id="city" name="city" type="text" placeholder="Stad" size="80%"/>
</td>
</tr>
<tr>
<td>
<label>Contactgegevens:</label>
</td>
<td>
<form:input path="telephone" id="telephone" name="telephone" type="text" placeholder="Telefoonnummer"/>
</td>
<td colspan="2">
<form:input path="email" id="email" name="email" type="e-mail" placeholder="E-mailadres" size="100%"/>
</td>
</tr>
<tr>
<td>
<label for="notes" style="vertical-align: middle">Opmerkingen:</label>
</td>
<td colspan="3">
<form:textarea path="notes" id="notes" name="notes" style="width: 100%"/>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td></td>
<td colspan="2" style="align-content: center;"><input type ="submit" value="Opslaan" class="button"/></td>
</tr>
</table>
</form:form>
<%@include file="footer.jsp" %>
</body>
</html>
发票控制器:
@Controller
public class InvoiceController
@Autowired
private GeneralRepo repo;
@GetMapping("invoices")
public String handleGetInvoice(Map<String, Object> model)
Invoices invoiced = new Invoices();
model.put("invoiceForm", invoiced);
return "invoices";
@PostMapping("invoices")
public String handlePost(@ModelAttribute("invoiceForm") Invoices invoiced, Map<String, Object> model)
General general = repo.findLast();
general.setInvoice(invoiced);
repo.save(general);
System.out.println("invoice saved");
return "redirect:/employee";
发票:
<%@ page contentType="text/html;charset=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" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Facturen</title>
<link href="$pageContext.request.contextPath/css/bootstrap.min.css" rel="stylesheet">
<link href="$pageContext.request.contextPath/css/modern-business.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="$pageContext.request.contextPath/css/customUtilities.css" rel="stylesheet">
<link href="$pageContext.request.contextPath/css/general.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
<link rel="icon" href="$pageContext.request.contextPath/images/vigilis.jpg" type="image/x-icon">
</head>
<body>
<!-- Navigation -->
<%@include file="navigation.jsp" %>
<%--@elvariable id="invoiceForm" type=""--%>
<form:form action="invoices" method="post" commandName="invoiceForm">
<table align="center" id="forms">
<tr>
<td>
<label>Ontvangen:</label>
</td>
<td>
<form:select path="received" id="received" name="received">
<form:option value="Ok"/>
<form:option value="Niet Ok"/>
<form:option value="Niet Aanwezig"/>
<form:option value="Aanwezig"/>
</form:select>
</td>
<td>
<label>Status: </label>
</td>
<td>
<form:select path="state" id="state" name="state">
<form:option value="In Behandeling"/>
<form:option value="Goedgekeurd"/>
<form:option value="Geweigerd"/>
<form:option value="Ingetrokken"/>
<form:option value="Moraliteitsonderzoek"/>
</form:select>
</td>
</tr>
<tr>
<td>
<label>Aantal Vorig jaar:</label>
</td>
<td>
<form:input path="last" id="last" type="text" name="last" required="required"/>
</td>
<td>
<label>Aantal Andere jaren:</label>
</td>
<td>
<form:input path="early" id="early" type="text" name="early" required="required"/>
</td>
</tr>
<tr>
<td>
</td>
<td><label>BevestigingsDatum:</label></td>
<td><form:input path="confirmationDate" id="confirmationDate" type="date" name="confirmationDate"
required="required"/></td>
</tr>
<tr>
<td></td>
<td colspan="2" style="align-content: center;"><input type="submit" value="Opslaan" class="button"/>
</td>
</tr>
</table>
</form:form>
<!-- Footer -->
<%@include file="footer.jsp" %>
<!-- Bootstrap core javascript -->
<script src="$pageContext.request.contextPath/js/jquery.min.js"></script>
<script src="$pageContext.request.contextPath/js/popper.min.js"></script>
<script src="$pageContext.request.contextPath/js/bootstrap.min.js"></script>
</body>
</html>
【问题讨论】:
【参考方案1】:将完整的 URL 传递到返回示例中
return "http://localhost:8080/projectname/"+url;
【讨论】:
???那有什么好处呢?显示了第二种形式,它是 postMethod 不起作用,我不明白你的建议将如何解决这个问题以上是关于从前一个控制器重定向时未调用 Post 方法的主要内容,如果未能解决你的问题,请参考以下文章