目次 |
---|
・アクションフォームクラスでのvalidate検証 ・validateの成否 |
package jp.co.confrage.form; 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 ValidatorLoginForm extends ActionForm{ private String id = null; private String password = null; public void setId(String id) { this.id = id; } public String getId() { return id; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public ActionErrors validate(ActionMapping mapping,HttpServletRequest req){ ActionErrors errors = new ActionErrors(); if(this.password == null || this.password.length() < 8){ errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("msg1")); } return errors; } }
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="test_form" type="jp.co.confrage.form.ValidatorLoginForm"> <form-property name="id" type="java.lang.String" initial="id" /> <form-property name="password" type="java.lang.String" initial="pass" /> </form-bean> </form-beans> <action-mappings> <action path="/sample_form" type="jp.co.confrage.action.TestValidatorLoginAction" name="test_form" scope="request" input="/WEB-INF/pages/error.jsp"> <forward name="success" path="/WEB-INF/pages/login.jsp" /> </action> </action-mappings> <message-resources parameter="MessageResources"/> </struts-config>
<%@ page contentType="text/html; charset=Windows-31J" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <html:html> <head> <title>入力エラー</title> <meta http-equiv="Content-Type" content="text/html; charset=Windows-31J"> </head> <body> エラー </body> </html:html>