loginAction.xml 

<struts>

<package name="login" extends="struts-default">

<action name="joinForm">

<result name="success">/jh/jsp/login/joinForm.jsp</result>

</action>

<action name="join" class="jh.login.action.UserJoinAction">

<result name="success" type="redirect">/list.do</result>

</action>


<action name="login" class="jh.login.action.UserLoginAction" method="login">

<result name="success" type="redirect">/list.do</result>

<result name="input">/jh/jsp/error/error.jsp</result>

</action>

<action name="loginForm">

<result name="success" type="dispatcher">/jh/jsp/login/loginForm.jsp</result>

</action>

<action name="logout" class="jh.login.action.UserLoginAction" method="logout">

<result name="success" type="redirect">/loginForm.do</result>

</action>

 

</package>

</struts>



UserLoginAction.java

public class UserLoginAction extends ActionSupport implements SessionAware {


private LoginVO vo = new LoginVO(); 

private LoginBO bo = new LoginBO();

private String userid;

private String passwd;

private String result;

SessionMap<String,Object> session;

String errMsg;

public String login() throws Exception{

vo.setUserid(userid);

vo.setPasswd(passwd);

/* [ Board 2013 ] 2013.07.12 이재희 : id,pw db에서 찾아 그 결과 값을 담는다. */

result = bo.login(vo);

if(result!=null){ /* [ Board 2013 ] 2013.07.12 이재희 : id,pw 존재하고 일치한다면 */

session.put("userid", userid); // 세션에 userid 저장

return SUCCESS;

}else{

/* [ Board 2013 ] 2013.07.12 이재희 : 인증이 잘못되어 있을 경우 result값을 다르게 주어서 다른 페이지(view)로 이동시킨다. */

            addFieldError("err.msg", "잘못된 로그인입니다"); // struts 태그 error 태그의 메시지값을 담는다.

            return INPUT;

}

}

public String logout() throws Exception{

session.remove("userid");

return SUCCESS;

}



/**

* @Method Name  : setSession

* @작성일     : 2013. 7. 10. 

* @작성자     : 이재희

* @param    :

* @Method 설명 :

*/

public void setSession(Map<String, Object> session) {

this.session = (SessionMap<String,Object>) session;

}



/**

* @return the userid

*/

public String getUserid() {

return userid;

}



/**

* @param userid the userid to set

*/

public void setUserid(String userid) {

this.userid = userid;

}



/**

* @return the passwd

*/

public String getPasswd() {

return passwd;

}



/**

* @param passwd the passwd to set

*/

public void setPasswd(String passwd) {

this.passwd = passwd;

}


}



loginForm.jsp

<form name="UserLoginForm" method="post" action="/login.do">

<table width="100%" border="0" cellspacing="0" cellpadding="1">

<tr>

<td colspan="2" id="td0">아이디 비밀번호</td>

</tr>

<tr>

<td id="td1"><input type="text" name="userid" id="tx1"

maxlength="12" /></td>

</tr>

<tr>

<td id="td1"><input type="password" name="passwd" id="tx1"

maxlength="16" /></td>

</tr>

<tr>

<td id="td11"><a id="bt_login" class="a_default" href="#"

onclick="FormSubmit( UserLoginForm );">Login</a> <a id="bt_login"

class="a_default" href="/joinForm.do">Join</a></td>

</tr>

</table>

</form>



loginok.jsp

<s:if test="#session.userid != null">

<%=session.getAttribute("userid")%> 님 안녕하세요.

<a href="/logout.do">로그아웃</a>

</s:if>

<s:else>

<script type='text/javascript'>

<!--

alert("로그인 바랍니다.");

setTimeout("location.href=' " + window.location.href + "/loginForm.do'",500);

//-->

</script>

</s:else>

+ Recent posts