달력

102018  이전 다음

  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  
  •  

작년부터 스트럿을 썼는데..  Validator 를 한번도 써보지 않았다 ^^;

 

http://struts.apache.org/userGuide/dev_validator.html

 

여기 보니 상당히 자세히 나왔음..

 

간단하게 몸풀기 예제 하나 적어놔야쥐..

 

준비물 :  commons-validator.jar , jakarta-oro.jar  <-- 필수

나머지 준비물 : vssh 에 필요한 lib 들...

 

1. 폼빈 작성(DynaValidatorForm)

 

<form-bean name="loginForm"
               type="org.apache.struts.validator.DynaValidatorForm">
   <form-property name="id" type="java.lang.String" />
   <form-property name="passwd" type="java.lang.String" />
</form-bean>

 

2. ActionServlet 작성 (검증만 할것이기 때문에 암것도 없다..)

package test;

 

import ........

 

public class LoginAction extends Action {
 
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest req,
        HttpServletResponse res
    )throws IOException, ServletException {

        return mapping.findForward("success");

    }

}

 

 

3. action-mappings 에 등록..

   <action-mappings>
    <action
      path="/login"
      type="test.LoginAction"
      name="loginForm"
      scope="request"
      input="/login.vm"
      unknown="false"
      validate="true"
    >
      <forward
        name="success"
        path="/index.do"
        redirect="true"
      />
     

</action-mappings>

 

4. validator-rules.xml 등록..(첨부자료에 있음..)

 

5. validation.xml 만들기..

<?xml version="1.0" encoding="euc-kr" ?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>
 <formset>
  <form name="loginForm">
   <field property="id" depends="required,minlength,maxlength">
    <arg0  key="아이디" resource="false"/>
    <arg1 name="minlength" key="${var:minlength}" resource="false"/>
    <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
    <var>
     <var-name>minlength</var-name>
     <var-value>3</var-value>
    </var>
    <var>
     <var-name>maxlength</var-name>
     <var-value>12</var-value>
    </var>    
   </field>
   <field property="passwd" depends="required">
    <arg0 key="비밀번호" resource="false"/>
   </field>
  </form>
 </formset>
</form-validation>

 

6. messageResource_ko.properties 만들기(native2ascii 해줄것)

# Struts Validator Error Messages
errors.required=<Font color="green">{0}</font> 는(은) 반드시 입력하셔야 합니다!!
errors.minlength=<Font color="green">{0}</font> 는(은) 최소 <Font color="red">{1}</font> 자 이상 입력하셔야 합니다!!
errors.maxlength=<Font color="green">{0}</font> 는(은) 최대 <Font color="red">{1}</font> 자 이하 입니다!!

 

7. struts-config.xml 에  messageResource 등록하기..

<message-resources parameter="messageResource" />

 

8. struts-config.xml 에 Validator PlugIn 등록

 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathnames"
   value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
 </plug-in>

 

9. 입력폼 만들기.

##------------------------------

## PRINT ERROR MESSAGE 

##------------------------------

#macro (errorMarkup)
    #if ($errors.exist())
        <ul>
        #foreach ($e in $errors.all )
            <li>$e</li>
        #end
        </ul>
    #end
#end

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    #errorMarkup()
    </td>
  </tr>
  <tr>
 <form method="POST"
  name="loginForm"
  action="$link.setRelative("/login.do")" >
    <td width="400" height="200" align="center">     
   <fieldset style="width:95%">
  <legend align="left" align="center" width="100%" >&nbsp;<B>로그인 하세요</B>&nbsp;&nbsp;</legend>
  <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#C6C6C6">
          <tr>
            <td width="50%" style="padding-left:6px">ID</td>
   
          <td width="50%" style="padding-left:6px"><input type="text" name="id"  value="$!loginForm.get("id")" /></td>
          </tr>
          <tr>
            <td width="50%" style="padding-left:6px">PWD</td>
   <td width="50%" style="padding-left:6px"><input type="password" name="passwd" value="$!loginForm.get("passwd")" /></td>
          </tr>   
          <tr>
            <td width="50%"style="padding-left:6px" colspan="2">
     <input type="submit" value=" 전 송 " >
   </td>
          </tr>   
        </table>   
   </fieldset>
    </td>
    </form>
  </tr>
</table>

 

편하긴 한데.. 설정 바꾸고.. 리로딩 하고 -.-;;

업로드 되는 곳이면 업로드 하면서 검증하는건지??

 

 

 

 

Posted by Tornado tornado