struts的表单验证

阅读更多

ActionForm中重写父类的方法validate(),该方法会在参数传递过来后对属性的进行验证,验证不同过的时候,会根据struts-config.xml文件中的Action配置返回到input属性所指定的界面,可以使用””>request范围中取出对应的错误信息提示用户

 

ActionForm中验证

 

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class HelloForm extends ActionForm {
	private static final long serialVersionUID = 1L;
	
	private String userName = null;

    public String getUserName() {
        return (this.userName);
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
   
    /**
     * 把属性重新设置为默认值
     * Reset all properties to their default values.
     */
    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.userName = null;
    }

    /**
     * 用于表单验证
     */
    @Override
    public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
//    	// 创建一个ActionMessage来存放错误信息
//    	ActionMessages actionMessages=new ActionMessages();
//    	/*
//    	 * 向ActionMessage存放信息
//    	 * property:为ActionMessage对象的key
//    	 * ActionMessage中key为资源文件中的key,Object为向资源文件传递的第一个参数{0}
//    	 */
//    	actionMessages.add("property",new ActionMessage("key",new Object()));
        ActionErrors errors = new ActionErrors();
        if ((userName == null) || (userName.length() < 1)){
            errors.add("username", new ActionMessage("hello.no.username.error"));
        }
        return errors;
    }
}

 

 

Action中验证

 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;

import com.cong.helloapp.Constants;
import com.cong.helloapp.bean.HelloForm;
import com.cong.helloapp.bean.PersonBean;

public class HelloAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    throws Exception {
    	// 在MessageRecourse对象中封装了对本地化的资源文件,过去要获取properties文件中的key,可以使用getMassage来获取
        MessageResources messages = getResources(request);
        messages.getMessage("hello.jsp.title");

        /*
         * 创建一个ActionMessages对象,如果userName对象为空的话,则向其中添加
         * 键值对的错误信息,并转发到响应的页面
         */
        ActionMessages errors = new ActionMessages();
        String userName = (String)((HelloForm) form).getUserName();
        String badUserName = "Monster";
		if (userName.equalsIgnoreCase(badUserName)) {
			/*
			 * new ActionMessage(String key, Object value0)
			 * 	key:表示资源文件中的key值
			 * 	value0:匹配资源文件中的第一个参数,eg:{0}
			 */
			errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName));
			/*
			 * 将错误信息添加到request中
			 */
			saveErrors(request, errors);
			/*
			 * 返回Action中的input对应的jsp页面
			 */
			return (new ActionForward(mapping.getInput()));
		}

        PersonBean pb = new PersonBean();
        pb.setUserName(userName);
        pb.saveToPersistentStore();
        // 将PersonBean对象存放到request作用于中,通过request传递到jsp页面中
        request.setAttribute( Constants.PERSON_KEY, pb);

        // 由于在上面的代码中,将PersonBean的数据添加到了request中,所以不需要将PersonBean转发到jsp页面,所以可取消mapping的传递
        request.removeAttribute(mapping.getAttribute());
        // Forward control to the specified success URI
        return (mapping.findForward("SayHello"));
    }
}

 

struts-config.xml文件配置

 







	
	
		
	
	
	
	
		
		
		
			
		
	
	
	
	
	

 

web.xml文件

 






	HelloApp Struts Application

	
	
		action
		org.apache.struts.action.ActionServlet
		
			config
			/WEB-INF/struts-config.xml
		
		2
	

	
	
		action
		*.do
	
	
		hello.jsp
	

	
	
		/WEB-INF/struts-bean.tld
		/WEB-INF/struts-bean.tld
	

	
		/WEB-INF/struts-html.tld
		/WEB-INF/struts-html.tld
	

	
		/WEB-INF/struts-logic.tld
		/WEB-INF/struts-logic.tld
	

 

hello.jsp文件

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>


	
		<bean:message key="hello.jsp.title" />
		
	
	
		

1111
---



  

 

 

  • StrutsHello.zip (24.6 KB)
  • 下载次数: 2

你可能感兴趣的:(strust,validator)