我在这个游戏中有两个控制器。第一个控制器为welcome.jsp设置模型和视图,它要求输入数据。
第二个控制器接收数据,并路由到服务以将数据带到数据库。我遇到的问题是,当我在控制器#2中有这段代码时
@ModelAttribute("transaction")
public TransactionHelper getTransactionHelper2(){
System.out.println("in helper2");
return new TransactionHelper();
我的所有表单数据都是null(有意义,因为它返回一个新实例。但我不知道如何正确传递表单数据)。 如何将表单数据传递给此方法?请参阅以下代码:
控制器1
package com.atmWebApp.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.atmWebApp.entities.Account;
import com.atmWebApp.entities.TransactionHelper;
import com.atmWebApp.services.AccountService;
import com.atmWebApp.services.TransactionService;
@Controller
public class AccountController {
@Autowired
AccountService accountService;
@ModelAttribute("account")
public Account getAccountObject() {
return new Account();
}
@ModelAttribute("transaction")
public TransactionHelper getTransactionHelper(){
System.out.println("in helper");
return new TransactionHelper();
}
@RequestMapping("/")
public ModelAndView helloWorld1() {
return new ModelAndView("index");
}
@RequestMapping(value = "/login/", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("account") Account account, BindingResult result) {
String accountId = account.getAccountId();
String pin = account.getPin();
if (accountId != null && pin != null){
Account currentAccount = accountService.login(accountId, pin);
System.out.println(currentAccount.getAccountId());
if (currentAccount.getAccountId() != null){
return new ModelAndView("welcome", "balance", accountService.getAccountBalance(currentAccount.getAccountId()))
.addObject("accountId", accountId);
}
}
return new ModelAndView("index", "message", "Wrong Account Number/PIN Combination");
}
}
控制器2
package com.atmWebApp.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.atmWebApp.entities.TransactionHelper;
import com.atmWebApp.services.TransactionService;
@Controller
public class TransactionController {
@Autowired
TransactionService transactionService;
/* @ModelAttribute("transaction")
public TransactionHelper getTransactionHelper2(){
System.out.println("in helper2");
return new TransactionHelper();
}*/
@RequestMapping(value = "/transact/")
public ModelAndView login(@ModelAttribute("transaction") TransactionHelper transactionHelper, BindingResult result) {
String accountId = transactionHelper.getAccountId();
System.out.println(transactionHelper);
System.out.println(transactionHelper.getDollarAmount());
System.out.println(transactionHelper.getAccountId());
String dollarAmount = transactionHelper.getDollarAmount();
String transactionType = transactionHelper.getTransactionType();
if (!transactionService.isValidDollarAmount(dollarAmount)){
System.out.println("invalid amount");
return new ModelAndView("welcome", "message", "Invalid Dollar Amount");
}
transactionService.initiateTransaction(transactionType, dollarAmount, accountId);
return new ModelAndView(new RedirectView("index"), "message", "Transaction Successful");
}
}
观点:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Your account balance is ${balance}.
<form:form commandName="transaction" action="/AtmWebApp/transact/">
<table>
<tr>
<td>Your Account Number:</td>
<td>
<form:select path="accountId">
<form:option value= "${accountId}" />
</form:select></td>
<td>Deposit or Withdrawal?</td>
<td><form:select path="transactionType">
<form:option value="deposit" />
<form:option value="withdrawal" />
</form:select></td>
</tr>
<tr>
<td>How Much?</td>
<td><form:input path="dollarAmount" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:1)
将为同一个@SessionAttributes
类的所有处理程序方法维护@ModelAttribute
和controller
,请注意,此session attributes/model attributes
无法被其他控制器类访问为此,您必须在HttpSession
中添加此属性。
您有3个选项
@SessionAttributes
将请求中的对象存储在会话中。@ModelAttribute
带注释的方法在每个请求之前检索对象HttpSession
作为参数。完成属性后,请从HttpSession
。因此,在您的情况下,选项1和选项2不适合。
请完成此操作,可能会help你。