1. 程式人生 > >easyui+spring+springmvc+ibatis學習(一)

easyui+spring+springmvc+ibatis學習(一)

使用easyui+spring+springmvc+ibatis搭建一個管理系統的專案,效果圖如下:



首先,基於建立一個maven工程,轉換為web專案,我的專案結構如下:


我使用的是jetty伺服器,jetty的配置檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Serve" class="org.mortbay.jetty.Server">
    <Call name="addConnector">
      <Arg>
          <New class="org.mortbay.jetty.nio.SelectChannelConnector">
            <Set name="host"><SystemProperty name="jetty.host" /></Set>
            <Set name="port"><SystemProperty name="jetty.port" default="9090"/></Set>
          </New>
      </Arg>
    </Call>
</Configure>

在pom.xml中引入相關的jar包,web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		 xmlns="http://java.sun.com/xml/ns/javaee" 
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
		 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <!-- 應用程式名 -->
  <display-name>easyui</display-name>
  
  <!-- 應用程式描述說明文字 -->
  <description>spring springmvc ibatis easyui</description>
  
  <!-- ServletContext初始化引數 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <context-param>   
    <param-name>log4jConfigLocation</param-name>   
    <param-value>classpath:log4j.properties</param-value>   
  </context-param> 
  
  <!-- 預設起始頁面 -->
  <welcome-file-list>
  	<!--welcome-file-list一般情況下只能使用靜態網頁,如果非要把他配置成SpringMVC的控制器URL就會報錯. -->
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring security 過濾器    
  <filter>   
    <filter-name>springSecurityFilterChain</filter-name>   
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>   
  </filter>   
  <filter-mapping>   
    <filter-name>springSecurityFilterChain</filter-name>   
    <url-pattern>/*</url-pattern>   
  </filter-mapping> -->
  
  <!-- 配置監聽器,用於初始化 -->
  <listener>
  	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!--Servlet初始化引數,配置springmvc特性  -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 登陸過濾 -->
  <filter>
  	<filter-name>LoginFilter</filter-name>
  	<filter-class>com.cjb.test.filter.MyLoginFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>LoginFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

spring的配置檔案applicationContext.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring框架配置檔案 -->


<!-- 配置ContextLoaderListener表示,該工程要以spring的方式啟動。啟動時會預設在/WEB-INF目錄下查詢applicationContext.xml
作為spring容器的配置檔案,這裡可以初始化一些bean,如DataSource和iBATIS的整合 -->
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
      <span style="white-space:pre">	</span>http://www.springframework.org/schema/aop  
      <span style="white-space:pre">	</span>http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">  


  
   <!-- 自動掃描 所有註解 註冊為bean (啟動註解)-->   
   <context:component-scan base-package="com.cjb.test">
   <!-- 自動掃描元件,這裡要把web下面的 controller去除,他們是在springmvc-servlet.xml中配置的,如果不去除會影響事務管理的。-->  
   <span style="white-space:pre">		</span><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan> 
  


<span style="white-space:pre">	</span><!-- DataSource定義 -->
<span style="white-space:pre">	</span><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<span style="white-space:pre">		</span><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<span style="white-space:pre">		</span><property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
<span style="white-space:pre">		</span><property name="username" value="chengjunbo" />
<span style="white-space:pre">		</span><property name="password" value="chengjunbo" />
<span style="white-space:pre">		</span><property name="maxActive" value="50" />
<span style="white-space:pre">		</span><property name="maxIdle" value="5" />
<span style="white-space:pre">		</span><property name="maxWait" value="5000" />
<span style="white-space:pre">		</span><property name="removeAbandoned" value="true" />
<span style="white-space:pre">		</span><property name="removeAbandonedTimeout" value="5" />
<span style="white-space:pre">	</span></bean>


<span style="white-space:pre">	</span><!--  配置事務管理器-->
<span style="white-space:pre">	</span><bean id="transactionManager"
<span style="white-space:pre">		</span>class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<span style="white-space:pre">		</span><property name="dataSource" ref="dataSource" />
<span style="white-space:pre">	</span></bean>
 
    <!-- 啟動spring事務註解,事務註解盡在此 --> 
    <tx:annotation-driven transaction-manager="transactionManager"/>


<span style="white-space:pre">	</span><bean id="transactionTemplate"
<span style="white-space:pre">		</span>class="org.springframework.transaction.support.TransactionTemplate">
<span style="white-space:pre">		</span><property name="transactionManager" ref="transactionManager" />
<span style="white-space:pre">	</span></bean>


<span style="white-space:pre">	</span><!-- 配置SqlMapClient物件 -->
<span style="white-space:pre">	</span><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<span style="white-space:pre">		</span><property name="dataSource" ref="dataSource" />
<span style="white-space:pre">		</span><property name="configLocation" value="classpath:sql-map-config.xml"/>
<span style="white-space:pre">	</span></bean>
    <!--根據sqlMapClien建立一個SqlMapClient模版類-->   
    <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <span style="white-space:pre">	</span><property name="sqlMapClient" ref="sqlMapClient"/>
    </bean>
</beans>

springmvc的配置檔案springmvc-servlet.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--springmvc-servlet.xml(Springmvc框架配置檔案)該檔案是springmvc框架配置檔案,也是它的核心檔案  -->

<!-- Bean頭部 
配置DispatcherServlet表示,該工程將採用springmvc的方式。啟動時也會預設在/WEB-INF目錄下查詢XXX-servlet.xml作為配置檔案,
XXX就是DispatcherServlet的名字,該檔案中將配置兩項重要的mvc特性:
HandlerMapping,負責為DispatcherServlet這個前端控制器的請求查詢Controller;
ViewResolver,負責為DispatcherServlet查詢ModelAndView的檢視解析器
-->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
						http://www.springframework.org/schema/util
  						http://www.springframework.org/schema/util/spring-util.xsd
             			http://www.springframework.org/schema/context 
             			http://www.springframework.org/schema/context/spring-context-3.0.xsd  
             			http://www.springframework.org/schema/mvc 
             			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  default-autowire="byName">
	 <!-- default-autowire="byName",約定優於配置,約定優於配置 -->  
	
	<!-- 預設的註解對映的支援 -->  
    <mvc:annotation-driven/>
	
	<!-- 如果當前請求為“/”時,則轉發到“/helloworld/index” -->
    <mvc:view-controller path="/" view-name="forward:/login.jsp"/> 
    
	<!-- 自動掃描,完成bean建立和依賴注入   把標記了@Controller註解的類轉換為bean--> 
	<context:component-scan base-package="com.cjb.test.web.controller"  use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

	
	<!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->  
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
		<!-- 在Controller的某個方法上加@ResponseBody註解,表示該方法的返回結果直接寫入HTTP response body中,返回一個字串,
		spring mvc中很好的支援了json格式,配置檔案中新增json轉換器,如果不加這個註解,你是看不到效果的 -->
		<property name="messageConverters">
			<list>
			<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
			</bean></list>
		</property>
	</bean>
	
	<!-- HandlerMapping -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <!-- 檢視解析器 --> 
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	<!--  靜態資源對映 EasyUI樣式,JS配置  -->
	<mvc:annotation-driven/>
	<mvc:resources location="/jslib/easyui-1.4.3/" mapping="/jslib/easyui-1.4.3/**"/>
	<mvc:resources location="/jslib/css/" mapping="/jslib/css/**"/>
	<mvc:resources location="/jslib/js/" mapping="/jslib/js/**"/>
	<mvc:resources location="/" mapping="/**"/>
	<!-- 當上面要訪問的靜態資源不包括在上面的配置中時,則根據此配置來訪問 -->
    <mvc:default-servlet-handler/>
	
</beans>