close

  Spring3於2009/12/16釋出3.0版,於2010/2/18釋出3.0.1版。把Spring3引為架構核心,必須用Spring3的新功能和既有的Struts1搭配,終於有成果了。

  古早時代寫的一篇Struts 1被Spring托管的配置,繼續沿用。弄一個LoginAction為飯粒:

  1. struts-config.xml

    <struts-config> 
        <form-beans>
            <form-bean name="loginForm" type="com.foo.demo.form.LoginForm"/>
        </form-beans>
        <action-mappings>
            <action path="/login" name="loginForm"
                parameter="login"
                input="/jsp/Login.jsp">
                <forward name="success" path="/jsp/OK.jsp" />
            </action>
        </action-mappings>
       
    <controller>
            <set-property property="processorClass"
                value="org.springframework.web.struts.DelegatingRequestProcessor" />
        </controller>
    </struts-config>

      從上例得知,因為設定了controller為Spring的DelegatingRequestProcessor,<action>標籤毋須再指定type屬性(即full class name)。

  2. applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="
    http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
        <!--
    <context:annotation-config/> --> 
        <context:component-scan base-package="com.foo.demo"/>
       
    <!--
        <bean name="/login" class="com.foo.demo.action.LoginAction"/>
         -->

    </beans>

    1. 到Spring3比較煩的一點是,要引入一堆xmlns的schema檔(藍字部份)。
    2. 但好處是只要定好粗紅字部份那兩行,就不用再像過去一樣,一個struts-config.xml裡的action path要對到一個applicationContext.xml裡的bean name(即灰字被註解掉部份)
    3. 第一行紅字<context:annotation-config/>表示啟動Spring的annotation功能。 而若啟動context:component-scan第二行,則本行可以移除。
    4. 第二行紅字<context:component-scan base-package="com.foo.demo"/>顧名思義。要scan package "com.foo.demo"底下有定義@Component的類別,@Component在Spring 2.5就有提供,等同把一個類別當作一個applicationContext.xml來使用,用法見第四點。
  3. com.foo.demo.LoginAction.java (普通的Struts 1 Action)

    public class LoginAction extends MappingDispatchAction {
        private static Logger logger = Logger.getLogger(LoginAction.class);
        public ActionForward login(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response) {
            LoginForm loginForm = (LoginForm) form;
            logger.info("UserID=" + loginForm.getUserid());
            logger.info("Password=" + loginForm.getPassword());
            return mapping.findForward("success");
        }
    }

    不用多做說明,LoginForm應該大家都會寫了。重點Struts怎麼知道要用這個類別?在下一個說明。

  4. com.foo.demo.DemoConfig.java

    @Component
    public class DemoConfig {
        private static Logger logger = Logger.getLogger(DemoConfig.class);
        @Bean(name="/login")
        public LoginAction loginAction() {
            logger.info("come in");
            return new LoginAction();
        }
    }

  從上例看出很單純,由<context:component-scan base-package="com.foo.demo"/>找到有@Component的類別,而@Bean則是等同被註解掉的<bean name="/login" class="com.foo.demo.action.LoginAction"/>。若不設定name的屬性和struts的path匹配,在Tomcat會出現No action instance for path /login could be created的錯誤訊息。這樣做的好處除了不用維護兩份日益膨脹的configuration file以外,在維護增修案功效也比較明顯,動到舊有的設定機會就較少。@Component換作@Configuration也應該可行,但還沒確認這兩者間的差別。

arrow
arrow
    全站熱搜

    Jemmy 發表在 痞客邦 留言(0) 人氣()