我有一个带更新链接的问题,用于更新我的交易。在一个jsp上,我有一个包含事务的列表,在按下“更新”后,我想通过它传递transactionId,对其进行更新,保存并返回列表。我使用了c:param隐藏的功能,但它不起作用。了解为什么它不通过id和自动填充表单。当我点击测试交易时,似乎在会话中传递了值:
http://localhost:8080/transaction/addTransaction?transactionId=64&userId=1
这里是事务控制器:
@Controller
@RequestMapping("/transaction")
public class TransactionController {
@Autowired
TransactionService transactionService;
@Autowired
CategoryService categoryService;
@Autowired
UserService userService;
@GetMapping("/addTransaction")
public String transactionsList(Model theModel){
Transaction transaction = new Transaction();
theModel.addAttribute("user",userService.getAllUsers());
theModel.addAttribute("category", categoryService.getAllCategories());
theModel.addAttribute("newTransaction", transaction);
return "addTransaction";
}
@PostMapping("/saveTransaction")
public String saveTransaction(@ModelAttribute("newTransaction") Transaction
newTransaction,
BindingResult theBindingResult,
HttpServletRequest request) throws
ParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = request.getParameter("transactionDate");
LocalDate localDate = LocalDate.parse(date, formatter);
Date formatedDate =
Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.ofHours(-3)));
newTransaction.setTransactionDate(formatedDate);
transactionService.saveTransaction(newTransaction);
return "redirect:/user/userPage";
}
@GetMapping("/deleteTransaction")
public String deleteUser(@RequestParam("transactionId") int idFromTransactionToDelete,
@RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes){
transactionService.deleteTransactionById(idFromTransactionToDelete);
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
return "redirect:/user/userPage";
}
@GetMapping("/updateTransaction")
public String updateUser(@RequestParam("transactionId") int
idFromTransactionToUpdate,
@RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes,
Model theModel){
Transaction transactionToUpdate = transactionService.getSingleTransactionById(idFromTransactionToUpdate);
transactionToUpdate.setUser(userService.getUserById(loggedUserId));
theModel.addAttribute("newTransaction",transactionToUpdate);
theModel.addAttribute("category", categoryService.getAllCategories());
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
redirectAttributes.addFlashAttribute("newTransaction",
transactionToUpdate);
return "addTransaction";
}
}
交易列表页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<html>
<head>
<title>User page</title>
</head>
<body>
<c:url var="addTransactionLink" value="/transaction/addTransaction"/>
<c:url var="addFixedTransactionLink"
value="/fixedTransaction/addFixedTransaction"/>
<h1>Welcome ${loggedUser.login} !</h1>
<br>
<h2>Here are your latest transactions:</h2>
<br>
<a href=${addTransactionLink}>
<input type="button" value="Add Transaction"/>
</a>
<a href=${addFixedTransactionLink}>
<input type="button" value="Add Fixed Transaction"/>
</a>
<table>
<tr>
<th>Category</th>
<th>Price</th>
<th>Description</th>
<th>Date</th>
</tr>
<c:forEach var="transaction" items="${userTransactions}">
<c:url var="deleteTransactionLink"
value="/transaction/deleteTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<tr>
<td>${transaction.category.categoryName}</td>
<td>${transaction.moneyAmount}</td>
<td>${transaction.description}</td>
<td>${transaction.transactionDate}</td>
<td><a href="${deleteTransactionLink}">Delete</a> </td>
<td><a href="${updateTransactionLink}">Update</a> </td>
<td>${transaction.transactionId}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
并添加交易公式:
<%@ taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add Transaction Form</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/resources/css/style.css"/>
</head>
<body>
<h1>Add transaction to database:</h1>
<form:form action="saveTransaction" modelAttribute="newTransaction"
method="post">
<form:hidden path="transactionId"/>
${newTransaction.toString()}
${newTransaction.transactionId}
<table>
<tr>
<td><label>Choose category:</label></td>
<td><form:select name="category.id" path="category.id">
<c:forEach items="${category}" var="category">
<form:option value="${category.id}">${category.categoryName}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Choose user:</label></td>
<td><form:select name="user.id" path="user.id">
<c:forEach items="${user}" var="user">
<form:option value="${user.id}">${user.login}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Amount:</label></td>
<td><form:input path="moneyAmount" />
</td>
</tr>
<tr>
<td><label>Transaction date:</label></td>
<td><form:input type = "date" path="transactionDate"/>
</td>
</tr>
<tr>
<td><label>Description:</label></td>
<td><form:input path="description" />
</td>
</tr>
<tr>
<label></label>
<td><input type="submit" value="Submit" class="save"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:0)
问题似乎在您的代码中。您已在jsp中定义:
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
在使用Java代码时:
@GetMapping("/updateTransaction")
public String updateUser(@RequestParam("transactionId") int
在JSP中将其更改为:
<c:url var="updateTransactionLink"
value="/transaction/updateTransaction">