2012-09-11 83 views
1

嗨,我是Spring MVC註釋的新手。我現在正在將基於Spring註解的Stuts 2.x控制器部分移到工作分配上,我在會話中遇到麻煩。 我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; 
    } 
} 

這是我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> 

回答

0

我使用SessionAttributes註解我控制器上有會話的一些屬性彈簧夾持該我會放入我的模型。 Spring將這些屬性放入模型或注入到控制器方法中,透明地放入並從會話中檢索這些屬性。正如javadoc所指出的,當臨時性地在會話中設置屬性時,這是一種很好的方法,並且它建議以另一種方式直接訪問會話以查找應該永久存儲的屬性。該方法用足夠的細節here來描述。

你應該去那。

或者,例如,對於您的安全數據,您可以在類型爲java.util.HashMap的應用程序上下文中創建會話範圍的bean,然後將其注入到控制器中。我不知道這是否是一種好的做法,但優點是可以在需要時將這個bean注入服務中。