我有一个带有两个文本框和一个提交按钮的jsp页面。从表单获取输入值时,名为“amount”的第二个文本框的值将变为null。
这是我的jsp页面 -
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.css" />
<title>CREDIT / DEBIT Funds</title>
</head>
<body>
<%
String context_path=request.getContextPath();
String navbar_path=context_path+"/pages/navbar.jsp";
%>
<%@include file="../navbar.jsp" %>
<div class="panel panel-primary">
<div class="panel-heading"> CREDIT / DEBIT Funds</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"></h3>
</div>
<div class="panel-body">
<c:if test="${ accountStatus != null}">
<div class="btn-primary">
<div id="status" class="label-primary">${accountStatus}</div>
</div>
</c:if>
<form class="form-horizontal" role="form" method="POST" id="accountFormBean"
commandName="accountFormBean" action="/ira_bank/ExternalUsers/credit_debit">
<div class="form-group">
<label class="radio-inline col-sm-4 control-label">
<input type="radio" name="CreditDebit" id="CreditDebit" value="Credit"> Credit
</label>
<label class="radio-inline col-sm-4 control-label">
<input type="radio" name="CreditDebit" id="CreditDebit" value="Debit"> Debit
</label>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">From/To</label>
<div class="col-sm-7 col-md-7">
<input type="text" name="accountNumber" class="form-control" id="accountNumber" placeholder="From/To Account Number">
</div>
</div>
<div class="form-group">
<label for="inputamount" class="col-sm-2 control-label">Amount</label>
<div class="col-sm-7 col-md-7">
<input type="text" name="amount" class="form-control" id="amount" placeholder="Amount">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-7 col-md-offset-2 col-md-7">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>
<script src="../../js/jquery-1.10.2.js"></script>
<script src="../../js/bootstrap.js"></script>
</body>
</html>
这就是我从中得到价值的外国人 -
public class AccountFormBean {
private String amount;
private String accountNumber;
private String CreditDebit;
public void setAccountNumber(String accountNumber){
this.accountNumber = accountNumber;
}
public String getAccountNumber() {
return accountNumber;
}
public void setbalance(String amount) {
this.amount = amount;
}
public String getbalance() {
return amount;
}
public void setCreditDebit(String CreditDebit) {
this.CreditDebit = CreditDebit;
}
public String getCreditDebit() {
return CreditDebit;
}
}
帐号的值正常,但金额为空。
答案 0 :(得分:1)
在AccountFormBean
中,amount
字段的getter和setter未正确命名。它们应命名为getAmount()
和setAmount(String amount)
,而不是getbalance()
和setbalance(String amount)
。该框架使用getter和setter名称将输入数据映射到bean字段。
答案 1 :(得分:0)
你正在使用这个:
<input type="text" name="amount" class="form-control" id="amount" placeholder="Amount">
这里输入字段的名称是amount,因此框架希望你在java bean中使用getAmount()orsetAmount(int amount)方法(注意camelcase)。