Oki 在 Dave Newton 的建议之后,我挖掘了一点以完成正确的方法:)
到目前为止,这是我的回答,不再刷新、向后或向前重新提交表单并保留 ValidationAware 消息(错误和消息)
我的 struts.xml :
<action name="register" class="registerAction">
<interceptor-ref name="defaultWithoutAuthenticationStack"/>
<result type="tiles" name="input">
<param name="titleKey">global.title.register</param>
<param name="location">register</param>
</result>
<result name="success" type="redirectAction">index</result>
</action>
<action name="goRegister" class="registerAction">
<interceptor-ref name="defaultWithoutAuthenticationStack"/>
<result name="input" type="redirectAction">register</result>
<result name="success" type="redirectAction">index</result>
</action>
还有我的jsp:
<s:form method="post" action="goRegister" validate="true">
<s:textfield name="usernameRegister" id="usernameRegister" key="global.username" labelposition="left" />
<s:password name="passwordRegister" id="passwordRegister" key="global.password" labelposition="left" />
<s:password name="passwordConfirmRegister" id="passwordConfirmRegister" key="global.password.confirm" labelposition="left" />
<s:textfield name="emailRegister" id="emailRegister" key="global.email" labelposition="left" />
<s:submit key="global.register" name="submitRegister" method="goRegister"></s:submit>
</s:form>
我找到了消息保存here 的答案,当我们处于重定向情况时,他将不同的操作消息(消息、错误、字段错误)存储在会话中,否则将它们推送到操作消息
这是我的实现(你会发现我在默认消息和错误消息中添加了成功消息):
public class MessagesInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = -6230422534075664728L;
private Map<String, Object> session;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
session = invocation.getInvocationContext().getSession();
MyAction action = (MyAction) invocation.getAction();
this.addSessionMessagesInActionMessages(action);
String output = invocation.invoke();
Result result = invocation.getResult();
// If it's a redirection, store the messages in session
if(result instanceof ServletRedirectResult || result instanceof ServletActionRedirectResult)
this.addActionMessagesInSessionMessages(action);
return output;
}
@SuppressWarnings("unchecked")
private void addSessionMessagesInActionMessages(MyAction action) {
Object messagesObject = getSession().remove(SESSION_ACTION_MESSAGES);
if (messagesObject != null) {
List<String> sessionMessages = (List<String>)messagesObject;
for (String sessionMessage : sessionMessages) {
action.addActionMessage(sessionMessage);
}
}
Object errorsObject = getSession().remove(SESSION_ACTION_ERRORS);
if (errorsObject != null) {
List<String> sessionErrors = (List<String>)errorsObject;
for (String sessionError : sessionErrors) {
action.addActionError(sessionError);
}
}
Object successObject = getSession().remove(SESSION_ACTION_SUCCESS);
if (successObject != null) {
List<String> sessionSuccessList = (List<String>)successObject;
for (String sessionSuccess : sessionSuccessList) {
action.addActionSuccess(sessionSuccess);
}
}
@SuppressWarnings("rawtypes")
Map<String, List<String>> fieldErrors = (Map) session.remove(SESSION_FIELD_ERRORS);
if (fieldErrors != null && fieldErrors.size() > 0){
for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()){
for (String message : fieldError.getValue()){
action.addFieldError(fieldError.getKey(), message);
}
}
}
}
protected void addActionMessagesInSessionMessages(MyAction action) throws Exception{
Collection<String> actionErrors = action.getActionErrors();
if (actionErrors != null && actionErrors.size() > 0){
session.put(SESSION_ACTION_ERRORS, actionErrors);
}
Collection<String> actionMessages = action.getActionMessages();
if (actionMessages != null && actionMessages.size() > 0){
session.put(SESSION_ACTION_MESSAGES, actionMessages);
}
Collection<String> actionSuccess = action.getActionSuccess();
if (actionSuccess != null && actionSuccess.size() > 0){
session.put(SESSION_ACTION_SUCCESS, actionSuccess);
}
Map<String, List<String>> fieldErrors = action.getFieldErrors();
if (fieldErrors != null && fieldErrors.size() > 0){
session.put(SESSION_FIELD_ERRORS, fieldErrors);
}
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
MyAction 继承 ActionSupport
希望这会对某人有所帮助;)
现在可以保留操作消息和字段验证消息,没有刷新问题
但我的字段值现在是空白的,如何检索/存储它们以重新填充我的字段?