Spring+SpringMVC


MVC呢,现在似乎越来越流行使用SpringMVC框架,我自己用的感觉,是非常好,确实很舒服,配置一开始是麻烦了一点点,但是后续的开发真的是很清爽!

SpringMVC配置文件


目录:resource/config/spring,文件名:spring-mvc.xml

 <?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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"> <!--自动扫描控制器-->
<context:component-scan base-package="com.magic.rent.controller"/>
<!--视图渲染-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--控制器映射器和控制器适配器-->
<mvc:annotation-driven/>
</beans>

spring-mvc.xml

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

 package com.magic.rent.exception.custom;

 public class BusinessException extends RuntimeException {
public BusinessException(String message) {
super(message);
} public BusinessException(String message, Throwable cause) {
super(message, cause);
}
}

其实也没什么内容,就继承一下,然后复写一下方法就好了。当然,根据自己的需求,可以自己增加内容。使用的是,也是非常容易,比如下面这个代码片段。不符合条件,就抛出异常,然后不断地通过方法向上抛不要try-Catch,最后就会被这个异常控制器捕捉。

 //如果查找不到用户信息,则抛出异常
if (sysUsers == null) {
throw new UsernameNotFoundException(
"UserDetailsService.userNotFount");
}

【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(三)的更多相关文章

  1. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(前言)

    一直希望能够搭建一个完整的,基础Web框架,方便日后接一些外快的时候,能够省时省力,终于花了一周的时间,把这个东西搞定了.特此写下此博客,一来是纪念,二来是希望能够为别人提供方便.顺带说一下,恩,组合 ...

  2. Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(前言)

    简单介绍一下,本框架的基本功能点: Spring:整个框架的主体部分,这个自不用说. SpringMVC:MVC部分我还是比较喜欢Spring的. MyBatis:选型的时候选择这个ORM主要也是考虑 ...

  3. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(五)

    SpringSecurity(2) 好久没有写了,之前只写了一半,我是一边开发一边写Blog一边上班,所以真心没有那么多时间来维护Blog,项目已经开发到编写逻辑及页面部分了,框架基本上已经搭建好不会 ...

  4. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(四)

    SpringSecurity(1) 其实啊,这部分我是最不想写的,因为最麻烦的也是这部分,真的是非常非常的麻烦.关于SpringSecurity的配置,让我折腾了好半天,网上的配置方式一大把,但总有一 ...

  5. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(一)

    Spring+MyBatis 首先要搭建的是Spring+MyBatis的整合框架,毕竟Spring是整个Web框架的核心部位,而数据库操作是一切测试的基础嘛. 目录结构 ━java ┣ contro ...

  6. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(二)

    Log4j 这个东西,大家都熟悉,就简单的介绍一下,算是一个抛砖引玉,因为我自己在Log日志的搭建方面,没有什么经验,但这东西确实是非常重要的,日后调Bug没有它基本不可能,如果有朋友有什么比较好的L ...

  7. 【JavaWeb】SSM+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(六)

    Showings 我个人的项目,当前不断地在更新. 我希望做成一个好项目,同时,也是在锻炼自己的技术. 在项目中发现问题,学习知识,是比较可取的一条路子. 这样学习到的知识,虽然分散,但是都很实用,而 ...

  8. Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建

    1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...

  9. javaweb项目-医者天下 (Spring+SpringMVC+MyBatis)

    项目下载地址:http://download.csdn.net/detail/qq_33599520/9826683 项目完整结构图: 项目简介: 医者天下项目是一个基于Spring+SpringMV ...

随机推荐

  1. CSS选择器的权重与优先规则?

    我们做项目的时候,经常遇到样式层叠问题,被其他的样式覆盖,或者写的权重不高没效果,对权重没有具体的分析,做了一个总结. css继承是从一个元素向其后代元素传递属性值所采用的机制.确定应当向一个元素应用 ...

  2. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  3. WWDC2016 观后杂感

    WWDC2016已经落幕了,我没有熬夜看看的录播. 总的来说觉得还是比较兴奋的,因为苹果将更多的APi开发出来了,可以玩出更多花样了.

  4. [转]ASP.NET应用程序生命周期趣谈(五) IIS7瞎说

    Ps:建议初学者在阅读本文之前,先简要了解一下之前的几篇文章,以便于熟悉本文提到的一些关于IIS6的内容,方便理解.仅供参考. PS:为什么叫瞎说呢?我觉得自己理解的并不到位,只能是作为一个传声筒,希 ...

  5. Android View的绘制流程

    写得太好了,本来还想自己写的,奈何肚里墨水有限,直接转吧.正所谓前人种树,后人乘凉.. View的绘制和事件处理是两个重要的主题,上一篇<图解 Android事件分发机制>已经把事件的分发 ...

  6. STSDB、NDataBase 对象数据库在不同.net framework下无法读取的解决办法

    STSDB.NDataBase 等对象数据库将对象保存在文件中后,如果在不同的windows平台.不同的.net frameWork下总是无法读取,原因是对象模式已经不同了. 解决的办法也很简单,就是 ...

  7. 好玩的Handler

    Android提供了Handler和Looper来满足线程间的通信; Handler和Activity的任务栈不同,它是先进先出原则; Handler:你可以构造Handler对象来与Looper沟通 ...

  8. 转载文章(Redis中对key的操作)

    转载地址:http://www.cnblogs.com/stephen-liu74/archive/2012/03/26/2356951.html 一.概述: 在该系列的前几篇博客中,主要讲述的是与R ...

  9. mysql插入多条数据时间复杂度比较

    SQL脚本 select * from users; 方式一: insert into users(name, age, course_id) VALUES("test",1, & ...

  10. vim 学习积累(一)

    首先是简单的认识了三种状态(大家公认的说法是模式),分别是:插入,视图,和一般. 进入vim之后默认的是一般模式,这时直接使用'a', 'i', 'o'(也就是进入vim之后直接按下a/i/o键均可进 ...