利用struts1防止表单重复提交

1、首先在请求表单之前:this.saveToken(request);

2、其次表单使用:<html:form>标签

3、再者处理表单处用isTokenValid(request)检查token,然后合法后resetToken(request);

 

 

	<html:form action="/Login.do?m=doLogin"  method = "POST">
		<html:text property="username"></html:text>
		<html:submit>Submit</html:submit>
	</html:form>

 

package com.tim.beans;

import org.apache.struts.action.ActionForm;

public class UserInfo extends ActionForm{

	private static final long serialVersionUID = 6705948863335091123L;
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
}

 

	public ActionForward Login(ActionMapping mapping, ActionForm form,  
            HttpServletRequest request, HttpServletResponse response){
		
		this.saveToken(request);
		return mapping.findForward("login");
	}

 

 

	public ActionForward doLogin(ActionMapping mapping, ActionForm form,  
            HttpServletRequest request, HttpServletResponse response){
		ActionMessages errors = new ActionMessages();
		
		if(!isTokenValid(request)){
			errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("表单重复提交!",false));
		}
		
		if(!errors.isEmpty()){
			saveErrors(request,errors);
			return mapping.findForward("info");
		}
		
		resetToken(request);
		System.out.println("进来了!");
		return mapping.findForward("info");
	}

 

	<form-beans>
		<form-bean name="userInfo" type="com.tim.beans.UserInfo">
			<form-property name="username" type="java.lang.String"></form-property>		
		</form-bean>
	</form-beans>
 

 

<load-on-startup>0</load-on-startup>
 

 

你可能感兴趣的:(struts1)