一、從已有項目代碼瞭解ssh
(這裏說的是struts1)
首先,默認創建好了一個項目,你去了解其中的業務流程。拿到一個項目,先從web.xml開始瞭解。瞭解其加載順序:ServletContext -> listener -> filter -> servlet
同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。我們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是不是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,因此真正的加載順序爲:context-param -> listener -> filter -> servlet
對於某類配置節而言,與它們出現的順序是有關的。以 filter 爲例,web.xml 中當然可以定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這裏一定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出現在 filter 之後,否則當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。web 容器啓動時初始化每個 filter 時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的。
servlet 同 filter 類似,此處不再贅述。
由此,可以看出,web.xml 的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的(更多關於web.xml的我是從web.xml詳解瞭解)。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-*.xml
</param-value>
</context-param>
由classpath,跳轉到查看spring的管理層面。
spring要配置數據訪問層信息。
<bean id="statChannelChartBean" class="com.cic.cooperator.flashchart.StatChannelLine" scope="prototype">
<property name="coopCoreService">
<ref local="coopCoreService"/>
</property>
</bean>
spring要進行一些bean的管理,即配置控制器併爲其注入業務邏輯層組件。
<bean name="/channelV3" class="com.cic.cooperator.action.V3.ChannelAction">
<property name="coopCoreService">
<ref bean="coopCoreService"/>業務邏輯層組件
</property>
</bean>
其中id 和 name的區別:
id是唯一標識bean.不能用特殊字符:×#@ ,不能用數字開頭。在bean引用的時候只能有id指向你需要的bean
name 可以用特殊字符,並且一個bean可以用多個名稱:name=“bean1,bean2,bean3” ,用逗號隔開。如果沒有id,則name的第一個名稱默認是id
通過id和name都可以取出該Bean.
<bean id=“thisbean” name=“bean1,bean2,bean3” class=“org.example.Dao" />
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("config.xml"));
Dao dao=(Dao)factory.getBean("thisbean");
Dao dao1=(Dao)factory.getBean("bean1");
Dao dao2=(Dao)factory.getBean("bean2"); .........
// 驗證是否指向同一個Bean
System.out.println((dao== dao1));
System.out.println((dao1== dao2));
// 輸出bean的別名
String[] thisbean= factory.getAliases("thisbean");
for (String strbean: thisbean) {
System.out.println(strbean);
}
同時需要在struts.xml文件中配置該控制器,制定該控制器的處理路徑以及跳轉頁面信息。
<action path="/channelV3"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request"
parameter="method">
<forward name="toShowChannelAssign" path="/V2/frame/channel_assign.jsp" />
<forward name="toNOV2Cooperator" path="/V2/channel_no.jsp" />
<forward name="toShowGuide" path="/V2/guide_page_home.jsp?step=1" />
<forward name="toShowIndex" path="/indexV3.do?method=showIndex" redirect="true"/>
<forward name="toShowDashBoard" path="/dashBoard.do?method=showDashBoard" redirect="true"/>
<forward name="toNOChannel" path="/V2/channel_none.jsp" />
</action>
(由class跳轉到Action操作)spring由註解功能(註解就是可以直接引用,不許要new出來),註解的方式由兩種:
1.用set方法:
public class ChannelAction extends DispatchAction {
private CoopCoreService coopCoreService ;
public void setCoopCoreService(CoopCoreService coopCoreService) {
this.coopCoreService = coopCoreService;
}
2.用@方法:
@Repository
public class TcoopRuleDAO extends BaseDao {
對於要被註解調用的類要先用@repository標註,才能用@resource註解調用。@Service
public class TcoopRuleServiceImpl implements TcoopRuleITFService {
protected Logger logger = Logger.getLogger("TcoopRuleLogServiceImpl");
@Resource
private TcoopRuleDAO tcoopRuleDAO;
@Resource
private GlobalUserDAO globalUserDao;
@Resource
private TcoopServiceDAO tcoopServiceDAO;
@Service、@Resource,可以在類中直接調用方法,不需要再new出來。
struts會進行一些由action操作中的一些返回關鍵字到jsp界面的跳轉。
<forward name="toShowIndex" path="/V2/index.jsp"></forward>
<forward name="toShowAccount" path="/V2/account_detail.jsp"></forward>
二、着手創建一個項目(此處參考ssh設計)
1、關於搭建3框架問題,MyEclipse版本用5.5或6.0.1版本的容易成功,具體原因是包衝突。注意搭建時3框架加入的次序。一種是自底向上,順序爲hibernate->spring->struts。一種是自頂向下,順序是Struts->Spring->Hibernate。當然熟練到一定程度,可以不按這兩種方式了。選擇Spring框架和Hibernate框架時注意選擇copy library選項,具體原因是包衝突時,刪除相對容易些。
2、選擇Spring框架時加入AOP Libraries,Core Libraries,Persistence Core Libraries,以及Web Libraries類包就行了;選擇Hibernate框架時,加入Hibernate __Core Libraries-<MyEclipse-Library>類包就可以了。如果報錯的話一般是缺少類包,只要在網上下載最新版本的類包既可以了。
3、整合SS時Spring的加載有兩種方式:一種是在Struts-config.xml中實現,在</struts-config>之前加入<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
注意將<action中的type屬性改爲type="org.springframework.web.struts.DelegatingActionProxy";另一種方式:在web.xml中實現,加載如下,
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!--通過servlet加載-->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
</servlet>
<!--通過listener加載,兩種方法選一即可以
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>-->
同樣注意將Struts-config.xml的“<action”中的type屬性改爲type="org.springframework.web.struts.DelegatingActionProxy"。方式二可以加入多個Spring配置文件,本文中applicationContext.xml文件路徑"/WEB-INF/applicationContext.xml"
4、用Hibenate生成逆向工程時,使<generator class="increment" />,可以減少好多不必要麻煩
5、Spring整合Hibernate要用到事務提交機制,否則的話數據不能保存到數據庫中,之所以MySQL可以保存,是因爲MySQL本身的bug問題。具體操作如下:在applicationContext.xml中,加入下列代碼,
<!-- 聲明一個 Hibernate 3 的 事務管理器供代理類自動管理事務用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="UsersDAOProxy"<!--此處bean id可以自己取-->
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 注意這個屬性, 詳細意義請參考文章開頭的參考資料, 必須爲 true 使用CGLIB纔不用強制編寫DAO接口 -->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref bean="transactionManager" /><!--注意保持一致-->
</property>
<property name="target">
<ref local="UsersDAO" /><!--注意此處local指的是本文件中可以操作entity的DAO,或自己寫,或Hibernate自動生成-->
</property>
<property name="transactionAttributes">
<props>
<!-- 這裏的方法簽名可以精確到方法, 先懶惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
此處還要注意的一件事是,以前有DAO做的配置,全部要改成有DAO代理來配置。
6、整合完畢後,一般報asm的錯誤,原因是Referenced Libraries中,asm.jar和asm-2.2.3.jar衝突,刪掉asm-2.2.3.jar即可。
7、log4j.properties文件,在工程名上點擊右鍵new一個file命名爲log4j.properties,確定後。打開在裏邊寫入:
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
後保存,重新發布工程,重啓Tomcat,可以在console中看到出錯信息,用於查找錯誤
8、在spring中如何配置的log4j?
在web.xml中加入以下代碼即可。
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>