SpringMVC第二天笔记.docx

上传人:b****8 文档编号:10880623 上传时间:2023-02-23 格式:DOCX 页数:34 大小:448.37KB
下载 相关 举报
SpringMVC第二天笔记.docx_第1页
第1页 / 共34页
SpringMVC第二天笔记.docx_第2页
第2页 / 共34页
SpringMVC第二天笔记.docx_第3页
第3页 / 共34页
SpringMVC第二天笔记.docx_第4页
第4页 / 共34页
SpringMVC第二天笔记.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

SpringMVC第二天笔记.docx

《SpringMVC第二天笔记.docx》由会员分享,可在线阅读,更多相关《SpringMVC第二天笔记.docx(34页珍藏版)》请在冰豆网上搜索。

SpringMVC第二天笔记.docx

SpringMVC第二天笔记

SpringMVC第二天

昨天的内容

一、什么是springmvc?

一个类似struts2的前端web框架。

二、Springmvc的入门程序

1、实现controller接口

2、实用性HttpRequestHandler接口

3、使用注解形式开发controller

a)在Controller上加上@Controller注解

b)方法上加上@RequestMapping注解

c)需要在springmvc的配置文件中添加一个包扫描器。

三、Springmvc的框架结构

请求先到前端控制器DispatcherServlet,到处理器映射器中找url对应的handler,返回HandlerExecutionChain,执行链中包含拦截器和handler。

执行handler使用处理器适配器执行,执行完handler返回ModelAndView对象,需要视图解析器将逻辑视图转换成物理视图,渲染视图。

4、springmvc整合mybatis

整合的思路是spring容器关联mapper对象、server、Controller。

事务配置在service层由spring容器关联

5、参数映射

a)简单数据类型,需要页面中input的name属性和方法的形参名称一致。

不一致,使用@Requestparam注解转换

b)Pojo类型name属性和pojo中的属性名称一致。

c)默认支持的参数类型

i.HttpServletRequest

ii.httpservletResponse

iii.HttpSession

iv.Model/ModelMap

d)自定义参数绑定

1、实现converter接口,需要泛型一个是Source、Target

2、配置到springmvc.xml中

3、配置的converterService配置到Annotation-driven标签中。

课程计划

1、参数绑定的高级应用

a)绑定包装的pojo类型

b)集合类型

i.List

ii.数组

iii.Map

2、Handler的返回值

3、有效性验证validation(了解)

4、参数回显(掌握)

5、异常处理,架构级别的异常处理(了解)

6、上传图片(掌握)

7、Json数据交换(掌握)

8、Restful风格的实现,url模板映射(了解)

9、拦截器(掌握)

参数绑定的高级应用

包装pojo类型的绑定

多条件查询时就会用到包装类型的pojo

创建一个queryVo对象,包含Items对象。

实现步骤

创建一个QueryVo

publicclassQueryVo{

privateItemsitems;

publicItemsgetItems(){

returnitems;

}

publicvoidsetItems(Itemsitems){

this.items=items;

}

}

修改Controller

在itemsList方法中添加一个QueryVo参数。

查询省略。

修改jsp

集合类型的参数绑定

当批量删除是需要页面向后台传递一个id数组类型。

绑定数组类型

修改queryVo

publicclassQueryVo{

privateItemsitems;

privateInteger[]ids;

publicInteger[]getIds(){

returnids;

}

publicvoidsetIds(Integer[]ids){

this.ids=ids;

}

publicItemsgetItems(){

returnitems;

}

publicvoidsetItems(Itemsitems){

this.items=items;

}

}

修改jsp页面

绑定List

修改QueryVo

添加一个Items列表,目的是让页面传递过来一个商品列表,就可以批量更新商品信息。

修改jsp

forEachitems="${itemsList}"var="items"varStatus="s">

value="

formatDatevalue="${items.createtime}"pattern="yyyy-MM-ddHH:

mm:

ss"/>"/>

id=${items.id}">修改

forEach>

实现效果

绑定Map类型(自己实现)

在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。

包装类中定义Map对象如下:

PublicclassQueryVo{

privateMapitemInfo=newHashMap();

//get/set方法..

}

页面定义如下:

学生信息:

姓名:

年龄:

......

Contrller方法定义如下:

publicStringuseraddsubmit(Modelmodel,QueryVoqueryVo)throwsException{

System.out.println(queryVo.getStudentinfo());

}

Handler的返回值

一个handler就是一个方法,对应一个url。

ModelAndView

返回模型数据和视图名称。

返回String类型

字符串代表的就是视图的名称,也就是逻辑视图。

返回String类型中重定向redirect

商品修改完毕后跳转到商品列表。

@RequestMapping("/editItemsSubmit")

publicStringeditItemsSubmit(Itemsitems)throwsException{

//Itemsitems=newItems();

//items.setId(id);

//items.setName(name);

//items.setCreatetime(createtime);

//items.setDetail(detail);

//items.setPrice(price);

//更新数据库

itemsService.updateItmes(items);

//return"success";

//商品更新完毕后跳转到商品列表页面,使用redirect

return"redirect:

/itemsList.action";

}

返回String类型中重定向forward

@RequestMapping("/editItemsSubmit")

publicStringeditItemsSubmit(Itemsitems)throwsException{

//Itemsitems=newItems();

//items.setId(id);

//items.setName(name);

//items.setCreatetime(createtime);

//items.setDetail(detail);

//items.setPrice(price);

//更新数据库

itemsService.updateItmes(items);

//return"success";

//商品更新完毕后跳转到商品列表页面,使用redirect

//return"redirect:

/itemsList.action";

//forward跳转是共享request,当前request中包含id属性,跳转到editItems处理器中同样request中包含id属性。

//方法是forwardj+:

+要跳转的url

return"forward:

/editItems.action";

}

返回void

@RequestMapping("/itemsList2")

//返void

publicvoiditemsList2(QueryVoqueryVo,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{

//查询商品列表

ListitemsList=itemsService.queryItemsList();

//将结果返回到页面

request.setAttribute("itemsList",itemsList);

//跳转到页面

//使用request时必须是jsp的全路径

request.getRequestDispatcher("/WEB-INF/jsp/itemsList.jsp").forward(request,response);

}

返回json数据或者是xml数据

//返void

publicvoiditemsList2(QueryVoqueryVo,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{

//查询商品列表

ListitemsList=itemsService.queryItemsList();

//将结果返回到页面

request.setAttribute("itemsList",itemsList);

//跳转到页面

//使用request时必须是jsp的全路径

//request.getRequestDispatcher("/WEB-INF/jsp/itemsList.jsp").forward(request,response);

//返回json数据

response.setCharacterEncoding("utf-8");

response.setContentType("application/xml;charset=utf-8");

Stringxml="

xmlversion=\"1.0\"encoding=\"UTF-8\"?

>\n"+

"

xsi=\"http:

//www.w3.org/2001/XMLSchema-instance\"\n"+

"xmlns=\"xmlns:

web=\"+

"xsi:

schemaLocation=\"+

"id=\"WebApp_ID\"version=\"2.5\">\n"+

"springmvc_mybatis0429\n"+

"\n"+

"index.html\n"+

"index.htm\n"+

"index.jsp\n"+

"default.html\n"+

"default.htm\n"+

"default.jsp\n"+

"\n"+

"

--spring容器配置-->\n"+

"

--加载spring容器-->\n"+

"\n"+

"contextConfigLocation\n"+

"classpath:

spring/ApplicationContext-*.xml\n"+

"\n"+

"\n"+

"org.springframework.web.context.ContextLoaderListener\n"+

"\n"+

"

--springmvc的前端控制器-->\n"+

"\n"+

"springmvc\n"+

"org.springframework.web.servlet.DispatcherServlet\n"+

"

--contextConfigLocation不是必须的,如果不配置contextConfigLocation,springmvc的配置文件默认在:

WEB-INF/servlet的name+\"-servlet.xml\"-->\n"+

"\n"+

"contextConfigLocation\n"+

"classpath:

spring/springmvc.xml\n"+

"\n"+

"1\n"+

"\n"+

"\n"+

"springmvc\n"+

"*.action\n"+

"\n"+

"

--解决post乱码-->\n"+

"\n"+

"CharacterEncodingFilter\n"+

"org.springframework.web.filter.CharacterEncodingFilter\n"+

"\n"+

"encoding\n"+

"utf-8\n"+

"\n"+

"\n"+

"\n"+

"CharacterEncodingFilter\n"+

"/*\n"+

"\n"+

"";

response.getWriter().write(xml);

}

@RequestMapping注解

1、配置url到handler的映射关系

2、窄化请求映射

如果url中不加任何修饰是就是一个相对路径,相对当前路径来说。

限定请求方法

@RequestMapping(value="/editItemsSubmit",method=RequestMethod.POST)

Springmvc和struts2的区别

1、前端控制器springmvc是DispatcherServlet。

Struts2是Filter。

2、Springmvc处理请求是使用使用方法来处理,接收页面传递过来的参数是使用方法的形参。

Struts2接收页面参数使用类级成员变量就是类的属性。

如果是使用属性接收参数是线程不安全的,struts2配置时需要配置成多例。

Springmvc使用方法的形参接收参数。

是线程安全的,springmvc中controller都是单例的。

3、返回值传递方法,springmvc是使用request传递参数。

Struts2使用值栈。

有效性验证validation(了解)

一般使用js校验。

不太安全,如果安全性要求高的话需要使用后台校验。

原理

Springmvc实现了jsr-303标准,是oracle公司出的标准。

但是springmvc中并没有实现,想使用校验需要加上实现类。

Hibernate提供了以实现的校验器。

和hibernate框架无任何关系。

需要加入hibernate校验器的jar包。

需求

修改商品时,对商品的有效性进行验证,验证商品的名称字符必须是大于1小于30,商品的价格必须输入。

实现步骤

第一步:

配置validator。

在springmvc.xml中配置。

然后再添加到annotation-driven节点中。

第二步:

编写校验规则。

需要添加到pojo类中

第三步:

接收错误消息。

需要在controller中接收。

1、需要在pojo参数前加@Validator注解

2、Pojo参数后添加一个参数BindingResult参数。

一个pojo后紧跟一个BindingResult

第四步:

需要把错误消息添加到request域中传递到页面

第五步:

在页面显示错误消息。

配置validator

--配置validator-->

--校验器-->

class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">

--校验器-->

--指定校验使用的资源文件,如果不指定则默认使用classpath下的ValidationMessages.properties-->

--校验错误信息配置文件-->

class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

--资源文件名-->

classpath:

CustomValidationMessages

--资源文件编码格式-->

--对资源文件内容缓存时间,单位秒-->

--注解驱动,配置了这个节点就相当于配置了注解形式的处理器映射器和适配器-->

annotation-drivenconversion-service="conversionService"validator="validator"/>

CustomValidationMessages.properties

items.name.size.error=商品名称的长度必须大于1小于30

items.price.isnull=商品价格不能为空

添加校验规则

Controller添加校验

@RequestMappin

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 中考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1