0

为了查看目的,我正在使用 struts2 和 jsp,这里我面临一个问题。每当我刷新数据库中的值时,都会发生更改,因此当我尝试将值检索到 jsp 时,它会为我提供那些更改的值,这不是我真正想要的值,当我单击时它应该被更改而不是当我刷新。知道如何解决吗?任何帮助将不胜感激。

package com.rajesh.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class DepositAction extends ActionSupport {

private int accountno;
private String username;
private String password;
private String amount;
private int dataamount = 0;

public int getDataamount() {

return dataamount;
}

public void setDataamount(int dataamount) {
this.dataamount = dataamount;
}

public int getAccountno() {
return accountno;
}

public void setAccountno(int accountno) {
this.accountno = accountno;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String execute() throws Exception {

Connection con = GetCon.getCon();

PreparedStatement ps = con.prepareStatement("Select * from account where accountno=? and username = ? and password =?");
ps.setInt(1, accountno);
ps.setString(2, username);
ps.setString(3, password);

ResultSet rs = ps.executeQuery();

boolean status = rs.next();

System.out.println(status);

if (status == true) {

PreparedStatement ps1 = con.prepareStatement("Select * from account where accountno=?");

ps1.setInt(1, accountno);

ResultSet rs1 = ps1.executeQuery();

if (rs1.next()) {
dataamount = Integer.parseInt(amount) + rs.getInt(5);
System.out.println(rs1.getInt(5));
System.out.println(dataamount);

}

PreparedStatement ps2 = con.prepareStatement("update account set amount=? where accountno='"
                        + accountno + "'");
ps2.setInt(1, dataamount);
int state = ps2.executeUpdate();

if (state > 0)
System.out.println("AMOUNT DEPOSITED");

return SUCCESS;

} else {
return "error";
}
}
}

和我的 deposit.jsp

<div id="header12">
<table>
<tr><td><h4>amount deposited is:<s:property value="amount"/> Rs</h4></td></tr>
<tr>
<td align="center" valign="middle"><h4>your current Balance is:<s:property value="dataamount" />   Rs</h4></td>
</tr>
</table>

和我的 deposit1.jsp

<form name=F1 onSubmit="return dil(this)" action="depositAction" method="post">
<table cellspacing="5" cellpadding="3">
<tr><td>ACCOUNT NO:</td><td> <input type="text" name="accountno"/></td></tr>
<tr><td>USER NAME:</td><td> <input type="text" name="username"/></td></tr>
<tr><td>PASSWORD:</td><td> <input type="password" name="password"/></td></tr>
<tr><td>AMOUNT:</td><td> <input type="text" name="amount"/></td></tr>
4

1 回答 1

1

您正在遭受双重提交问题

传统方法是使用Post/Redirect/Get,Struts2 上更难的方法是使用Token Interceptor,但如果你不是针对旧浏览器,就这样做:

<body>
    ....

    <script>
        window.history.pushState("","", location.href);
    </script>
</body>

这将防止在刷新时重新提交。

这个答案的链接。

于 2013-05-31T14:32:25.993 回答