地址请求映射@RequestMapping 、Controller方法返回值

一、 @RequestMapping

@RequestMapping :代表路径的映射,帮助处理器映射器找到具体的Handler
通过@RequestMapping注解可以定义不同的处理器映射规则。

1. 添加在方法上面

@RequestMapping(value=“item”)或@RequestMapping("/item")
value的值是数组,可以将多个url映射到同一个方法

RequestMapping支持标准的URL格式
也支持Ant风格的URL,其实就是支持在URL中使用通配符
— ?:匹配单个字符
— *:匹配0个或者任意个数的字符,但是仅限当前层级目录
— **:匹配0个或者任意个数的字符和任意层级的目录
@return


@RequestMapping(value = "/fr/**")
@ResponseBody
public String firstRequest(){
//默认情况返回字符串,SpringMVC会根据返回值查找对应的模板引擎的名字
//如果加了@ResponseBody,则直接响应数据,而不是找页面
return "First Request";
}

2. 添加在类上面

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

可以使用此方法对url进行分类管理

此时需要进入queryItemList()方法的请求url为:
http://localhost:8080/springmvc-web2/item/itemList.action
或者 http://localhost:8080/springmvc-web2/item/itemListAll.action

@Controller 
@RequestMapping("item") 
public class ItemController { 

@Autowired 
private ItemService itemService; 

/** * 查询商品列表 * * @return */ T 
@RequestMapping(value ={"itemlist", "itemListAll"}) 
public ModelAndView queryItemList() { 

//查询商品数据 
List<Item> list = this.itemService.queryItemList()

}

3. 请求方法限定

除了可以对url进行设置,还可以限定请求进来的方法

其实Spring后期,在基于 @RequestMapping 注解之上做了一些派生注解
比如只支持Get请求的 @GetMapping
只支持Post请求: @PostMapping
只支持Put请求: @PutMapping
等等.点开源码,实际就是对@RequestMapping套了一层娃,设定了具体的method属性

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {

限定GET方法

@RequestMapping(method = RequestMethod.GET)

如果通过POST访问则报错:
HTTP Status 405 - Request method ‘POST’ not supported

例如:

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

限定POST方法

@RequestMapping(method = RequestMethod.POST)

如果通过GET访问则报错:
HTTP Status 405 - Request method ‘GET’ not supported

GET和POST都可以

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

二、Controller方法返回值

1. 返回ModelAndView

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

@Controller
public class ItemController {

	// action可以写也可以不写
	@RequestMapping("/itemList.action")
	public ModelAndView queryItemList() {
		// 创建页面需要显示的商品数据
		List<Item> list = new ArrayList<>();
		list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));
		list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));

		// 创建ModelAndView,用来存放数据和视图
		ModelAndView modelAndView = new ModelAndView();
		// 设置数据到模型中  --model数据
		modelAndView.addObject("itemList", list);
		// 设置视图jsp,需要设置视图的物理地址   --view视图
		modelAndView.setViewName("itemList");

		return modelAndView;
	}
}

2. 返回void

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

  1. 使用request转发页面,如下:
    request.getRequestDispatcher(“页面路径”).forward(request, response);

  2. 可以通过response页面重定向:
    response.sendRedirect(“url”)

  3. 可以通过response指定响应结果,例如响应json数据如下:
    response.getWriter().print("{“abc”:123}");

/**
 * 返回void测试 
 * @param request
 * @param response
 * @throws Exception
 */
@RequestMapping("queryItem")
public void queryItem(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// 1 使用request进行转发
	 request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);

	// 2 使用response进行重定向到编辑页面
	 response.sendRedirect("/springmvc-web2/itemEdit.action");

	// 3 使用response直接显示
	response.getWriter().print("{\"abc\":123}");

)

3. 返回String

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

//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/itemList.jsp
@RequestMapping("Item")
public void Item(){
 return "itemList";
}

Contrller方法返回字符串可以重定向到一个url地址

如下商品修改提交后重定向到商品编辑页面。

/**
 * 更新商品
 * 
 * @param item
 * @return
 */
@RequestMapping("updateItem")
public String updateItemById(Item item) {
	// 更新商品
	this.itemService.updateItemById(item);

	// 修改商品成功后,重定向到商品编辑页面
	// 重定向后浏览器地址栏变更为重定向的地址,
	// 重定向相当于执行了新的request和response,所以之前的请求参数都会丢失
	// 如果要指定请求参数,需要在重定向的url后面添加 ?itemId=1 这样的请求参数
	return "redirect:/itemEdit.action?itemId=" + item.getId();
}

Controller方法执行后 转发 执行另一个Controller方法

如下商品修改提交后转向到商品修改页面,修改商品的id参数可以带到商品修改方法中。

/**
 * 更新商品 
 * @param item
 * @return
 */
@RequestMapping("updateItem")
public String updateItemById(Item item) {
	// 更新商品
	this.itemService.updateItemById(item);

	// 修改商品成功后,转发到商品编辑页面
	return "forward:/itemEdit.action";

}
//结果转发到editItem.action,request可以带过去
return "forward: /itemEdit.action";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值