SpringMVC简版教程、部分功能
注:本文只用注解来实现
前言
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/311731、http://www.cnblogs.com/wangtj-19/p/5821725.html、http://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简版教程、部分功能的更多相关文章
- gerrit简版教程
设置public key 1.生成密钥:ssh-keygen -t rsa -C "xiaoming" 2.查看是否已经有了ssh密钥:cd ~/.ssh 3.不知道为什么hook ...
- [C#HttpHelper]类1.4正式版教程与升级报告
[C#HttpHelper]类1.4正式版教程与升级报告 导读 1.升级报告 2.HttpHelper1.4正式版下载 3.HttpHelper类使用方法, 4.最简单的Post与Get的写法 ...
- OD调试6—使未注册版软件的功能得以实现
OD调试6—使未注册版软件的功能得以实现 本节使用的软件下载链接 (想动手试验的朋友可以下载来试试) 继续开始我OD调试教程的学习笔记. 本次试验对真正的程序进行逆向.(之前的都是为破解而专门设计的小 ...
- python练习_购物车(简版)
python练习_购物车(简版) 需求: 写一个python购物车可以输入用户初始化金额 可以打印商品,且用户输入编号,即可购买商品 购物时计算用户余额,是否可以购买物品 退出结算时打印购物小票 以下 ...
- Virtex6 PCIe 超简版基础概念学习(二)
Virtex6 PCIe 超简版基础概念学习(二) 分类:FPGAPCIe (2081) (0) 举报 收藏 文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 ise14.7 ...
- [原创]spring及springmvc精简版--AOP
接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...
- SpringBoot2+Netty打造通俗简版RPC通信框架(升级版)
背景 上篇文章我简单的介绍了自己打造的通俗简版RPC通信框架,这篇是对简版的增强~ 如果大家对此项目还感兴趣的话,可到码云上瞄瞄:Netty-RPC 上 ...
- SpringBoot2+Netty打造通俗简版RPC通信框架
2019-07-19:完成基本RPC通信! 2019-07-22:优化此框架,实现单一长连接! 2019-07-24:继续优化此框架:1.增加服务提供注解(带版本号),然后利用Spring框架的在启动 ...
- 简版在线聊天Websocket
序言 What is Webscoket ? websocket 应用场景 简版群聊实现 代码例子 小结 Webscoket Websokcet 是一种单个TCP连接上进行全双工通信的协议,通过HTT ...
随机推荐
- 小偷网站工具--Teleport Ultra
可以克隆别人网站的工具 http://jingyan.baidu.com/article/219f4bf7dce58bde442d3836.html
- STM32-USB详细使用说明(转)
源:STM32-USB详细使用说明 附件HID的双向通信 亮点STM32开发板充实了USBHID数据发送和接收例程(STM32固件库3.5 USB库3.4)
- PHP的高并发和大数据处理
收集前人的经验.加速学习,解决工作中的难题. 一.代码优化(包括sql语句的优化), 合理的使用索引,避免整表查询.二.日常海量数据处理我用文件缓存,文件缓存分两种,第一种是最常见的生成html静太文 ...
- 在Eclipse中配置tomcat
为了在Eclipse中进行struts2的测试,才发现自己机器上的Eclipse没有集成Tomcat,在网上找了半天,不是这个插件没有下载地址,就是那个有好多注意事项或者版本问题. 结果,自己到tom ...
- BZOJ2318: Spoj4060 game with probability Problem
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...
- c# post 数据的方法
网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser.WebClient.HttpWebRequest这三 ...
- python dataframe 针对多列执行map操作
Suppose I have a df which has columns of 'ID', 'col_1', 'col_2'. And I define a function : f = lambd ...
- Jquery Validate 动态添加校验
<cx:script> <script type="text/javascript"> //修改表单验证,提交 $(document).ready(func ...
- Linux环境下的GCC编译器与GDB调试工具介绍
假如现在我们有如下代码需要编译运行和调试.文件名为:test.c #include <stdio.h> int main() { int day, month, year, sum, le ...
- Violin 琴弦介绍
共四根弦,从粗到细: 第四弦:音名:G:唱名:Sol 第三弦:音名:D:唱名:Re 第二弦:音名:A:唱名:La 第一弦:音名:E:唱名:Mi