2

こんにちは、Spring MVC アノテーションは初めてです。現在、Stuts 2.x コントローラー パーツを Spring アノテーション ベースに移動する割り当てを受けています。セッション中に問題が発生しています。私は struts2 を使用しました セッションを認識し、そのストアはマップ値です。春にはどうすれば達成できますか?誰か助けてください。... 1 週間以内に 1 人でも返信があれば、もっと役に立ちます。これが私のStruts 2.xアクションクラスです。

import java.util.HashMap; 
import java.util.Map; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.cmt.admin.business.AdminService; 
import com.cmt.admin.dao.User; 
import com.cmt.common.constants.CommonConstants; 

public class LoginAction extends CMTAction { 
    private String userName; 
    private String profileName; 
    private String password; 
    private Map<String, String> securityData; 
    String pageName = CommonConstants.INDEX; 
    private String menuName; 

    @Autowired private AdminService cmtAdminService; 

    @Override 
    @SuppressWarnings("unchecked") 
    public String execute() throws Exception { 
        if((userName==null || (CommonConstants.EMPTY_STRING).equals(userName)) && (password==null || (CommonConstants.EMPTY_STRING).equals(password))) { 
            return CommonConstants.INVALID; 
        } 

        Map<String, Map<String, String>> securityData = new HashMap<String, Map<String, String>>(); 
        String returnStatus = CommonConstants.SUCCESS; 
        Map<String,Object> resultData = null; 
        Boolean validUser = false; 
        this.getSession().put(CommonConstants.MENU_NAME, CommonConstants.HOME); 
        resultData = cmtAdminService.loginProcess(userName, password,profileName); 
        validUser = (Boolean) resultData.get(CommonConstants.IS_VALID_USER); 
        if (validUser) { 
            User logggedUser = (User) resultData.get(CommonConstants.LOGGED_INUSER); 
            this.getSession().put(CommonConstants.LOGGED_INUSER, logggedUser); 
            securityData = (HashMap<String, Map<String, String>>) resultData.get( CommonConstants.SECURITY_DATA); 
            this.getSession().put(CommonConstants.SECURITY_DATA, securityData); 
            returnStatus = getPageSecurityData(pageName); return returnStatus; 
        } else { 
            showErrorMessage(CommonConstants.INVALID_USERNAME_PASSWRD_ERROR); 
            return CommonConstants.INVALID; 
        } 
    } 

    public String index() { 
        return CommonConstants.INDEX; 
    } 

    public String home() throws Exception { 
        if (this.getSession()==null || this.getSession().get(CommonConstants.LOGGED_INUSER) == null || "".equals(this.getSession().get(CommonConstants.LOGGED_INUSER))) { 
            return CommonConstants.INVALID; 
        } 
        String returnStatus = CommonConstants.SUCCESS; 
        returnStatus = getPageSecurityData(pageName); 
        return returnStatus; 
    } 

    private String getPageSecurityData(String pageName) throws Exception { 
        String returnStatus = CommonConstants.SUCCESS; 
        Map<String, Map<String, String>> securtyData = new HashMap<String, Map<String, String>>(); 
        Map<String, Object> resultData = new HashMap<String, Object>(); 
        Map<String, String> currentPageSecurty = new HashMap<String, String>(); 
        securtyData = (HashMap<String, Map<String, String>>) this.getSession().get( CommonConstants.SECURITY_DATA); 
        resultData = cmtAdminService.getPageSecurityData(securtyData,pageName); 
        currentPageSecurty = (HashMap<String, String>) resultData .get(CommonConstants.CURRENT_PAG_SECRTY_INFO); 
        returnStatus = (String) resultData.get(CommonConstants.PAGE_ACTION); 
        setSecurityData(currentPageSecurty); 
        setMenuName((String) this.getSession().get(CommonConstants.MENU_NAME)); 
        return returnStatus; 
    } 

    public String getMenuName() { 
        return menuName; 
    } 
    public void setMenuName(String menuName) { 
        this.menuName = menuName; 
    } 
    public Map<String, String> getSecurityData() { 
        return securityData; 
    } 
    public void setSecurityData(Map<String, String> securityData) { 
        this.securityData = securityData; 
    } 
    public void setUsername(String value) { 
        this.userName = value; 
    } 
    public String getUsername() { 
        return userName; 
    } 
    public void setPassword(String password) { 
        this.password = password;             
    } 
    public String getPassword() { 
        return password; 
    }
    public String getProfileName() { 
        return profileName; 
    } 
    public void setProfileName(String profileName) { 
        this.profileName = profileName; 
    } 
}

これは私のCMTActionクラスです

    package com.cmt.admin.web.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.cmt.admin.dao.User;
import com.cmt.common.constants.CommonConstants;
import com.opensymphony.xwork2.ActionSupport;

public class CMTAction extends ActionSupport implements SessionAware
{

    // This Map will contain vales in Session
    private Map<String, Object> sessionMap;

    protected User getLoggedInUser()
    {

        User user = (User) this.getSession().get(CommonConstants.LOGGED_INUSER);
        return user;
    }

    protected void showActionMessage(String message)
    {
        addActionMessage(message);
    }

    protected void showErrorMessage(String message)
    {
        addActionError(message);
    }

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

    public Map<String, Object> getSession()
    {
        return sessionMap;
    }


}

これは私のstruts.xmlファイルです

<?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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.ui.theme" value="css_xhtml" />
    <constant name="struts.custom.i18n.resources" value="resources.message,resources.label" />
    <package name="default" namespace="/" extends="struts-default,json-default">

        <interceptors>
            <interceptor name="sessionTimedOut"
                class="com.cmt.common.interceptors.SessionTimeOutInterceptor" />
            <interceptor name="sessionCheck" class="com.cmt.common.interceptors.SessionCheckInterceptor"/>
            <interceptor-stack name="CMTStack">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="sessionTimedOut" />
                <interceptor-ref name="sessionCheck" />
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="CMTStack"/>
        <default-action-ref name="index" />
        <global-results>
            <result name="index">/jsp/admin/pgLogin.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="sessionTimedOut">/jsp/admin/pgSessionTimedOut.jsp</result>
            <result name="invalid">/jsp/admin/pgLogin.jsp</result>
            <result name="uploadCodeComments">/jsp/iConfigure/pgUploadCode.jsp</result>


        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="error" />
        </global-exception-mappings>

        <action name="index" method="index" class="LoginAction">
            <result name="index">/jsp/admin/pgLogin.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>
        <action name="Login" method="execute" class="LoginAction">
            <result name="success">/jsp/admin/pgIndex.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>
        <action name="Logout" method="logout" class="LogoutAction">
            <result name="success">/jsp/admin/pgLogin.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>       
        <action name="home" method="home" class="LoginAction">
            <result name="success">/jsp/admin/pgIndex.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>
        <action name="search" method="execute" class="SearchAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success">/jsp/iCRL/pgiCRL.jsp</result>
        </action>
        <action name="icrl" method="search" class="SearchAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success">/jsp/iCRL/pgiCRL.jsp</result>
        </action>
        <action name="searchCodes" method="searchCodes" class="SearchAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success" type="json"></result>
        </action>
        <action name="searchicrl" method="submitPage" class="SearchAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success" type="json"></result>
        </action>


        <action name="iconfdtl" method="retrieveIConfigureDetails" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success">/jsp/iConfigure/pgiConfigureReviewStep2.jsp
            </result>
        </action>
        <action name="retrieveCodeList" method="retrieveCodeList" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success" type="json">
            </result>
        </action>

        <action name="filterassc" method="filterAssociationCodes" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success" type="json">
            </result>
        </action>

        <action name="codeSearch" method="retrieveIConfigureDetails" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="invalid">/jsp/admin/pgSessionTimedOut.jsp</result>
            <result name="success">/jsp/iConfigure/pgiConfigureReviewStep2.jsp
            </result>
        </action>

        <action name="iconfaddcategory" method="retrieveIConfigureViewDetails"
            class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success">/jsp/iConfigure/pgiConfigureCategory.jsp</result>
        </action>

        <action name="iconfviewdtl" method="retrieveIConfigureViewDetails"
            class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>
        <action name="reloadconfdtl" method="retrieveIConfigureViewDetails"
            class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>

        <action name="populateaddnew" method="showAddNewForm" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>
        <action name="editaddnew" method="populateEditAssociationData" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>
        <action name="saverow" method="saveRowData" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="success">/jsp/iConfigure/pgiConfigureReviewStep2.jsp</result>
        </action>
        <action name="editassoc" method="saveEditedAddNewData" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>

    <action name="populateOrgUnit" method="populateOrgUnit" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>
     <action name="populateUserAccount" method="populateUserAccount" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result name="invalid">/jsp/admin/pgSessionTimedOut.jsp</result>
            <result type="json" name="success"></result>
    </action>

        <action name="addassoc" method="saveNewCodeAssociation" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>
        <action name="saveNewCategory" method="saveNewCategory" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>

        <action name="codeView" method="getCodeViewDetails" class="CodeViewAction">
            <result name="success">/jsp/iCRL/pgiCRLDetailView.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>
                <action name="iPolicyStepThreeSubmit" class="IPolicyAction" method="iPolicyList" >               
            <result name="success">/jsp/iPolicy/pgiPolicyDetailsStep4.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>
        <action name="iPolicyStepFourSubmit" class="IPolicyAction" method="iPolicyList" >                
            <result name="success">/jsp/iPolicy/pgiPolicy.jsp</result>
            <result name="error">/jsp/admin/pgError.jsp</result>
        </action>

        <action name="findCategoryName" method="findCategoryName" class="IConfigureAction">
            <result name="error">/jsp/admin/pgError.jsp</result>
            <result type="json" name="success"></result>
        </action>

    </package>

</struts>
4

1 に答える 1

0

コントローラーでSessionAttributesアノテーションを使用して、モデルに配置するいくつかの属性をセッションでスプリングに保持させます。これらの属性がモデルに挿入されるか、コントローラー メソッドに挿入されると、Spring は透過的にセッションにこれらの属性を挿入および取得します。javadoc が示すように、これは属性を一時的にセッションに設定する場合に適した方法であり、永続的に保存する必要がある属性についてセッションに直接アクセスする別の方法をアドバイスします。この方法については、ここで十分に詳細に説明されています。

あなたはおそらくそれに行くべきです。

または、たとえばセキュリティ データの場合は、タイプ java.util.HashMap のアプリケーション コンテキストでセッション スコープの Bean を作成し、それをコントローラに注入することもできます。これが良い習慣かどうかはわかりませんが、必要に応じてこの Bean をサービスに注入できるという利点があります。

于 2012-09-11T16:55:44.570 に答える