完成SpringMVC框架搭建。
本实验主要介绍MVC的各种URL映射和数据绑定方式,为了方便描述对象或表单数据的请求和返回,本实验定义了水果类(Fruit)。
MVC_DEMO项目创建类FruitController、类Fruit和fruit.jsp文件
FruitController:实验中所涉及到的方法都在该类中实现
类路径:com.inspur.demo.controller.FruitController
Fruit:水果类,用于描述对象或表单的数据请求和返回
类路径:com.inspur.demo.data.Fruit
fruit.jsp:前端展现页面,请求URL处理后跳到该URL进行展现
文件路径:jsp/demo/fruit.jsp
1. Fruit类实现
package com.inspur.demo.data;
/** * 水果实体 */ publicclass Fruit {
/** * 编码 */ private String code;
/** * 名称 */ private String name;
/** * 产地 */ private String origin;
/** * 进货日期 */ private String datetime;
/** * 描述 */ private String note;
public String getCode() { returncode; }
publicvoid setCode(String code) { this.code = code; }
public String getName() { returnname; }
publicvoid setName(String name) { this.name = name; }
public String getOrigin() { returnorigin; }
publicvoid setOrigin(String origin) { this.origin = origin; }
public String getDatetime() { returndatetime; }
publicvoid setDatetime(String datetime) { this.datetime = datetime; }
public String getNote() { returnnote; }
publicvoid setNote(String note) { this.note = note; }
} |
2.FruitController类实现
FruitController.java类作为“水果”案例的控制层,该层实现用户访问的拦截和跳转任务。当用户请求相应的路径时,会被FruitController层拦截,并进行对应方法的匹配。该方法调用对应的javabean进行业务处理,处理完成后跳转到需求页面。内容如下:
package com.inspur.demo.controller;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import com.inspur.demo.data.Fruit;
@Controller @RequestMapping("/demo") publicclass FruitController {
} |
注:@Controller注解符用来将该类注册为controller层。
@RequestMapping用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
3. fruit.jsp
fruit.jsp用于请求数据的展现,采用JSTL的方式绑定数据。
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>水果超市</title> </head>
<body> <table> <tr><td>产品编码:</td><td>${fruit.code}</td></tr> <tr><td>产品名称:</td><td>${fruit.name}</td></tr> <tr><td>产品产地:</td><td>${fruit.origin}</td></tr> <tr><td>进货日期:</td><td>${fruit.datetime}</td></tr> <tr><td>产品说明:</td><td>${fruit.note}</td></tr> </table> </body>
</html> |
编写FruitController类
使用ServletAPI对象作为入参
在FruitController类中添加如下代码。该方法使用ServletAPI对象HttpservletRequest来接受参数。该对象封装了请求的所有内容,使用对应的get方法可以获取所需参数。
/** * 使用 Servlet API 对象作为入参 * http://localhost:8080/mvc_demo/service/demo/test1?code=008&name=测试 * @return */ @RequestMapping(value="/test1") public ModelAndView test1(HttpServletRequest request){ String code = request.getParameter("code"); String name = request.getParameter("name"); Fruit fruit = new Fruit(); fruit.setCode(code); fruit.setName(name); fruit.setOrigin("济南"); ModelAndView mv = new ModelAndView(); mv.addObject("fruit", fruit); mv.setViewName("/demo/fruit"); returnmv; } |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test1?code=008&name=测试
通过请求参数访问URL(参数code不可省略)
在FruitController类中添加如下代码。该方法使用@RequestParam注解来接受参数。该方法必须要求请求URL中携带参数,否则会报错。
/** * 通过请求参数访问URL(参数code不能省略) * http://localhost:8080/mvc_demo/service/demo/test2?code=001 * @return */ @RequestMapping(value="/test2") public ModelAndView test2(@RequestParam("code") String code){ Fruit fruit = new Fruit(); fruit.setCode(code); fruit.setName("苹果"); fruit.setOrigin("济南"); ModelAndView mv = new ModelAndView(); mv.addObject("fruit", fruit); mv.setViewName("/demo/fruit"); returnmv;
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test2?code=001
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test2
注:不携带code参数的情况
通过占位符访问URL
在FruitController类中添加如下代码。当使用形如XXURL/{paramId}样式映射时,这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri 参数中的名称。
/** * 通过占位符访问URL * http://localhost:8080/mvc_demo/service/demo/test4/code * @return */ @RequestMapping(value="/test4/{code}") public ModelAndView test4(@PathVariable(value="code") String code){ Fruit fruit = new Fruit(); fruit.setCode(code); fruit.setName("苹果"); fruit.setOrigin("济南"); ModelAndView mv = new ModelAndView(); mv.addObject("fruit", fruit); mv.setViewName("/demo/fruit"); returnmv; } |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test4/A0001
String类型返回(不返回对象信息)
在FruitController类中添加如下代码。该方法直接用了跳转到指定路径,不返回任何对象信息。
/** * String类型返回(不返回对象信息) * * http://localhost:8080/mvc_demo/service/demo/test5 * @return */ @RequestMapping(value="/test5") public String test5(){ return"/demo/fruit";
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test5
String类型返回(返回对象信息)
在FruitController类中添加如下代码。该方法将返回修改后“水果”属性。
/** * String类型返回(返回对象信息) * * http://localhost:8080/mvc_demo/service/demo/test6 * @return */ @RequestMapping(value="/test6") public String test6(Model model){ Fruit fruit = new Fruit(); fruit.setCode("111"); fruit.setName("香蕉"); fruit.setOrigin("海南"); model.addAttribute("fruit", fruit); return"/demo/fruit"; } |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test6
@ModelAttribute用法
在FruitController类中添加如下代码。该注解有两个用法,一个是用于方法上,一个是用于参数上;用于方法上时:通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上。
/** * @ModelAttribute用法 * * http://localhost:8080/mvc_demo/service/demo/test7 * @return */ @RequestMapping(value="/test7") public String test10(@ModelAttribute Fruit fruit){ fruit.setCode("113"); fruit.setName("香蕉"); fruit.setOrigin("海南"); return"/demo/fruit";
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test7
对象传播
在FruitController类中添加如下代码。该方法使用Friut对象来传值。
/** * 对象传值 * http://localhost:8080/mvc_demo/service/demo/test8?code=008&name=测试 * @return */ @RequestMapping(value="/test8") public ModelAndView test8(Fruit fruit){ ModelAndView mv = new ModelAndView(); mv.addObject("fruit", fruit); mv.setViewName("/demo/fruit"); returnmv;
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test8?code=008&name=测试
Body传值
在FruitController类中添加如下代码。@RequestBody用于body传值,将body中的数据绑定到请求对象中(需要POST方式)。
/** * Body传值 * 使用Firefox rest插件测试 * http://localhost:8080/mvc_demo/service/demo/test9 * POST请求 * json格式请求头:Content-Type:application/json * json数据 {"code":"121","name":"测试"} * @return */ @RequestMapping(value="/test9") public ModelAndView test9(@RequestBody Fruit fruit){ ModelAndView mv = new ModelAndView(); mv.addObject("fruit", fruit); mv.setViewName("/demo/fruit"); return mv; } |
注:为了方便测试,我们提供以下测试页面
浏览器访问:http://localhost:8080/mvc_demo/jsp/rest.jsp
将Method修改为post,URL处填写:http://localhost:8080/mvc_demo/service/demo/test9
参数填写:{"code":"121","name":"测试"}。
Json数据请求(List处理)
在FruitController类中添加如下代码。该方法返回返回List类型数据。
/** * json数据请求(List处理) * http://localhost:8080/mvc_demo/service/demo/test10/code */ @RequestMapping(value="/test10/{code}") @ResponseBody public List<Fruit> test10(@PathVariable(value="code") String code){ List<Fruit> list = new ArrayList<Fruit>();
Fruit fruit1 = new Fruit(); fruit1.setCode(code); fruit1.setName("苹果"); fruit1.setOrigin("济南"); list.add(fruit1); Fruit fruit2 = new Fruit(); fruit2.setCode(code); fruit2.setName("苹果"); fruit2.setOrigin("济南"); list.add(fruit2); returnlist;
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test10/A001
Json数据请求(Map处理)
在FruitController类中添加如下代码。该方法返回Map类型的数据。
/** * json数据请求(Map处理) * http://localhost:8080/mvc_demo/service/demo/test11/code */ @RequestMapping(value="/test11/{code}") @ResponseBody public Map<String, Object> test5(@PathVariable(value="code") String code){ Map<String, Object> map = new HashMap<String, Object>();
List<Fruit> list = new ArrayList<Fruit>(); Fruit fruit1 = new Fruit(); fruit1.setCode(code); fruit1.setName("苹果"); fruit1.setOrigin("济南"); list.add(fruit1); Fruit fruit2 = new Fruit(); fruit2.setCode(code); fruit2.setName("苹果"); fruit2.setOrigin("济南"); list.add(fruit2); map.put("fruit", list); returnmap;
} |
浏览器访问:http://localhost:8080/mvc_demo/service/demo/test11/A001