尝试为用户提供选项,他们可以按几个按钮来增加当前"平衡" "买家"。截至目前,我的按钮什么都不做,我确信我做了一件非常简单的事情是非常愚蠢的。感谢您提前提供任何帮助。
实体:
@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;
@Column(name = "BUYER_BALANCE")
private int balance;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "buyer")
private Set<Toner> toners;
public Buyer(){}
public Buyer(String firstName, String lastName, String buyerAddress, int balance) {
this.firstName = firstName;
this.lastName = lastName;
this.buyerAddress = buyerAddress;
this.balance = balance;
}
public Buyer(String firstName, String lastName, String buyerAddress, int balance, Set<Toner> toners) {
this.firstName = firstName;
this.lastName = lastName;
this.buyerAddress = buyerAddress;
this.balance = balance;
this.toners = toners;
}
public Set<Toner> getToners() {
return toners;
}
public void setToners(Set<Toner> toners) {
this.toners = toners;
}
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;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
控制器:
@Controller
public class BuyerController {
private BuyerService buyerService;
@Autowired
private TonerService tonerService;
@Autowired
public void setBuyerService(BuyerService buyerService){
this.buyerService = buyerService;
}
@RequestMapping("/add-buyer")
public String showBuyerPager(Model model){
//List
List<Buyer> buyers = buyerService.findAllBuyers();
List<Toner> toners = tonerService.findAllToners();
//sending list to view
model.addAttribute("buyers", buyers);
model.addAttribute("toners", toners);
//creating objects in view
model.addAttribute("buyer", new Buyer());
model.addAttribute("toner", new Toner());
return "add-buyer";
}
@GetMapping("/showBuyerForm")
public String addBuyerForm(Model model){
model.addAttribute("buyer", new Buyer());
model.addAttribute("toner", new Toner());
model.addAttribute("buyerId", new Buyer().getBuyerId());
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/{id}")
public String deleteBuyer(@PathVariable Long id){
buyerService.deleteBuyer(id);
return "redirect:/";
}
@PostMapping("/addToBuyerBal/{id}")
public String addToBuyerBalance(Model model, @PathVariable Long id, @ModelAttribute("add") int add){
Buyer mBuyer = buyerService.findOne(id);
mBuyer.setBalance(mBuyer.getBalance() + add);
return "redirect:/";
}
@PostMapping("/purcahseToner/{toner}")
public String buyToner(@PathVariable Toner toner){
Buyer buyer = new Buyer();
return "redirect:/";
}
@PostMapping("/saleToner/{toner}")
public String saleToner(){
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>
</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>
<th>Select Toner</th>
<th>Current Balance</th>
<th>Add to Balance</th>
<th>Delete Buyer</th>
<th>Purchase Item</th>
<th>Sale Item</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>
<select>
<option th:each="toner : ${toners}"
th:text="${toner.tonerName}"
th:value="${toner.id}">
</option>
</select>
</td>
<td th:text="${buyer.balance}"></td>
<td>
<form th:action="@{/addToBuyerBal/{id}(id=${buyer.buyerId})}" th:object="${buyer}" method="post">
<button type="submit" value="25" onclick="return confirm('Are you sure you want to pay $25?')">$25</button>
<button type="submit" value="50" onclick="return confirm('Are you sure you want to pay $50?')">$50</button>
<button type="submit" value="100" onclick="return confirm('Are you sure you want to pay $100?')">$100</button>
</form>
</td>
<td>
<form th:action="@{/deleteBuyer/{id}(id=${buyer.buyerId})}" th:object="${buyer}" method="post">
<input type="hidden" th:field="${buyer}">Delete</input>
<button type="submit" onClick="return confirm('Are you sure you want to delete a Manager?')"/>
</form>
</td>
<td>
<form th:action="@{/purcahseToner/{toner}(toner=${buyer.toners})}" th:object="${buyer}" method="post">
<input type="hidden" th:field="*{toners}">Purchase</input>
<button type="submit" onclick="return confirm('Are you sure you want to make this purchase?')"/>
</form>
</td>
<td>
<form th:action="@{/saleToner/{toner}(toner=${buyer.toners})}" th:object="${buyer}" method="post">
<input type="hidden" th:field="*{toners}">Sale</input>
<button type="submit" onclick="return confirm('Are you sure you want to sale this?')"/>
</form>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>