SpringMVC简版教程、部分功能

时间:2023-03-09 23:33:42
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.****.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.****.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方法的路径,如果有参数,还要将参数带上。