根据 ActionBean 的 return 值显示确认对话框
Show confirm dialog based on a return value of the ActionBean
我是这个 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>
动作豆:
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结束到同一个页面,也就是说会出现下面的场景:
- 用户单击按钮,没有显示对话框,用户将被转发到同一页面。
- 现在转发后计算isWarningDialog,LastClick不到5分钟
- 用户等了 6 分钟,直到他第二次点击按钮。
- 用户不应收到任何警告对话框,但目前他会收到,因为时间是在上次单击后立即计算的 <-- 这就是问题所在。
解决方法很简单
用户第一次点击按钮时,将时间存储在会话变量中(请求对象可以在您的支持 bean 中使用 jsp:setProperty )
long timestamp = System.currentTimeMillis();
request.getSession.setAttribute("firstClickTime", timestamp);
第二次只需检查firstClickTime不为null(表示按钮已经被点击)并确保时间差小于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);
}
更新:或者,使您的支持 bean 会话范围 (@SessionScoped) 并为时间戳字段添加一个附加字段和相应的 getter/setter 方法。这样你根本不需要请求对象。
我是这个 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>
动作豆:
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结束到同一个页面,也就是说会出现下面的场景:
- 用户单击按钮,没有显示对话框,用户将被转发到同一页面。
- 现在转发后计算isWarningDialog,LastClick不到5分钟
- 用户等了 6 分钟,直到他第二次点击按钮。
- 用户不应收到任何警告对话框,但目前他会收到,因为时间是在上次单击后立即计算的 <-- 这就是问题所在。
解决方法很简单
用户第一次点击按钮时,将时间存储在会话变量中(请求对象可以在您的支持 bean 中使用 jsp:setProperty )
long timestamp = System.currentTimeMillis(); request.getSession.setAttribute("firstClickTime", timestamp);
第二次只需检查firstClickTime不为null(表示按钮已经被点击)并确保时间差小于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); }
更新:或者,使您的支持 bean 会话范围 (@SessionScoped) 并为时间戳字段添加一个附加字段和相应的 getter/setter 方法。这样你根本不需要请求对象。