2012-09-11 7 views
1

안녕하세요, 저는 Spring MVC 어노테이션이 처음입니다. 지금 Stuts 2.x 컨트롤러 부분을 Spring 어노테이션으로 이동하도록 지정했습니다. 세션 중에 문제가 발생합니다. struts2 세션 인식 기능을 사용했으며, 맵 값을 저장합니다. 봄에는 어떻게하면 도움이 될 수 있나요? ... 그 중 하나에 대한 답변이 더 나를 위해 도움이 .. 일주일에. 여기는 내 struts 2.x 액션 클래스입니다.Struts 2.x에서 Spring 3x MVC로 변경하기

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; 
    } 
} 

이 내가 어떤 속성 세션에서 스프링 고정을 가지고 내 컨트롤러에 SessionAttributes 주석을 사용이 내 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> 

답변

0

내 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; 
    } 


} 

입니다 나는 내 모델을 넣을 것이다. Spring은 모델에 삽입되거나 컨트롤러 메소드에 삽입 될 때 세션에서 이러한 속성을 투명하게 배치하고 검색합니다. javadoc가 가리키고 있듯이,이 속성은 세션에서 일시적으로 속성을 설정할 때 좋은 방법이며, 영원히 저장해야하는 속성에 대해 세션에 직접 액세스하는 다른 방법을 조언합니다. 이 방법은 충분한 세부 사항 here으로 설명됩니다.

아마도 그렇게해야합니다.

예를 들어 보안 데이터의 경우 java.util.HashMap 유형의 응용 프로그램 컨텍스트에서 세션 범위 bean을 만든 다음 컨트롤러에 주입 할 수 있습니다. 이 방법이 좋은 방법인지는 모르겠지만, 필요한 경우이 콩을 서비스에 주입 할 수 있다는 이점이 있습니다.