struts的validator框架

阅读更多

依赖包

-- jakarta-oro.jar:提供了一组处理文本的类,具有文本替换、过滤和分割等功能

-- commons-validator.jar:提供了一个简单的、可扩张的验证框架,包含了通用的验证方法和验证规则

基于XML配置,在WEB-INF目录下

-- validator-rules.xml:包含了一组通用的验证规则,对所有的Struts应用都适用

-- validation.xmlvalidator框架的第二个配置文件。这个是针对于某个具体的Struts应用,需要开发人员来创建,它可以为应用中的ActionForm配置所需要的验证规则,取代ActionForm类中以编程方式来实现验证逻辑

 

struts-config.xml中配置

  


	
		
	
	
		
			
		
		
		
			
		
	

	
	

	
	
		
	

 

LoginAction.java

 

public class LoginAction extends DispatchAction {
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("view");
	}
	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//TODO print
		System.out.println("into login");
		LoginForm loginForm = (LoginForm)form;
		System.out.println(loginForm.getUserName());
		System.out.println(loginForm.getPassword());
		return mapping.findForward("view");
	}
}

 

 

LoginForm.java

public class LoginForm extends ActionForm {
	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

web.xml


  Login
  
    action
    
			org.apache.struts.action.ActionServlet
		
    
      config
      /WEB-INF/struts-config/struts-config.xml
    
    1
  
  
    action
    *.do
  

 

validator-login.xml

	
		
minlength 6

 

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>






Insert title here


	
		

 

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>




main.jsp


	
userName:
password:

 

 

  • Login.zip (25.8 KB)
  • 下载次数: 1

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