2013-05-31 38 views
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> 
+0

請張貼代碼。 – NINCOMPOOP

+0

沒有看到代碼??沒有人是超級英雄在這裏.. –

+0

看到我編輯我的代碼 – user2424732

回答

1

您正在遭受雙提交問題

傳統的方式是使用郵政/重定向/獲取,在Struts2的困難的方法是使用令牌攔截,但如果你不是目標老的瀏覽器,就做這樣的:

<body> 
    .... 

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

這將防止刷新時重新提交。

鏈接上this answer

+0

感謝它的工作..請投票了我的問題..我已經搜索很多... ... – user2424732

相關問題