整個(gè)Web應(yīng)用分為四部分
1、 單獨(dú)的DAO,負(fù)責(zé)數(shù)據(jù)訪問,執(zhí)行CUID(create, update, insert ,delete),完成O/R映射。不涉及任何的其他邏輯,僅執(zhí)行這以上的操作,如果有唯一性檢查、事務(wù)也不需要做。(可以在數(shù)據(jù)庫端加一些trigger,constraint)。該層需要定義DAO接口,DAO實(shí)現(xiàn)(hibernate,ibaits,JDBC等)。
2、 簡(jiǎn)單的Logic Object(Java Bean),data field+setter+getter+Other Logic,可以將一些共有的操作提取到父類中,減少代碼。
3、 Business Sevice,實(shí)現(xiàn)業(yè)務(wù)邏輯,在此使用DAO和Logic Object完成業(yè)務(wù)操作。這里當(dāng)使用到DAO時(shí),只需要IOC注入,真實(shí)對(duì)象由外界(Web容器調(diào)用Spring容器)注入。
4、 編寫Action,將用戶界面的操作映射到Busibess service。
使用Spring MVC的具體配置:
1、在web.xml中, 增加dispatcher的定義,配置好URL Mapping分發(fā)用戶調(diào)用。
<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>
編寫相應(yīng)的action-servlet.xml
2、如果使用了多個(gè)context文件(Spring的配置文件),則需要在Web.xml中進(jìn)行配置,具體如下:
<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>
對(duì)照了Spring in action,那個(gè)講的更好哦。從spring的基礎(chǔ)開始講起,很不錯(cuò)。