springmvc经典教程(3)

2023-01-31 01:01:46 教程 经典 SpringMvc

springMVC史上最好教程(2)

springmvc史上最好教程(1)

1.1 需求

使用springmvc+mybatis架构实现商品信息维护。

 

 

1.2 商品修改

1.2.1 dao

使用逆向工程自动生成的代码:

ItemsMapper.Java

ItemsMapper.xml

 

1.2.2 service

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //根据id查询商品信息  

  2.   

  3. public Items findItemById(int id) throws Exception;  

  4.   

  5. //修改商品信息  

  6.   

  7. public void saveItem(Items items)throws Exception;  



 

1.2.3 controller

修改商品信息显示页面:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. @RequestMapping(value="/editItem")  

  2.   

  3. public String editItem(Model model, Integer id) throws Exception{  

  4.   

  5. //调用service查询商品信息  

  6.   

  7. Items item = itemService.findItemById(id);  

  8.   

  9. model.addAttribute("item", item);  

  10.   

  11. return "item/editItem";  

  12.   

  13. }  



 

修改商品信息提交:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //商品修改提交  

  2.   

  3. @RequestMapping("/editItemSubmit")  

  4.   

  5. public String editItemSubmit(Items items)throws Exception{  

  6.   

  7. System.out.println(items);  

  8.   

  9.    

  10.   

  11. itemService.saveItem(items);  

  12.   

  13. return "success";  

  14.   

  15. }  



 

 

1.2.4 页面

/WEB-INF/jsp/item/itemsList.jsp

/WEB-INF/jsp/item/editItem.jsp

 

 

1.3 @RequestMapping

 

通过RequestMapping注解可以定义不同的处理器映射规则。


1.3.1 URL路径映射

@RequestMapping(value="/item")或@RequestMapping("/item)

value的值是数组,可以将多个url映射到同一个方法

 

1.3.2 窄化请求映射

在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。

 

如下:

@RequestMapping放在类名上边,设置请求前缀 

@Controller

@RequestMapping("/item")

 

方法名上边设置请求映射url:

@RequestMapping放在方法名上边,如下:

@RequestMapping("/queryItem ")


访问地址为:/item/queryItem

 

 

1.3.3 请求方法限定

1、限定GET方法

@RequestMapping(method = RequestMethod.GET)

 

如果通过Post访问则报错:

Http Status 405 - Request method 'POST' not supported

 

例如:

@RequestMapping(value="/editItem",method=RequestMethod.GET)

2、 限定POST方法

 

@RequestMapping(method = RequestMethod.POST)

 

如果通过Post访问则报错:

HTTP Status 405 - Request method 'GET' not supported

 

3、GET和POST都可以

@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})

 

 

1.4 controller方法返回值

1.4.1 返回ModelAndView

controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。

 

1.4.2 返回void

在controller方法形参上可以定义request和response,使用request或response指定响应结果:

使用request转向页面,如下:

request.getRequestDispatcher("页面路径").forward(request, response);

通过response页面重定向:

response.sendRedirect("url")

通过response指定响应结果,例如响应JSON数据如下:

response.setCharacterEncoding("utf-8");

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

response.getWriter().write("json串");

 

 

1.4.3 返回字符串

1.4.3.1 逻辑视图名

 

controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/item/editItem.jsp  

  2.   

  3. return "item/editItem";  



 

1.4.3.2 Redirect重定向

Contrller方法返回结果重定向到一个url地址,如下商品修改提交后重定向到商品查询方法,参数无法带到商品查询方法中。

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //重定向到queryItem.action地址,request无法带过去  

  2. return "redirect:queryItem.action";  



 

redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。

由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem.action后边加参数,如下:

/item/queryItem?...&…..

 

 

1.4.3.3 forward转发

controller方法执行后继续执行另一个controller方法,如下商品修改提交后转向到商品修改页面,修改商品的id参数可以带到商品修改方法中。

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //结果转发到editItem.action,request可以带过去  

  2.   

  3. return "forward:editItem.action";  



 

forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。

 

 

 

 

 

1.5 参数绑定

 

1.5.1 默认支持的参数类型

处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。

1.5.1.1 HttpServletRequest

通过request对象获取请求信息

1.5.1.2 HttpServletResponse

通过response处理响应信息

1.5.1.3 HttpSession

通过session对象得到session中存放的对象

1.5.1.4 Model

通过model向页面传递数据,如下:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //调用service查询商品信息  

  2. Items item = itemService.findItemById(id);  

  3. model.addAttribute("item", item);  



 

页面通过${item.XXXX}获取item对象的属性值。

 

 

1.5.2 参数绑定介绍

 

注解适配器对RequestMapping标记的方法进行适配,对方法中的形参会进行参数绑定,早期springmvc采用PropertyEditor(属性编辑器)进行参数绑定将request请求的参数绑定到方法形参上,3.X之后springmvc就开始使用Converter进行参数绑定。

 

1.5.3 @RequestParam

@RequestParam用于绑定单个请求参数。


value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报;

TTP Status 400 - Required Integer parameter 'XXXX' is not present

 

defaultValue:默认值,表示如果请求中没有同名参数时的默认值

 

定义如下:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public String editItem(@RequestParam(value="item_id",required=true) String id) {  

  2.   

  3. }  



 

形参名称为id,但是这里使用value=" item_id"限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。

注意:如果请求参数中没有item_id将跑出异常:

HTTP Status 500 - Required Integer parameter 'item_id' is not present

 

这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值

 

1.5.4 简单类型

当请求的参数名称和处理器形参名称一致时会将请求参数与形参进行绑定。

1.5.4.1 整型

public String editItem(Model model,Integer id) throws Exception{

1.5.4.2 字符串

例子略

 

1.5.4.3 单精度/双精度

例子略

 

1.5.4.4 布尔型

处理器方法:

public String editItem(Model model,Integer id,Boolean status) throws Exception

 

请求url:

http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false

 

说明:对于布尔类型的参数,请求的参数值为true或false。

 

 

1.5.5 自定义参数绑定

1.5.5.1 需求

根据业务需求自定义日期格式进行参数绑定。

 

1.5.5.2 propertyEditor

1.5.5.2.1 使用WebDataBinder 

在controller方法中通过@InitBinder标识方法为参数绑定方法,通过WebDataBinder注册属性编辑器,问题是此方法只能在单个controller类中注册。

 

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1.   

  2.   

  3. @InitBinder  

  4.   

  5. public void initBinder(WebDataBinder binder) throws Exception {  

  6.   

  7. binder.reGISterCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFORMat("yyyy-MM-dd"),true));  

  8.   

  9. }  



 

 

1.5.5.2.2 使用WebDataBinder 

在controller方法中通过@InitBinder标识方法为参数绑定方法,通过WebDataBinder注册属性编辑器,问题是此方法只能在单个controller类中注册。


 

如下:

编写CustomPropertyEditor:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public class CustomPropertyEditor implements PropertyEditorRegistrar {  

  2.   

  3.    

  4.   

  5. @Override  

  6.   

  7. public void registerCustomEditors(PropertyEditorRegistry registry) {  

  8.   

  9. registry.registerCustomEditor(Date.class, new CustomDateEditor(new  

  10.   

  11.  SimpleDateFormat("yyyy-MM-dd HH-mm-ss"),true));  

  12.   

  13. }  

  14.   

  15.    

  16.   

  17. }  



 

配置如下:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <!-- 注册属性编辑器 -->  

  2.   

  3. <bean id="customPropertyEditor" class="com.sihai.SSM.propertyeditor.CustomPropertyEditor"></bean>   

  4.   

  5. <!-- 自定义webBinder -->  

  6.   

  7. <bean id="customBinder"  

  8.   

  9. class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  

  10.   

  11. <property name="propertyEditorRegistrars">  

  12.   

  13. <list>  

  14.   

  15. <ref bean="customPropertyEditor"/>  

  16.   

  17. </list>  

  18.   

  19. </property>  

  20.   

  21. </bean>  

  22.   

  23.    

  24.   

  25. <!--注解适配器 -->  

  26.   

  27. <bean  

  28.   

  29. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  

  30.   

  31.  <property name="webBindingInitializer" ref="customBinder"></property>   

  32.   

  33. </bean>  

  34.   

  35.    

  36.   

  37.    



 

1.5.5.3 Converter

1.5.5.3.1 自定义Converter

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public class CustomDateConverter implements Converter<String, Date> {  

  2.   

  3.    

  4.   

  5. @Override  

  6.   

  7. public Date convert(String source) {  

  8.   

  9. try {  

  10.   

  11. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");  

  12.   

  13. return simpleDateFormat.parse(source);  

  14.   

  15. } catch (Exception e) {  

  16.   

  17. e.printStackTrace();  

  18.   

  19. }  

  20.   

  21. return null;  

  22.   

  23. }  

  24.   

  25.    

  26.   

  27. }  

  28.   

  29.    



1.5.5.3.2 配置方式1

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <!--注解适配器 -->  

  2.   

  3. <bean  

  4.   

  5. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  

  6.   

  7.  <property name="webBindingInitializer" ref="customBinder"></property>   

  8.   

  9. </bean>  

  10.   

  11. <!-- 自定义webBinder -->  

  12.   

  13. <bean id="customBinder"  

  14.   

  15. class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  

  16.   

  17. <property name="conversionService" ref="conversionService" />  

  18.   

  19. </bean>  

  20.   

  21. <!-- conversionService -->  

  22.   

  23. <bean id="conversionService"  

  24.   

  25. class="org.springframework.format.support.FormattinGConversionServiceFactoryBean">  

  26.   

  27. <!-- 转换器 -->  

  28.   

  29. <property name="converters">  

  30.   

  31. <list>  

  32.   

  33. <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>  

  34.   

  35. </list>  

  36.   

  37. </property>  

  38.   

  39. </bean>  



 

1.5.5.3.3 配置方式2

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <mvc:annotation-driven conversion-service="conversionService">  

  2.   

  3. </mvc:annotation-driven>  

  4.   

  5. <!-- conversionService -->  

  6.   

  7. <bean id="conversionService"  

  8.   

  9. class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  

  10.   

  11. <!-- 转换器 -->  

  12.   

  13. <property name="converters">  

  14.   

  15. <list>  

  16.   

  17. <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>  

  18.   

  19. </list>  

  20.   

  21. </property>  

  22.   

  23. </bean>  



 

 

1.5.6 pojo

1.5.6.1 简单pojo

将pojo对象中的属性名于传递进来的属性名对应,如果传进来的参数名称和对象中的属性名称一致则将参数值设置在pojo对象中

 

页面定义如下;

 

<input type="text" name="name"/>

<input type="text" name="price"/>

 

Contrller方法定义如下:

 

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. @RequestMapping("/editItemSubmit")  

  2.   

  3. public String editItemSubmit(Items items)throws Exception{  

  4.   

  5. System.out.println(items);  



请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。

 

1.5.6.2 包装pojo

如果采用类似struts中对象.属性的方式命名,需要将pojo对象作为一个包装对象的属性,action中以该包装对象作为形参。

包装对象定义如下:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. Public class QueryVo {  

  2.   

  3. private Items items;  

  4.   

  5.    

  6.   

  7. }  



 

页面定义:

 

<input type="text" name="items.name" />

<input type="text" name="items.price" />

 

Controller方法定义如下:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{  

  2.   

  3. System.out.println(queryVo.getItems());  



 

 

1.5.6.3 ModelAttribute

@ModelAttribute作用如下:

1、绑定请求参数到pojo并且暴露为模型数据传到视图页面

此方法可实现数据回显效果。

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1.    

  2.   

  3. // 商品修改提交  

  4.   

  5. @RequestMapping("/editItemSubmit")  

  6.   

  7. public String editItemSubmit(@ModelAttribute("item") Items items,Model model)  



页面:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <tr>  

  2.   

  3. <td>商品名称</td>  

  4.   

  5. <td><input type="text" name="name" value="${item.name }"/></td>  

  6.   

  7. </tr>  

  8.   

  9. <tr>  

  10.   

  11. <td>商品价格</td>  

  12.   

  13. <td><input type="text" name="price" value="${item.price }"/></td>  

  14.   

  15. </tr>  



 

 

如果不用@ModelAttribute可以使用model.addAttribute("item", items)完成数据回显。

 

2、将方法返回值暴露为模型数据传到视图页面

 

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. //商品分类  

  2.   

  3. @ModelAttribute("itemtypes")  

  4.   

  5. public Map<String, String> getItemTypes(){  

  6.   

  7. Map<String, String> itemTypes = new HashMap<String,String>();  

  8.   

  9. itemTypes.put("101", "数码");  

  10.   

  11. itemTypes.put("102", "母婴");  

  12.   

  13. return itemTypes;  

  14.   

  15. }  



 

页面:

商品类型:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <select name="itemtype">  

  2.   

  3. <c:forEach items="${itemtypes }" var="itemtype">  

  4.   

  5. <option value="${itemtype.key }">${itemtype.value }</option>  

  6.   

  7. </c:forEach>  

  8.   

  9. </select>  



 

1.5.7 集合类

1.5.7.1 字符串数组

页面定义如下:

页面选中多个checkbox向controller方法传递

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <input type="checkbox" name="item_id" value="001"/>  

  2.   

  3. <input type="checkbox" name="item_id" value="002"/>  

  4.   

  5. <input type="checkbox" name="item_id" value="002"/>  

  6.   

  7.    



 

传递到controller方法中的格式是:001,002,003

 

Controller方法中可以用String[]接收,定义如下:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public String deleteitem(String[] item_id)throws Exception{  

  2.   

  3. System.out.println(item_id);  

  4.   

  5. }  



 

1.5.7.2 List

List中存放对象,并将定义的List放在包装类中,action使用包装对象接收。

 

List中对象:

成绩对象

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. Public class QueryVo {  

  2.   

  3. Private List<Items> itemList;//订单明细  

  4.   

  5.    

  6.   

  7.   //get/set方法..  

  8.   

  9. }  



 

 

包装类中定义List对象,并添加get/set方法如下:

 

 

页面:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1.    

  2.   

  3.    

  4.   

  5. <tr>  

  6.   

  7. <td>  

  8.   

  9. <input type="text" name=" itemList[0].id" value="${item.id}"/>  

  10.   

  11. </td>  

  12.   

  13. <td>  

  14.   

  15. <input type="text" name=" itemList[0].name" value="${item.name }"/>  

  16.   

  17. </td>  

  18.   

  19. <td>  

  20.   

  21. <input type="text" name=" itemList[0].price" value="${item.price}"/>  

  22.   

  23. </td>  

  24.   

  25. </tr>  

  26.   

  27. <tr>  

  28.   

  29. <td>  

  30.   

  31. <input type="text" name=" itemList[1].id" value="${item.id}"/>  

  32.   

  33. </td>  

  34.   

  35. <td>  

  36.   

  37. <input type="text" name=" itemList[1].name" value="${item.name }"/>  

  38.   

  39. </td>  

  40.   

  41. <td>  

  42.   

  43. <input type="text" name=" itemList[1].price" value="${item.price}"/>  

  44.   

  45. </td>  

  46.   

  47. </tr>  



 

 

Contrller方法定义如下:

 

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{  

  2.   

  3. System.out.println(queryVo.getItemList());  

  4.   

  5. }  



 

1.5.7.3 Map

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

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

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. Public class QueryVo {  

  2.   

  3. private Map<String, Object> itemInfo = new HashMap<String, Object>();  

  4.   

  5.   //get/set方法..  

  6.   

  7. }  

  8.   

  9.    



 

 

页面定义如下:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <tr>  

  2.   

  3. <td>学生信息:</td>  

  4.   

  5. <td>  

  6.   

  7. 姓名:<inputtypeinputtype="text"name="itemInfo['name']"/>  

  8.   

  9. 年龄:<inputtypeinputtype="text"name="itemInfo['price']"/>  

  10.   

  11. .. .. ..  

  12.   

  13. </td>  

  14.   

  15. </tr>  



 

Contrller方法定义如下:

 

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{  

  2.   

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

  4.   

  5. }  



 

 

 

 

 

1.6 问题总结

1.6.1 404

页面找不到,视图找不到。

 

 

 

 

 

HandlerMapping根据url没有找到Handler。

 

 

1.6.2 Post时中文乱码

在web.xml中加入:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <filter>  

  2.   

  3. <filter-name>CharacterEncodingFilter</filter-name>  

  4.   

  5. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

  6.   

  7. <init-param>  

  8.   

  9. <param-name>encoding</param-name>  

  10.   

  11. <param-value>utf-8</param-value>  

  12.   

  13. </init-param>  

  14.   

  15. </filter>  

  16.   

  17. <filter-mapping>  

  18.   

  19. <filter-name>CharacterEncodingFilter</filter-name>  

  20.   

  21. <url-pattern>/*</url-pattern>  

  22.   

  23. </filter-mapping>  



 

以上可以解决post请求乱码问题。

对于get请求中文参数出现乱码解决方法有两个:

 

修改Tomcat配置文件添加编码与工程编码一致,如下:

 

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>  


 

另外一种方法对参数进行重新编码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. String userName new  

  2.   

  3. String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")  



 

ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

 

 

 

1.7 与struts2不同

1、 springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过虑器。

2、 springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发,传递参数是通过类的属性,只能设计为多例。

3、 Struts采用值栈存储请求和响应的数据,通过OGNL存取数据,springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过reques域传输到页面。Jsp视图解析器默认使用jstl。

 


相关文章