在struts2中防止表单在刷新时重新提交(带有动作消息和字段消息保存)



我得到了一个简单的表单验证和提交,工作得很好

这是我的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 type="tiles" name="success">register.success</result>
    </action>

我的jsp表单:

<s:form method="post" action="register.action" 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>

和我的submit函数:

public String goRegister(){
    user.setUsername(getUsernameRegister());
    user.setPassword(getPasswordRegister());
    user.setEmail(getEmailRegister());
    userService.addUser(user);
    ArrayList<String> successMessageUsername = new ArrayList<String>();
    successMessageUsername.add(getUsernameRegister());
    this.addActionSuccess(getText("global.register.success", successMessageUsername));
    return SUCCESS;
}

没什么特别的!

问题是,当我在成功页面或表单页面(验证失败时)刷新页面时,我有一个窗口要求重新提交表单信息

我该如何预防呢?

Thx

编辑:我在我的问题中添加了动作消息和字段消息保存如何使用重定向保存动作消息和字段消息(验证)?

使用post-redirect-get模式

成功后,重定向到"您已注册"页面。这样,如果用户刷新,他们就会再次得到相同的页面。

在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>

我在这里找到了消息保存的答案,当我们处于重定向情况时,在会话中存储不同的操作消息(消息,错误,字段错误)并将它们推送到操作消息,否则

这是我的实现(你会发现我在默认消息和错误消息中添加了成功消息):

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

希望对大家有所帮助;)

操作消息和字段验证消息现在被保留,没有刷新问题

但是我的字段值现在是空白的,我如何检索/存储它们来重新填充我的字段?

相关内容

  • 没有找到相关文章

最新更新