Struts2でセッションを扱うサンプル

Posted by admin on 2014年1月26日 in 未分類 |
◆セッションを設定するAction

package sample.struts2spring.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestSessionAction implements SessionAware {

    public String initialize() {
        return ActionSupport.SUCCESS;
    }

    public String getUserId() {
        return (String)sessionMap.get("KEY_USER_ID");
    }

    public String getAuthority() {
        return (String)sessionMap.get("KEY_AUTHORITY");
    }

    private Map<String, Object> sessionMap;

    @Override
    public void setSession(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }

    public String setSession() {

        sessionMap.put("KEY_USER_ID", "UID001");
        sessionMap.put("KEY_AUTHORITY", "管理者");

        return ActionSupport.SUCCESS;
    }

    public String clearSession() {

        sessionMap.put("KEY_USER_ID", "");
        sessionMap.put("KEY_AUTHORITY", "");


        return ActionSupport.SUCCESS;
    }
  }


◆セッションを取得するAction
package sample.struts2spring.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestSessionAction2 implements SessionAware {

    public String initialize() {
        return ActionSupport.SUCCESS;
    }

    public String getUserId() {
        return (String)sessionMap.get("KEY_USER_ID");
    }

    public String getAuthority() {
         return (String)sessionMap.get("KEY_AUTHORITY");
    }

    private Map<String, Object> sessionMap;

    @Override
    public void setSession(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }

}

Comments are closed. Would you like to contact the author directly?

Copyright © 2013-2018 ソフトウェアをめぐる冒険 All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.2, from BuyNowShop.com.