Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms @ModelAttribute将表单数据传递给新控制器_Forms_Spring Mvc_Model - Fatal编程技术网

Forms @ModelAttribute将表单数据传递给新控制器

Forms @ModelAttribute将表单数据传递给新控制器,forms,spring-mvc,model,Forms,Spring Mvc,Model,我有两个控制器在这一个。第一个控制器为welcome.jsp设置模型和视图,后者要求输入数据 第二个控制器接收数据,并路由到一个服务,将数据传送到数据库。我的问题是,当我在控制器#2中有这段代码时 我所有的表单数据都是空的(这很有意义,因为它返回一个新实例。但是我不知道如何正确地传递表单数据)。 如何将表单数据传递给此方法?请参阅下面的代码: 控制器1 package com.atmWebApp.controllers; import org.springframework.beans.fac

我有两个控制器在这一个。第一个控制器为welcome.jsp设置模型和视图,后者要求输入数据

第二个控制器接收数据,并路由到一个服务,将数据传送到数据库。我的问题是,当我在控制器#2中有这段代码时

我所有的表单数据都是空的(这很有意义,因为它返回一个新实例。但是我不知道如何正确地传递表单数据)。 如何将表单数据传递给此方法?请参阅下面的代码:

控制器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>

在此处插入标题
您的帐户余额为${balance}。
您的帐号:
存款还是取款?
多少钱?

同一个
控制器
类的所有处理程序方法都将维护
@SessionAttributes
@modeldattribute
,请注意此
会话属性/模型属性
不能被其他控制器类访问,为此,必须在
HttpSession
中添加此属性

你有3个选择

  • 使用
    @SessionAttributes
    在请求之间的会话中存储对象
  • 在每次请求之前,使用
    @modeldattribute
    带注释的方法检索对象
  • 在方法中添加
    HttpSession
    作为参数。完成属性设置后,请从
    HttpSession
    中清除该属性
  • 因此,在您的情况下,选项1和选项2不合适

    请把这件事讲一遍,好吗

    <%@ 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>