最近在做一个模拟网上银行小系统时遇到了问题,自己怎么也调不出来,然后各种百度,并在多个论坛上提问,问题终于在传智论坛上被于洋老师给指出来了,真是非常感谢!

下面说一下我遇到的问题:

Struts2中Action代码如下:

@Service("transactionAction")
public class TransactionAction extends ActionSupport {
@Resource
private TransactionBiz transactionBiz;
@Resource
private UserBiz userBiz;
private Account account;
private TransactionLog log;
private Pager pager;
/**
* 存款
* @return
*/
public String deposit(){
//调用自定义方法isEnable判断账户是否冻结
if(isEnable()){
System.out.println(account.getBalance()+"存款");
//使用执行isEnable方法从session中重新获取的账户对象,给交易信息对象log中关联的账户对象属性赋值
log.setAccount(account);
ServletActionContext.getRequest().getSession().setAttribute("account", account);
//调用业务方法,更新账户表Account表中的余额,并在交易信息表transaction_log中添加交易记录
return isSuccess(transactionBiz.deposit(log));
}
return "message";
}
/**
* 取款页面验证
*/
public void validateWithdrawal(){
//比较取款页面输入的金额与账户余额
account = (Account) ServletActionContext.getRequest().getSession().getAttribute("account");
if(log.getTrMoney()>account.getBalance()){
System.out.println("***");
this.addFieldError("trMoney","您的账户余额不足!");
}
}
/**
*  取款
* @return
*/
public String withdrawal(){
if(isEnable()){
log.setAccount(account);
ServletActionContext.getRequest().getSession().setAttribute("account", account);
return isSuccess(transactionBiz.withdrawal(log));
}
return "message";
}
/**
* 转账页面验证
*/
public void validateTransfer(){
account = (Account)ServletActionContext.getRequest().getSession().getAttribute("account");
if(log.getOtherid().intValue()==account.getAccountid().intValue()){
this.addFieldError("log.otherid", "您不能转账给自己!");
}
if(userBiz.getAccount(log.getOtherid())==null){
this.addFieldError("log.otherid", "该账户不存在!");
}
if(log.getTrMoney()>account.getBalance()){
this.addFieldError("log.trMoney", "您的账户余额不足!");
}
}
/**
* 转账
* @return
*/
public String transfer(){
if(isEnable()){
log.setAccount(account);
ServletActionContext.getRequest().getSession().setAttribute("account", account);
return isSuccess(transactionBiz.transfer(log));
}
return "message";
}
/**
* 分页显示交易记录
* @return
*/
public String list(){
System.out.println(pager.getCurPage());
account = (Account)ServletActionContext.getRequest().getSession().getAttribute("account");
//获取待显示页的页码
int curPager = pager.getCurPage();
//根据待显示页的页码和账户对象获取交易记录
List<TransactionLog> logs = transactionBiz.getLogs(account, curPager);
//获得账户的交易记录总数,用来初始化分页类Pager对象,并设置其PerPageRows和rowCount属性
pager = transactionBiz.getPagerOfLogs(account);
//设置Pager对象中的待显示页页码
pager.setCurPage(curPager);
ServletActionContext.getRequest().setAttribute("logs", logs);
return "success";
}
/**
* 自定义方法,判断账户是否冻结
* @return
*/
private boolean isEnable(){
//从session中重新获取Account对象,该对象在登录成功时已保存到session中
account = (Account)ServletActionContext.getRequest().getSession().getAttribute("account");
userBiz.reflush(account);
if(account.getStatus().getName().equals("冻结")){
ActionContext.getContext().put("message", "对不起!该账户已被冻结,无法进行相关操作<br>");
return false;
}
return true;
}
/**
* 自定义方法,根据执行结果,显示操作成功或失败信息
* @param flag
* @return
*/
private String isSuccess(boolean flag){
if(flag){
ActionContext.getContext().put("message", "操作成功!");
return "message";
}
ActionContext.getContext().put("message", "操作失败!<a href='javascript:history.go(-1)'>返回</a>");
return "message";
}
public TransactionLog getLog() {
return log;
}
public void setLog(TransactionLog log) {
this.log = log;
}
public Pager getPager() {
return pager;
}
public void setPager(Pager pager) {
this.pager = pager;
}
}

struts.xml配置如下:

<package name="transaction" namespace="/transaction" extends="struts-default">
<action name="deposit" class="transactionAction" method="deposit">
<result name="message">/message.jsp</result>
<result name="input">/deposit.jsp</result>
</action>
<action name="withdrawal" class="transactionAction" method="withdrawal">
<result name="message">/message.jsp</result>
<result name="input">/withdrawal.jsp</result>
</action>
<action name="transfer" class="transactionAction" method="transfer">
<result name="message">/message.jsp</result>
<result name="input">/transfer.jsp</result>
</action>
<action name="list" class="transactionAction" method="list">
<result name="success">/transactionlog.jsp</result>
<result name="input">/transactionlog.jsp</result>
</action>
</package>

JSP页面代码如下:

 <script language="javascript" >
function disptime(){
var now=new Date();
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second =now.getSeconds();
document.getElementById("datetime").value=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
//year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
setTimeout("disptime()", 1000);
}
function withdrawal(){
var money=document.getElementById("trMoney").value;
if(money.length>0){
if(!(money.search(/^[\+\-]?\d+\.?\d*$/)==0))
{
document.getElementById("errormoney").innerHTML="含有非法字符";
return false;
}else
{
if(parseFloat(money)<=0)
{
document.getElementById("errormoney").innerHTML="金额必须大于0";
return false;
}
return confirm("确认取款吗?");
}
}else {
alert("金额不能为空!");
return false;
}
}
</script>
</head>
<body οnlοad="disptime()">
<form  method="post" name="myform" action="/netbank/transaction/withdrawal" οnsubmit="return withdrawal()">
<div align="center">
<table width="400" border="0" class="table">
<tbody>
<tr>
<td width="100"> 取款时间:</td>
<td><input type="text" name="log.datetime" id="datetime"></td>
</tr>
<tr>
<td> 取款金额:</td>
<td>
<input type="text" name="log.trMoney" id="trMoney" ><!-- value="${log.trMoney} -->
<span id="errormoney" style="color:red;"></span>
</td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" value="取款" />
<span style="color:red;">
<s:fielderror/>
</span> </td>
</tr>
</tbody>
</table>  

出现的问题是:在部署好以后,正确的存款、取款都能完成,但是如果取款额超过当前余额时,取款验证没通过,页面显示错误信息,但当再次重新输入小于余额的数再提交时,还是不能完成正常跳转,依旧显示验证未通过的信息“您的账户余额不足!”,再执行存款时,无论输入大于余额还是小于余额的值都显示“您的账户余额不足!”,就好像是如果取款时的操作金额超过余额,之后无论再重新执行什么其它操作都会走取款的验证。

问题是ssh整合常见问题 ,因为struts2默认Action是多例的,也就是每次提交都会新建一个Action实例,那么第一次转账失败,不会影响第二次转账, 但是struts2 整合 Spring后, Struts2 如果配置到Spring 就变成 单例的, 那么第一次转账失败, 信息就保存到Action 中, 第二次转账 即使数据正确,也会出现 第一次错误, 因为Action 单例,没有去除之前的错误信息
解决方案: 在spring 中配置Struts2 的Action  scope="prototype"

而我之前用的是Spring的注解方式,所以action是单例的

Spring和Struts2整合见问题之一相关推荐

  1. spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action

    在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...

  2. Spring+Hibernate+Struts2整合所需要的Jar包

    struts2.1.6 支持jar包 xwork-2.1.2.jar struts2-core-2.1.6.jar commons-logging-1.0.4.jar freemarker-2.3.1 ...

  3. Spring和Struts2整合

    Spring整合Struts2 步骤:1.导入Struts2jar相关包,并且导入Struts2-Spring-plugin-2.0.11.2.jar 2.配置xml文件:配置Struts2过滤器和S ...

  4. Spring与Struts2整合的两种解决方案

    http://www.itzhai.com/spring-and-struts2-integration-of-the-two-solutions.html Struts2与Spring整合的方案一: ...

  5. spring+hibernate+Struts2 整合(全注解及注意事项)

    最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...

  6. 孙宇java_[JAVA] 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程...

    资源介绍 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程 ===============课程目录===== ...

  7. struts2整合spring应用实例

    我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明stru ...

  8. Struts2+Spring+Hibernate的整合

    整体程序结构 1.maven依赖 <!--实现Struts2+Spring+Hibernate的整合 --><dependencies><!--Spring --> ...

  9. struts2+spring+ibatis框架整合

    实现效果 Ajax 涉及框架 Struts2 ibatis Spring JQuery 准备 Jar 包 commons-collections.jar commons-dbcp.jar common ...

最新文章

  1. 影像组学视频学习笔记(43)-标准差、标准误及95%置信区间CI、Li‘s have a solution and plan.
  2. Parcelable最强解析
  3. 北森iTalentX 3.0:聚焦场景一体,开启HR全面数字化时代
  4. Oracle脚本(二)
  5. DNA和纳米(Nano)Fusion技术的发展趋势
  6. 错误 -source 1.6 中不支持 diamond 运算符的解决办法(已解决)
  7. vue-element日期框点击不显示,不刷新
  8. 【#】Spring3 MVC 注解(二)---@RequestMapping
  9. CRC_8循环冗余校验码verilog实现
  10. 数据库复习-3.常用的概念模型
  11. 什么是数据安全,为什么需要保证数据安全
  12. 2022年4月语音合成(TTS)和语音识别(ASR)论文月报
  13. 金山词霸2016.1.3.3 手动去广告方法
  14. 淘宝网的技术发展史(三)――分布式时代
  15. 这款pubMed文献爬虫下载器,妥妥的科研利器
  16. 使用htmlunit采集网页+点击网页按钮
  17. ALTER ROUTINE 语句
  18. OA系统:企业驰骋信息化道路的神器
  19. Windows server 2003设置使用必备技巧集
  20. Power BI 链接 SharePoint Folder 数据源

热门文章

  1. postman需要激活吗_postman接口测试实战例子
  2. webmagic小试牛刀
  3. 跳槽也不一定涨薪,2023,别再裸辞了····
  4. moviepy视频剪辑总结学习笔记
  5. 《初级会计实务》考试学习分享之第二章 ——资产【考试大纲】
  6. POI读取excel。读取MultipartFile格式的excel文件
  7. 2020年度总结——心系彼岸,一苇以航
  8. ffmpeg4.3 安装 in Ubuntu18.04
  9. 300+篇CVPR 2020代码开源的论文,全在这里了!
  10. 【Xilinx】如何用sdk编译vcu-qt