整個Web應用分為四部分
1、 單獨的DAO,負責數據訪問,執行CUID(create, update, insert ,delete),完成O/R映射。不涉及任何的其他邏輯,僅執行這以上的操作,如果有唯一性檢查、事務也不需要做。(可以在數據庫端加一些trigger,constraint)。該層需要定義DAO接口,DAO實現(hibernate,ibaits,JDBC等)。
2、 簡單的Logic Object(Java Bean),data field+setter+getter+Other Logic,可以將一些共有的操作提取到父類中,減少代碼。
3、 Business Sevice,實現業務邏輯,在此使用DAO和Logic Object完成業務操作。這里當使用到DAO時,只需要IOC注入,真實對象由外界(Web容器調用Spring容器)注入。
4、 編寫Action,將用戶界面的操作映射到Busibess service。
使用Spring MVC的具體配置:
1、在web.xml中, 增加dispatcher的定義,配置好URL Mapping分發用戶調用。
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
編寫相應的action-servlet.xml
2、如果使用了多個context文件(Spring的配置文件),則需要在Web.xml中進行配置,具體如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext1.xml
/WEB-INF/applicationContext2.xml
</param-value>
</context-param>
其位置在“sitemesh filter”之后,但是在filter-mapping之前。
3. 增加ContextLoaderListener.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
對照了Spring in action,那個講的更好哦。從spring的基礎開始講起,很不錯。