我得到了一个简单的表单验证和提交,效果很好
这是我的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>
我的提交功能:
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
编辑:我将动作消息和字段消息保存添加到我的问题中 如何使用重定向保留操作消息和字段消息(验证)?
答案 0 :(得分:2)
成功后,重定向到“您已注册”页面。这样,如果用户刷新,他们只会再次获得相同的页面。
答案 1 :(得分:0)
到目前为止,这是我的答案,不再刷新,向后或向前重新提交表单并保留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
希望这会对某人有所帮助;)
现在保留动作消息和字段验证消息,没有刷新问题
但是我的字段值现在是空白的,如何检索/存储它们以重新填充我的字段?