Struts1.3 validationチェック

7351 ワード

1.webプロジェクトにStrust 1を追加する.3
ステップEclipse手動でstruts 1を追加する.3
2.テストページロゴを作成する.jspコードは以下の通りです.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page	language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<title>logon</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<meta name="GENERATOR"	content="Rational® Application Developer™ for WebSphere® Software">
</head>
<body>
	
	<html:errors/>
	<form action="logon.do" method="post">
		<table>
			<TR>
				<td>UserName</td>
				<td><input name="username" type="text" /></td>
			</TR>
			
			<TR>
				<td>PassWord</td>
				<td><input name="password" type="password" ></td>
			</TR>
			
			<TR>
				<td><input type="submit" value="LOGON" /></td>
			
				<td><input type="reset" value="RESET" /></td>
			</TR>
		
		</table>
	</form>
</body>
</html>

 
 
 
 
 
3.strutsプロファイルstruts-configを修正する.xmlは以下の通り
  
 
<?xml version="1.0" encoding="UTF-8"?>

<!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="logonForm" type="logon.LogonForm" />
        </form-beans>

        <action-mappings>
        		<!--  action  input  ,  struts validator  -->
                <action path="/logon" input="/logon.jsp" type="logon.LogonAction"
                        name="logonForm" validate="true" scope="request"> 
                        <forward name="LogonSuccess" path="/logonSuccess.html" />
                        <forward name="LogonFailure" path="/logonFailure.html" />
                </action>
        </action-mappings>
        
        
        
     <!--   -->
	<message-resources parameter="resources.application" />	   
    
    
    <!-- validate   -->    
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames" 
		value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml"/>
	</plug-in>


	
</struts-config>

 
 
4.web-infディレクトリの下でvalidationを確立する.xmlファイルの内容は以下の意味でコメントします.
  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">

<form-validation>
	<formset>

		<!-- An example form -->
		
		<form name="logonForm">
		
			<field property="username" depends="required,maxlength,minlength">		<!-- depends    ,      required username -->
				<arg key="logonForm.username" />				<!--  ,  “** ”   application.properties  -->
				
				
				<arg key="${var:maxlength}"  name="maxlength" resource="false"	/>	<!--     maxlength  application.properties  -->
				<var>
					<var-name>maxlength</var-name>
					<var-value>10</var-value>
				</var>
				
				<arg key="${var:minlength}"  name="minlength" resource="false"	/>	<!--   minlength -->
				<var>
					<var-name>minlength</var-name>
					<var-value>6</var-value>
				</var>
			</field>
			
			<field property="password" depends="required,mask">
				<arg key="logonForm.password" />
				<var>
					<var-name>mask</var-name>
					<var-value>^[0-9a-zA-Z]*$</var-value>
				</var>
			</field>
			
		</form>

	</formset>
</form-validation>

 
 
 
6.FormとActionはそれぞれ次の通りです.
package logon;

import org.apache.struts.validator.ValidatorForm;

public class LogonForm extends ValidatorForm {
	
	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;
	}

}

 
 
LogonAction.java
 
 
package logon;

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

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


public class LogonAction extends DispatchAction {
	


	private static Logger log=Logger.getLogger(LogonAction.class);
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		
		
		LogonForm logonform =(LogonForm)form;
		
		log.info("    "+logonform.getUsername()+"   !");
		
		if(logonform.getUsername().trim().length()>0 && logonform.getPassword().trim().length()>0){
			log.info("    "+logonform.getUsername()+"   !");
			return mapping.findForward("LogonSuccess");
			
		}
		
		return mapping.findForward("LogonFailure");
		
	}
	
}
Ps:これだけだ