根据ActionBean

时间:2016-03-09 11:05:45

标签: jsp jstl javabeans

我对这个JSTL和JSP编程很陌生,并且无法弄清楚应如何解决这个问题。这就是为什么我希望任何一个人能够指引我并帮助我朝着正确的方向前进。

我在页面上有一个按钮。如果用户在最后一次点击后的5分钟内再次点击该按钮,那么我想通过确认对话框提示用户,询问他们是否确定此操作。

JSP:

<c:choose>
<c:when test="${actionBean.warningDialog}">
    <!-- Show warning dialog -->
    <stripes:submit name="send" value="Send" style="height:20%;"
        onclick="return confirm('Last click is lesser than 5 minutes ago.\nAre you sure you want to click again?','Are you sure you want to click?')" />
</c:when>
<c:otherwise>
    <!-- Don't show any warnings -->
    <stripes:submit name="send" value="Send" style="height:20%;" />
</c:otherwise>

的ActionBean:

public boolean isWarningDialog(){
ClickBean bean = getClickSettings();
return isClickExpired(bean.getLastClick(), new Date());
}

private boolean isBroadcastExpired(Date lastSentDate, Date currentDate) {
    this.differenceInMilliseconds = currentDate.getTime()-     lastSentDate.getTime();
    return differenceInMilliseconds < minimumBroadcastWarningTime;
}

@HandlesEvent("broadcast")
public Resolution send(){
    //...Send message and return ForwardResolution...
}

所以我当前的问题是isWarningDialog是在加载页面时计算的,而不是单击时计算的。我们的想法是,当用户点击按钮时,页面应该计算是否应该提示警告。

当用户点击发送时,它将以同一页面的ForwardResolution结束,这意味着将出现以下情况:

  1. 用户点击按钮,没有显示对话框,用户将被转发到同一页面。
  2. 现在,转发后,会计算isWarningDialog,并且LastClick将少于5分钟。
  3. 用户等待6分钟,直到他第二次点击按钮。
  4. 用户不应该收到任何警告对话框,但目前他会,因为时间是在上次点击后立即计算的&lt; - 这就是问题。

1 个答案:

答案 0 :(得分:1)

解决方案非常简单

  1. 用户第一次点击按钮时,将时间存储在会话变量中(可以使用jsp:setProperty在您的支持bean中提供请求对象)

    long timestamp = System.currentTimeMillis();        
    request.getSession.setAttribute("firstClickTime", timestamp);
    
  2. 第二次检查firstClickTime是否为空(表示按钮已被点击),并确保时差小于5分钟

    long firstTimeClick = request.getSession().getAttribute("firstTimeClick");
    if( firstTimeClick!=0 ){ //if not 0, it's the second click
    
    //make sure that the user clicked within 5 minutes
    long currentTime = System.currentTimeMillis();
    if(  currentTime - firstTimeClick <= (5 * 60 * 1000) ) {//
     //initialize your warning dialog prompt 
    }
    //reset your session variable
    request.getSession().setAttribute("firstTimeClick", 0);
    
    } 
    
  3. 更新:或者,使您的支持bean会话作用域(@SessionScoped)并为时间戳字段添加其他字段和相应的getter / setter方法。这样,您根本不需要请求对象。