从Struts 2.x更改为Spring 3x MVC

时间:2012-09-11 12:08:02

标签: spring spring-mvc

您好我是Spring MVC注释的新手。我现在正在分配将Stuts 2.x控制器部分移动到基于Spring的注释,我在会话时遇到麻烦。 我struts2我使用会话感知,它存储地图vales。在春天我怎么能实现任何人都可以帮助我。 ... 如果任何一个回复...在一周内它对我更有帮助。 这是我的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>

1 个答案:

答案 0 :(得分:0)

我在我的控制器上使用SessionAttributes注释,让会话弹簧保留一些我将放入模型中的属性。 Spring将这些属性透明地放入和检索这些属性,因为它们被放入模型或注入控制器方法。正如javadoc所指出的,当临时在会话中设置属性时,这是一种很好的方法,并且它建议另一种方法来直接访问应该永久存储的属性的会话。描述了此方法的详细信息here

你应该为此而努力。

或者,例如,对于安全性数据,您可以在类型为java.util.HashMap的应用程序上下文中创建一个会话范围的bean,然后将其注入控制器。我不知道这是不是一个好习惯,但优点是你可以根据需要在服务中注入这个bean。