ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스트럿츠2 - 세션 로그인 설정
    웹프로그래밍/struts 2018. 5. 29. 22:10

    http://invincure.tistory.com/entry/Struts%EC%97%90%EC%84%9C-%EB%A1%9C%EA%B7%B8%EC%9D%B8login-%EC%B2%98%EB%A6%AC%ED%95%98%EA%B8%B0-%EC%84%B8%EC%85%98sessin-%EC%96%BB%EC%96%B4%EC%99%80%EC%84%9C-sessio%EC%97%90-%EA%B0%92-%EC%A0%80%EC%9E%A5%ED%95%98%EA%B8%B0




    Struts에서 로그인(login) 처리하기. -> 세션(sessin) 얻어와서 sessio에 값 저장하기

     

    /*

     * struts2에서 세션사용하기

     * 방법1)

     * - SessionAware implements 한다.

     * - Session메소드를 오버라이딩 하면 자동으로 세션객체가 파라미터 변수에

     *   저장된다.

     * - 파라미터 변수를 멤버변수 SessionMap 객체에 담아놓는다.

     *

     * 방법2)

     *     ActionContext cxt = ActionContext.getContext();

     *     Map<String, Object> session = ctx.getSession();

     * 

     */

     --- 에제 SourceCode --

    package hb.action;

     

    import java.util.Map;

     

    import org.apache.struts2.dispatcher.SessionMap;

    import org.apache.struts2.interceptor.SessionAware;

     

    import hb.model.IBatisDao;

    import hb.model.MemDto;

     

    import com.opensymphony.xwork2.ActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    import com.opensymphony.xwork2.ModelDriven;

    import com.opensymphony.xwork2.Preparable;

     

    public class LoginAction extends ActionSupport

           implements ModelDriven<MemDto>, Preparable, SessionAware{

           private MemDto dto;

           private String errMsg;

           private SessionMap<String, Object> session;

          

          

           @Override

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

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

           }

          

          

           public String getErrMsg() {

                 return errMsg;

           }

          

           @Override

           public MemDto getModel() {

                 // TODO Auto-generated method stub

                 return dto;

           }

          

           @Override

           public void prepare() throws Exception {

                 dto = new MemDto();       

           }

          

           public String login() throws Exception{

                 boolean result = IBatisDao.isMem(dto);

                 System.out.println(result);

                 if(result){

                        //세션에 사용자 정보 담기

                        session.put("login"dto);

                        return SUCCESS;

                 }

                 else{

                        errMsg = "아이디 또는 비밀번호가 틀립니다.";

                        return LOGIN;

                 }

           }     

          

           public String logout() throws Exception{

                 session.remove("login");

                 return SUCCESS;

           }

          

           //SessionAware 상속받지않고 session 얻어오는 방법

           public void sessionTest(){

                 ActionContext ctx = ActionContext.getContext();

                 Map<String, Object> session = ctx.getSession();

           }

     

    }

     

     

    struts 에서 namespace 사용하기

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

     

    <struts>

    <!--

    struts action 정의 xml에서 namespace사용하기

    struts.xml에서 여러개의 action 정의하는 xml파일을 인클루드  수있다.

    이렇게되면 중볻되는 name 있을  있다그래서 namespace 사용한다.

        사용방법은

         <package name="default" namespace="/aa" extends="struts-default">

        이렇게 defalult namespace="/" 되어있던것을 namespace="/aa" 바꿔준다.

        이렇게하면 action 호출할   페이지(~~.jsp)에서 form 태그의 action="aa/logout.action" 이런식으로 주어야 한다.         

    -->

        <package name="default" namespace="/aa" extends="struts-default">

            <action name="login"  class="hb.action.LoginAction" method="login">

                  <!-- redirect 이동시키기.

                             디폴트로 forwarding이다.

                           - type="dispatcher"  지정되어있다.

                             type="forwarding"  아니다

                  -->

                  <result type="redirect">/member.jsp</result>

                  <result name="login">/login.jsp</result>

            </action>

            <action name="loginForm">

                  <result>/login.jsp</result>

            </action>

            <action name="logout" class="hb.action.LoginAction" method="logout">

                  <result>/logout.jsp</result>

            </action>

        </package>

    </struts>

     



    출처: http://invincure.tistory.com/entry/Struts에서-로그인login-처리하기-세션sessin-얻어와서-sessio에-값-저장하기 [Do you want it? Come get it.]