Spring+SpringMVC
MVC呢,現在似乎越來越流行使用SpringMVC框架,我自己用的感覺,是非常好,確實很舒服,配置一開始是麻煩了一點點,但是后續的開發真的是很清爽!
SpringMVC配置文件
目錄:resource/config/spring,文件名:spring-mvc.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 7 8 <!--自動掃描控制器--> 9 <context:component-scan base-package="com.magic.rent.controller"/> 10 <!--視圖渲染--> 11 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 12 <property name="prefix" value="/WEB-INF/views/"/> 13 <property name="suffix" value=".jsp"/> 14 </bean> 15 <!--控制器映射器和控制器適配器--> 16 <mvc:annotation-driven/> 17 </beans>
Spring配置文件
目錄:resource/config/spring,文件名:applicationContext-service.xml
額,這個應該屬於Spring的配置文件,噗,之前忘了,就在這里一起補了吧。這個內容暫時比較少,就是Servicec層,以后如果還有什么內容,可以繼續往這里填,比如加入什么工具或框架之類的。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--掃描service--> <context:component-scan base-package="com.magic.rent.service"/> <!--注冊統一異常控制--> <bean id="exception" class="com.magic.rent.exception.exhandler.CustomExceptionHandler"/> </beans>
目錄:resource/config/spring,文件名:applicationContext-transaction.xml
這個主要是用於事務。其中用到了AOP的配置,其他AOP配置可以參照這樣的格式,但是AOP配置是有一點麻煩的,還是需要好好去看看Spring的文檔再配置比較好的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes>
<--這部分,主要是根據方法名稱來進行匹配限定,但是我們是用MyBatis自動生成的Mapper接口,所以在這邊大家要按照MyBatis的命名規范來設置name的值。 <tx:method name="select*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/>
</tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.magic.rent.service.*.*(..))"/> </aop:config> </beans>
講到這里,做一個小總結,其實到此為止,就是一個Spring+SpringMVC+MyBatis的整合,這個網上有很多這種類似的例子,這個也不會多難,到此為止,基本上也是簡單的配置,配置文件其實沒有多少復雜。
Spring統一異常處理
Spring框架自帶了統一異常處理的功能,方法有三個,但是我只介紹一個,因為只有這個,才是生產上大多數的用法。
創建目錄
━java
┗exception(這個類呢,是跟service、controller這些同級的,因為這個類中的Custom可以定義出非常詳細的異常情況。)
┣custom(存放自定義的異常,用於實際業務層進行拋出)
┗exhandler(存放異常處理的控制器)
━webapp
┣WEB-INF
┣admin(管理頁面)
┣error(存放錯誤頁面)
┗views(存放普通頁面)
如圖:
創建異常類控制類
目錄:com.magic.rent.exception.exhandler,文件名:CustomExceptionHandler.java
package com.magic.rent.exception.exhandler; import com.magic.rent.exception.custom.BusinessException; import com.magic.rent.exception.custom.ParameterException; import org.springframework.stereotype.Service; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; public class CustomExceptionHandler implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) { Map<String, Object> model = new HashMap<String, Object>(); model.put("ex", ex); // 根據不同錯誤轉向不同頁面 if (ex instanceof BusinessException) { return new ModelAndView("../error/business_error", model); } else if (ex instanceof ParameterException) { return new ModelAndView("../error/parameter_error", model); } else { return new ModelAndView("../error/404", model); } } }
類中ModelAndView的地址,是“../error/xxx”,這么寫是因為我們在SpringMVC配置“spring-mvc.xml”中的internalResourceViewResolver標簽設置了前綴和后綴,默認前綴是從views文件夾開始訪問頁面,要改成error文件夾,就得寫成這樣子,當然這個類可以統一的再優化一下,比如把“../error/”抽出來統一設置。當然這邊也可以改用Json的方式返回,而不是一定要跳轉界面,改成Json或許更符合需求,畢竟現在多是用Ajax做交互。以后如果有改,我再貼代碼上來。
寫一個自定義異常范例
目錄:com.magic.rent.exception.custom,文件名:BusinessException.java
1 package com.magic.rent.exception.custom; 2 3 public class BusinessException extends RuntimeException { 4 public BusinessException(String message) { 5 super(message); 6 } 7 8 public BusinessException(String message, Throwable cause) { 9 super(message, cause); 10 } 11 }
其實也沒什么內容,就繼承一下,然后復寫一下方法就好了。當然,根據自己的需求,可以自己增加內容。使用的是,也是非常容易,比如下面這個代碼片段。不符合條件,就拋出異常,然后不斷地通過方法向上拋不要try-Catch,最后就會被這個異常控制器捕捉。
//如果查找不到用戶信息,則拋出異常 if (sysUsers == null) { throw new UsernameNotFoundException( "UserDetailsService.userNotFount"); }