注:本文只用注解来实现

前言

SpringMVC各种流程图流程图(其他的各种流程图)

jsp、xml、action彼此之间的关系,都如何使用

spring-mvc.xml如何配置,放在哪里?

action中如何转发和重定向

action如何跳转到jsp

如何处理ajax

如何给action做单元测试

前言

SpringMVC各种流程图流程图(其他的各种流程图)

网络上有各种流程图的画法,例如:

http://howtodoinjava.com/spring/spring-mvc/spring-mvc-hello-world-example/中的

[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1033" type="#_x0000_t75" style='width:282pt; height:223pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image001.png" o:title=""/> </v:shape><![endif][if !vml][endif]

[if gte vml 1]><v:shape id="_x0000_i1032" type="#_x0000_t75" style='width:415pt;height:318pt; visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image003.png" o:title=""/> </v:shape><![endif][if !vml][endif]

http://www.cnblogs.com/zbf1214/p/5265117.html中的

[if gte vml 1]><v:shape id="_x0000_i1031" type="#_x0000_t75" style='width:415pt;height:236pt; visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image005.png" o:title=""/> </v:shape><![endif][if !vml][endif]

还有http://elf8848.iteye.com/blog/875830/中的[if gte vml 1]><v:shape id="_x56fe__x7247__x0020_5" o:spid="_x0000_i1030" type="#_x0000_t75" alt="说明: Macintosh HD:Users:QQMacAir:Downloads:620f63e1-ee68-30c9-a53d-13107e634364.png" style='width:415pt;height:601pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image007.png" o:title="620f63e1-ee68-30c9-a53d-13107e634364.png"/> </v:shape><![endif][if !vml][endif]

怎么说呢,这类流程图很多,但是核心都是一样的。

简单描述就是:前端发起请求,springMVC的核心类DispatcherServlet拦截,然后跳转到controller中,执行完成之后根据View控制器跳转到前端。

jsp、xml、action彼此之间的关系,都如何使用

jsp通过url,例如”user/getUser”请求后台的action/Controller,servlet拦截url,然后扫描所有的action及action中的方法上的注解是否有匹配的,一旦有匹配的,就执行该方法。如果没有匹配的怎么办?action中的方法执行完了,return一个字符串,servlet启动视图控制器拦截,匹配到对应的页面。

spring-mvc.xml如何配置,放在哪里?

前面的流程图,jsp、xml和action之间的关系,全部要依靠xml文件的配置

如何配置:

[if !supportLists]1、         [endif]web.xml配置DispatcherServlet

[if !supportLists]2、         [endif]spring-mvc.xml配置action/controller扫描位置

[if !supportLists]3、         [endif]spring-mvc.xml配置view控制器。

以上的配置写在了另一篇文章中:

放在那里?放哪里都没关系,只要web.xml中配置好servlet默认初始化扫描的xml位置即可

以上配置完了就行了吗?不行,既然用注解还需要注意以下几点:

jsp如何写url:例如“user/showUser”,user表示哪一个action类,“showUser”表示action类中哪一个方法。

action类、action方法上如何写注解:

action类名上一行写

@Controller

@RequestMapping("/user")

[if gte vml 1]><v:shape id="_x0000_i1029" type="#_x0000_t75" style='width:243pt;height:56pt; visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image009.png" o:title=""/> </v:shape><![endif][if !vml][endif]

action方法名上一行写

@RequestMapping("/showUser")

action如何跳转到jsp

记得spring-mvc.xml文件中写如下:

<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->

<property name="prefix" value="/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

每个action方法最后一行写return “页面名”,如return “success”,表示的就是跳转到success.jsp页面。

action中如何使用service

action方法中如果只是简单逻辑还好,但是我们经常要操作数据库,那么就需要调用service类,service类就要调用DAO类,DAO要操作数据库。

在action方法中,如下:

[if gte vml 1]><v:shape id="_x56fe__x7247__x0020_2" o:spid="_x0000_i1028" type="#_x0000_t75" style='width:415pt; height:142pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image011.png" o:title=""/> </v:shape><![endif][if !vml][endif]

简单描述,就是使用@Resource注入service。

IUserService是接口名,userService是具体的实现类的注解名,在IUserService接口的实现类上一行,会写上@Service(“userService”),例如

[if gte vml 1]><v:shape id="_x56fe__x7247__x0020_3" o:spid="_x0000_i1027" type="#_x0000_t75" style='width:404pt; height:41pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image013.png" o:title=""/> </v:shape><![endif][if !vml][endif]

service如何写

[if gte vml 1]><v:shape id="_x56fe__x7247__x0020_4" o:spid="_x0000_i1026" type="#_x0000_t75" style='width:415pt; height:324pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image015.png" o:title=""/> </v:shape><![endif][if !vml][endif]

基本没什么特殊,也是@Resource引入DAO接口,这里注意,userDao随意命名,因为我用的是mybatis,只需要一个IDAO接口,不需要实现类,mybatis的映射文件就相当于一个实现类了

action中如何转发和重定向

需求1:action方法执行完了,想要转发(上下文都带着)到另一个action方法

跳转到页面我么知道了,那么

return "forward:/question/getQuestion";

需求2:重定向到另一个action中:

return "redirect:/question/getQuestion";

需求3:防止表单重复提交,同需求2

如何处理ajax

http://blog.csdn.net/yangtang_newton/article/details/7525800

http://www.cnblogs.com/tingbogiu/p/5796199.html

http://elf8848.iteye.com/blog/875830/十五章节

简单说,基本就是后端用response的io流传递json到前端,不管你是string、list还是map都得想办法转换成json。Springmvc提供了中比较好的方式就是springmvc内置的json转换方式。建议采用。

如何给action做单元测试

https://my.oschina.net/u/142412/blog/311731http://www.cnblogs.com/wangtj-19/p/5821725.htmlhttp://blog.csdn.net/x1066988452/article/details/53519307

http://zhaozhiming.github.io/blog/2014/06/16/spring-mvc-unit-test-part-1/

已经写的很详细了

1、方法基于junit、springmvc和spring-test

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

2、@RunWith注解指定使用springJunit的测试运行器,

@ContextConfiguration注解指定测试用的spring配置文件的位置

[if !supportLists]4、          [endif]可以写一个BaseJunitTest,可以将

[if gte vml 1]><v:shape id="_x56fe__x7247__x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75" style='width:415pt;height:37pt; visibility:visible;mso-wrap-style:square'> <v:imagedata src="SpringMVC%E6%95%B4%E7%90%86.files/image017.png" o:title=""/> </v:shape><![endif][if !vml][endif]

写在baseJunitTest中

[if !supportLists]5、         [endif]this.mockMvc.perform(post("/user/showUser").param("name", "zml").param("password", "123456").param("age","100"));是需要指定访问action方法的路径,如果有参数,还要将参数带上。

SpringMVC简版教程、部分功能的更多相关文章

  1. gerrit简版教程

    设置public key 1.生成密钥:ssh-keygen -t rsa -C "xiaoming" 2.查看是否已经有了ssh密钥:cd ~/.ssh 3.不知道为什么hook ...

  2. [C#HttpHelper]类1.4正式版教程与升级报告

       [C#HttpHelper]类1.4正式版教程与升级报告 导读 1.升级报告 2.HttpHelper1.4正式版下载 3.HttpHelper类使用方法, 4.最简单的Post与Get的写法 ...

  3. OD调试6—使未注册版软件的功能得以实现

    OD调试6—使未注册版软件的功能得以实现 本节使用的软件下载链接 (想动手试验的朋友可以下载来试试) 继续开始我OD调试教程的学习笔记. 本次试验对真正的程序进行逆向.(之前的都是为破解而专门设计的小 ...

  4. python练习_购物车(简版)

    python练习_购物车(简版) 需求: 写一个python购物车可以输入用户初始化金额 可以打印商品,且用户输入编号,即可购买商品 购物时计算用户余额,是否可以购买物品 退出结算时打印购物小票 以下 ...

  5. Virtex6 PCIe 超简版基础概念学习(二)

    Virtex6 PCIe 超简版基础概念学习(二) 分类:FPGAPCIe (2081)  (0)  举报  收藏 文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 ise14.7 ...

  6. [原创]spring及springmvc精简版--AOP

    接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...

  7. SpringBoot2+Netty打造通俗简版RPC通信框架(升级版)

    背景         上篇文章我简单的介绍了自己打造的通俗简版RPC通信框架,这篇是对简版的增强~         如果大家对此项目还感兴趣的话,可到码云上瞄瞄:Netty-RPC         上 ...

  8. SpringBoot2+Netty打造通俗简版RPC通信框架

    2019-07-19:完成基本RPC通信! 2019-07-22:优化此框架,实现单一长连接! 2019-07-24:继续优化此框架:1.增加服务提供注解(带版本号),然后利用Spring框架的在启动 ...

  9. 简版在线聊天Websocket

    序言 What is Webscoket ? websocket 应用场景 简版群聊实现 代码例子 小结 Webscoket Websokcet 是一种单个TCP连接上进行全双工通信的协议,通过HTT ...

随机推荐

  1. Masonry布局框架的使用

    Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性.比我们使用自动布局,繁琐的约束条件,好用多了.下面我们来学学masonry的使用方 ...

  2. 微信小程序之----加载中提示框loading

    loading loading只有一个属性hidden .wxml <view> <loading hidden="{{hidden}}"> 加载中... ...

  3. html 上传文件

    1.html代码 <form id="form1" action="TestYield" method="post" enctype= ...

  4. Heka GeoIpDecoder 配置

    Prepare: 安装geoip-api-c,确保/usr/include/GeoIP.h存在: 源码编译安装Heka (容易出现问题): 下载GeoLiteCity.dat数据库. 配置文件举例: ...

  5. HttpListener 实现web服务端

    1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  6. chkdsk

    通过 Microsoft 的相关帮助就可以明白,例如对D盘进行操作,则: 示例1:chkdsk /? 显示帮助信息. 示例2:chkdsk d: 检查D盘的磁盘状态,报告磁盘错误. 示例3:chkds ...

  7. Core Data需求

    大家都在讨论怎么使用Core Data,但是什么时候用到Core Data,这好像是大家很少讨论的问题 我们使用Core Data ,主要用来存储两种类型的数据:固定的数据,和 可能变化的数据. 1. ...

  8. YII 1.0 分页类

    在控制器中 方法1 $criteria = new CDbCriteria();//AR的另一种写法 $model = Article::model(); $total = $model->co ...

  9. 详细解析Linux scp命令的应用

    详细解析Linux scp命令的应用 Linux命令有人统计说是有4000多个,Linux scp命令是用于Linux之间复制文件和目录,这里详细介绍scp命令使用和参数. AD: Linux scp ...

  10. HDU-1995-汉诺塔V

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1995 这题不知道该说水还是不水,对于这题我看到题目数据,就有了想法,因为题目数据给的好 所以我直接 假 ...