close
Struts 2真的是很多功能與Spring2有重複的功能,但Struts 2強項是學Ruby on Rails,在表現層的url映射與可搭配多種前端顯示技術,而Spring2則在IoC部份略勝一籌。而在Struts 2在給Spring2托管,是否所有資源都被Spring2主宰?目前是個疑問。
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>
從上面web.xml的設定,Listener比Filter先被Web Container攔截,照書上說法,會由struts2-spring-plugin.jar作為transfer。因此下面的struts.xml就有個疑問?
<action name="HelloWorld" class="com.HelloWorld">
    <result>/HelloWorld.jsp</result>
</action>
<action name="HelloSpring" class="helloSpring">
    <result>/HelloWorld.jsp</result>
</action>
上述的HelloSpring,helloSpring這時不是類別,而是id,指向ApplicationContext.xml的<bean id="helloSpring" class="XXX.YYY"/>,這時執行HelloSpring.action的類別才是"XXX.YYY";但HelloWorld的com.HelloWorld是真的類別,也未在Spring的config上有所設定,因此疑問如下:
1. com.HelloWorld是由Spring去new instance還是由Struts 2去new instance?
2. 若兩者皆有可能,那Web Container是會先把class name當成bean id去ApplicationContext.xml找,還是當成class name去classpath找?

後來剛剛做個測試,先寫個helloSpring.java,沒有package,在Tomcat 6下,是抓Spring的bean,之後將ApplicationContext.xml的helloSpring這個bean給拿掉,果然是抓到我寫的helloSpring.java。
 
arrow
arrow
    全站熱搜

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