简体   繁体   中英

Value of text field in jsp form is coming as null

I have a jsp page with two textboxes and one submit button. On getting the input value from the form, the value for the second textbox named as "amount" is coming as null.

This is my jsp page -

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

And this is the formbean from where i am getting the values -

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;
}    

}

The value for account number is coming fine but it is coming as null for amount.

In your AccountFormBean the getter and setter for amount field are not named correctly. They should be named getAmount() and setAmount(String amount) instead of getbalance() and setbalance(String amount) . The framework is usinng getter and setter names to map incoming data to bean fields.

You are using this:

<input type="text" name="amount" class="form-control" id="amount" placeholder="Amount">

Here the name of input field is amount so framework expects you to have getAmount() orsetAmount(int amount) method in your java bean (Note the camelcase as well).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM