IT 전용글/Struts

Login 시에 Struts Custom Tag 사용 메세지 띄우기 (사용자 Exception)

회상형인간 2009. 1. 30. 17:55
1)  사용자 Exception class 를 생성한다..(UserNotFoundException , PasswordMismatchException)
2)  1번을 생성하며  Exception 클래스를 상속 받는다. 
소스예시)  소스 확인요망.!!!!
    
3) struts-config.xml 파일에   해당 액션  부분에 추가된 exception  코딩작업.
소스예시)
<action path="/login" type="struts.controller.LoginAction" name="userForm" scope="request">
   <exception
    key="exception.passwordmismatch"
    type="model1.kkh.user.PasswordMismatchException"
    path="/user_login.jsp"
    scope="request"/>
      <exception
       key="exception.usernotfound"
       type="model1.kkh.user.UserNotFoundException"
       path="/user_login.jsp"
       scope="request"/>
      
      <forward name="loginSuccessF" path="/list.do" redirect="true"/>
      <forward name="loginFailIDF" path="/user_login.jsp" redirect="false"/>
      <forward name="loginFailPassF" path="/user_login.jsp" redirect="false"/>
     </action>
4)  익셉션을 전부 throws 처리 후 LoginAction 단에서  throw 처리
소스예시)
if(user==null){
   loginStatus=1;
   throw new UserNotFoundException("해당 아이디가 없습니다.");
  
}else{
   if(user.isMatchPassword(password)){
    loginStatus = 0;
   }else{
    loginStatus = 2;
    throw new PasswordMismatchException("암호가 일치 하지 않습니다.");
   }
  }
 
5) LoginAction 페이지에서  해당  익셉션을 잡은후  에러메세지 저장후 다시 던진다.
소스예시)
 try{
   loginStatus = manager.login(userForm.getUserId(), userForm.getPassword());
   
  }catch(UserNotFoundException e){
   emsg.add("errMSG",new ActionMessage("exception.usernotfound"));
   this.saveMessages(request, emsg);
   throw e;
   
  }catch(PasswordMismatchException e){
   emsg.add("errMSG",new ActionMessage("exception.passwordmismatch"));
   this.saveMessages(request, emsg);
   throw e;
   
  }
6)  이렇게 던지게 되면 컨테이너 단에서 Exception 처리를 하게 되는대  struts-config.xml 파일에  설정해놓은 url로
     포워딩 시킨다.
7) user_login.jsp 파일로 포워딩된후에  그곳에서 자동 생성된 errormsg와  사용자가 저장한 메세지를 뽑아본다.

소스예시)
<!-- 메세지 출력 위치. -->
     
     <logic:messagesPresent message="true">
      <html:messages id="message" message="true" property="errMSG">
       <li>임의로 저장시킨 에러 메세지 : <bean:write name="message"/>
      </html:messages>
     </logic:messagesPresent>


     
     <logic:messagesPresent message="false" >
      <html:messages id="error" >
       <li>액션 에러 메세지 : <bean:write name="error"/>
      </html:messages>
     </logic:messagesPresent>
     

상단 message=false 인거의 뜻은 자동 생성된 메세지라는 뜻입니다(에러메세지)
자동생성된 에러메세지는 키 값을 알수가 없기 때문에 커스텀 태그로 자동으로 받게 되어있습니다.



※자바스크립트 얼럿 창에 띄울 메세지가 있을경우에 쓰인다.

'IT 전용글 > Struts' 카테고리의 다른 글

스트럿츠 Tiles 설정방법. (2009.2.2) 오전  (0) 2009.02.02
ActionMessages  (0) 2009.01.30
Struts 정리  (0) 2009.01.22