考虑一下:
如何在页面刷新时保存错误?
<action name="displayLoginPage" class="DisplayLoginAction">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<interceptor-ref name="customStack"/>
<result name="success">Login.jsp</result>
<result name="input">Login.jsp</result>
</action>
<action name="validateloginForm" class="ValidateLoginAction">
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="customStack"/>
<result name="input" type="redirectAction">displayLoginPage</result>
<result name="success">LoginConfirmation.jsp</result>
</action>
答案 0 :(得分:1)
用户刷新页面
- 醇>
错误消失了
如何在页面刷新时保存错误?
这就是MessageStoreInterceptor的工作方式。它实际上是一个功能,而不是一个错误。
页面刷新是用户触发的动作,这意味着可以假设他已经读取了上一次操作的结果(登录尝试),除非他闭着眼睛按下F5。
您应该在第一次阅读后希望邮件过期。
考虑一个包含许多非ajax操作的页面,例如取决于其他的组合框等等...如果错误消息是持久的,它将在每次提交操作之后弹出,而不涉及到另一个页面。
你不想那样。您应该希望在 操作之后收到消息,说明一个操作出错(或正确)。如果用户继续执行其他操作(如刷新),如果这些操作没有出错(或处于特定成功状态),则不应显示任何消息。
之后,还有 从会话中删除持久性消息的问题,因为否则您将在RETRIEVE
或AUTOMATIC
的下一个操作中收到该消息操作模式。然后你可以(但不应该)自定义MessageStore拦截器,或者自己编写/读取/删除会话中的消息,基本上重新发明轮子。或者甚至为RETRIEVE
动作放置两个MessageStore拦截器,一个STORE
和一个displayLoginPage
,得到刚刚提到的陷阱。
您还在使用PRG pattern(发布/重定向/获取),以避免在刷新页面时重新发送数据。同样,你应该避免重复阅读相同的消息两次。
要了解具体如何运作,您可以查看MessageStore Interceptor source code,这很简单:
ValidationAware
且operationMode
为RETRIEVE
或AUTOMATIC
:
actionMessages
,actionErrors
,fieldErrors
; actionMessages
,actionErrors
,fieldErrors
; actionMessages
,actionErrors
,fieldErrors
。/**
* Handle the retrieving of field errors / action messages / field errors, which is
* done before action invocation, and the <code>operationMode</code> is 'RETRIEVE'.
*
* @param invocation
* @throws Exception
*/
protected void before(ActionInvocation invocation) throws Exception {
String reqOperationMode = getRequestOperationMode(invocation);
if (RETRIEVE_MODE.equalsIgnoreCase(reqOperationMode) ||
RETRIEVE_MODE.equalsIgnoreCase(operationMode) ||
AUTOMATIC_MODE.equalsIgnoreCase(operationMode)) {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
// retrieve error / message from session
Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
if (session == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Session is not open, no errors / messages could be retrieve for action ["+action+"]");
}
return;
}
ValidationAware validationAwareAction = (ValidationAware) action;
if (LOG.isDebugEnabled()) {
LOG.debug("retrieve error / message from session to populate into action ["+action+"]");
}
Collection actionErrors = (Collection) session.get(actionErrorsSessionKey);
Collection actionMessages = (Collection) session.get(actionMessagesSessionKey);
Map fieldErrors = (Map) session.get(fieldErrorsSessionKey);
if (actionErrors != null && actionErrors.size() > 0) {
Collection mergedActionErrors = mergeCollection(validationAwareAction.getActionErrors(), actionErrors);
validationAwareAction.setActionErrors(mergedActionErrors);
}
if (actionMessages != null && actionMessages.size() > 0) {
Collection mergedActionMessages = mergeCollection(validationAwareAction.getActionMessages(), actionMessages);
validationAwareAction.setActionMessages(mergedActionMessages);
}
if (fieldErrors != null && fieldErrors.size() > 0) {
Map mergedFieldErrors = mergeMap(validationAwareAction.getFieldErrors(), fieldErrors);
validationAwareAction.setFieldErrors(mergedFieldErrors);
}
session.remove(actionErrorsSessionKey);
session.remove(actionMessagesSessionKey);
session.remove(fieldErrorsSessionKey);
}
}
}
ValidationAware
且operationMode
为STORE
或(operationMode
为AUTOMATIC
且结果类型为redirectAction
), :
actionMessages
,actionErrors
,fieldErrors
来自行动; actionMessages
,actionErrors
,fieldErrors
。/**
* Handle the storing of field errors / action messages / field errors, which is
* done after action invocation, and the <code>operationMode</code> is in 'STORE'.
*
* @param invocation
* @param result
* @throws Exception
*/
protected void after(ActionInvocation invocation, String result) throws Exception {
String reqOperationMode = getRequestOperationMode(invocation);
boolean isRedirect = invocation.getResult() instanceof ServletRedirectResult;
if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
STORE_MODE.equalsIgnoreCase(operationMode) ||
(AUTOMATIC_MODE.equalsIgnoreCase(operationMode) && isRedirect)) {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
// store error / messages into session
Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
if (session == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Could not store action ["+action+"] error/messages into session, because session hasn't been opened yet.");
}
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("store action ["+action+"] error/messages into session ");
}
ValidationAware validationAwareAction = (ValidationAware) action;
session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
}
else if(LOG.isDebugEnabled()) {
LOG.debug("Action ["+action+"] is not ValidationAware, no message / error that are storeable");
}
}
}
<子>
注1 :登录操作也是不言自明的:如果登录后再次登陆登录页面,则只能表示登录失败... BTW上述说明适用于每一页/功能
注意2 :有些网站会在X秒后自动过期消息,而不关心用户是否已阅读过这些消息...... 违反了可用性,恕我直言子>