SSH框架搭建

閒來無事,來搞下SSH框架的搭建吧。


1.struts搭建

    這個不用說了吧,新建一個web項目,把struts-2.3.7-all解壓後的struts2-blank裏面的lib下面的包全部複製到你新建的web項目裏面,把web-inf下面的web.xml複製過去,把src目錄下的struts.xml複製到新建的src目錄下。最重要的是你要集成spring,那麼問題來了,你少包了,親。去struts-2.3.7-all文件夾的lib目錄下把struts2-spring-plugin-2.3.7包拷貝到新建的lib目錄下面。調試,ok。


2.集成spring

    (1)aspectj文件夾下面的包都拷貝過去

AspectJ是一個面向切面的框架,它擴展了Java語言。spring 使用AOP的時候也會用到它。

(2)spring的核心包肯定少不了了。


  (3)cglib包也是必須的。

cglib是一個強大的,高性能,高質量的Code生成類庫,它可以在運行期擴展Java類與實現Java接口。Hibernate用它來實現PO(Persistent Object 持久化對象)字節碼的動態生成。

(4)j2ee文件夾下的common-annotations也需要

annotation是註解時需要用到。

(5)jakarta-commons文件夾下的commons-logging,commons-pool(java數據庫連接池 包

(6)需要注意的是spring的jar包裏面有slf的api和slf-log4j的轉換包,那麼還需要一個log4j的包,也就是總共三個

slf4j-api-1.5.0,slf4j-log4j12-1.5.0和log4j12-1.5.0這樣輸出log就不會報log什麼的錯誤了。

ok上圖。


目前的jar包就這麼多。

光說不練不行,那麼我們先建立曬曬struts.xml的內容。

<span style="color:#333333;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    

    <package name="default" namespace="/pages" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="userAction" class="</span><strong><span style="color:#ff0000;">userActionBean</span></strong><span style="color:#333333;">">
            <result name="success">success.jsp</result>
        </action>
    </package>

</struts></span>

好,大家看到粗紅體的字了嗎?爲什麼不是類的包路徑呢???!!!對了,因爲action也要交給spring容器實例化的啊。所以,這裏只寫spring配置文件裏的bean的name/id,下面是spring的配置文件applicationContext.xml的內容

<span style="color:#333333;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       </span><span style="color:#ff0000;"><strong>xmlns:context="http://www.springframework.org/schema/context"</strong></span><span style="color:#333333;">      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- Resource註解方式得到實例化的bean -->
</span><strong><span style="color:#ff0000;">  <context:annotation-config/></span></strong><span style="color:#333333;">

  <bean id="userService" class="com.test.xuehf.serivce.UserServiceImp"/>
  <bean id="userActionBean" class="com.test.xuehf.action.UserAction">
  <!-- setter 方法注入  action中必須要有setter方法
  	<property name="userService" ref="userService"></property> -->
  </bean>


</beans></span>

怎麼又有粗紅體了!嗯,不要着急,這個是爲了方便你在action中調用service的時候直接以@Resource註解的方式獲取spring已經實例化的service實例而配置的,必須要寫上這兩句。OK,運行下吧

Caused by: java.lang.NullPointerException

oh,NO!why,爲什麼會報空指針異常呢?這是因爲你的web.xml沒有配置spring的監聽,需要spring去初始化bean纔不會有空指針的出錯啊。

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>
    <!-- 	 -->
	<context-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

那麼ok了,現在爲止spring集成struts已經可以運行了

未完待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章