用户刷新页面
错误消失了
我应该如何保存页面刷新时的错误?
这就是MessageStoreInterceptor的工作方式。它实际上是一个功能,而不是一个错误。
页面刷新是用户触发的动作,这意味着可以假设他已经阅读了上一次操作的结果(登录尝试),除非他是闭着眼睛按 F5。
您应该希望消息在第一次阅读后过期。
考虑一个有很多非 ajax 操作的页面,比如依赖于其他操作的组合框等。如果错误消息是持久的,它会在每次提交操作后弹出,而不涉及到另一个页面。
你不想要那个。您应该希望在该操作之后收到一条消息,指出一项操作出错(或正确)。如果用户随后继续进行其他操作,例如刷新,并且这些操作没有出错(或处于特定的成功状态),则不应显示任何消息。
之后,还有从会话中删除持久消息时的问题,因为否则您将在下一个操作中使用RETRIEVE
或AUTOMATIC
操作模式获取该消息。然后,您可以(但不应该)自定义 MessageStore 拦截器,或者自己在会话中写入/读取/删除消息,基本上是在重新发明轮子。甚至放置两个 MessageStore 拦截器,一个RETRIEVE
和一个STORE
用于displayLoginPage
Action,得到刚才提到的陷阱。
您还使用了PRG 模式(Post/Redirect/Get),以避免在刷新页面时重新发送数据。同样,您应该避免重复阅读相同的消息两次。
要了解这具体是如何工作的,您可以查看MessageStore Interceptor 源代码,这非常简单:
- 在调用之前,如果 Action 是
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);
}
}
}
- 调用后,如果 Action is
ValidationAware
and operationMode
is STORE
or ( operationMode
is AUTOMATIC
and Result is of type 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:登录操作也是不言自明的:如果登录后再次登陆登录页面,那只能意味着登录失败......顺便说一句,上面的解释适用于每个页面/功能
注2:有网站产生的消息会在 X 秒后自动过期,而不关心用户是否阅读过它们……这违反了可用性,恕我直言。