看了Struts2的Sample整合Spring和JPA還是無法deploy,所幸昨天跑了一趟書局,買了Struts2權威指南(簡體),還是可以簡單的上手:
lib要放的內容如下:
commons.-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
spring.jar
struts-core-2.0.6.jar
struts-spring-plugin-2.0.6.jar
xwork-2.0.1.jar
(web.xml-->webapp配置:用Listener委給Spring進行管理,用Filter委給Struts2經手請求)
<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>
 
(struts.xml-->Struts2配置:url的login.action對映到com.loginAction這個class)
<struts>
    <package name="com" extends="struts-default">
        <action name="login" class="loginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>       
        </action>
    </package>
</struts>
 
(applicationContext.xml-->Spring配置:com.loginAction同時也是Spring Bean,用IoC的Setter Injection)
    <bean id="IService" class="com.MyServiceImpl"/>        <!-- com.MyServiceImpl是myService的Implementation -->
    <bean id="loginAction" class="com.LoginAction" scope="prototype"> <!-- scope="prototype", Spring2才有,意指每次request就new一次instance -->
        <property name="service" ref="IService"/>                 <!-- setter injection, myServices是Interface -->
    </bean>
</beans>
 
(LoginAction.java節錄)
package com;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
    private IService service;                             // DAO的Interface
    public void setService(IService service) {  // setter injection
        this.service= service;
    }
 
    private String user_id;       // setter/getter,在submit的form裡必須有input tag為user_id
    private String password;    // setter/getter,在submit的form裡必須有input tag為password
 
    public String execute() throws Exception {  // implement Action必須實作的method
        // do something...
        if (this.service.valid(getUser_id(), getPassword()) == false) {
             return SUCCESS;     // Action常數,對映struts.xml的<result name="success">
        } else {
             return ERROR;        // Action常數,對映struts.xml的<result name="error">
        }
    }
}
 
(IService.java)
package com;     // 在上述的config中唯一沒有描述到的便是Interface的package,它只須能被引用它的class如LoginAction引用即可
public interface IService {       // 對映到applicationContext.xml的<property name="service" ref="IService"/>,與LoginAction的setService
    boolean valid(String username , String pass);
}
 
(MyServiceImpl.java)
package com;
public class MyServiceImpl implements IService {   // 對映到applicationContext.xml的<bean id="IService" class="com.MyServiceImpl"/>
    public boolean valid(String user_id , String pass) {
        // validate user_id, password
        return true;
   }
}

2009/2/23補充:回鍋後重新踹Spring+Struts2竟都忘了以下的眉角,主要是Spring的applicationContext.xml與Struts2的struts.xml及web.xml三者配置檔之間的糾葛。
1.Struts2的Action要給Spring容器管理,需在web.xml將Listener設給Spring,Filter才設給Struts2。
2.原本struts.xml配置如<action name="login" class="loginAction">中的class屬性必須為實體class,一旦被Spring托管後,成為在applicationContext.xml裡搜尋bean id的識別碼。是故以上例而言:
struts.xml的
<action name="login" class="loginAction"> 即是applicationContext.xml的<bean id="loginAction" class="com.LoginAction" scope="prototype">,真正實體class則在spring config檔配置


arrow
arrow
    全站熱搜

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