springmvc教程--RESTful支持详解

栏目: 编程工具 · 发布时间: 8年前

内容简介:springmvc教程--RESTful支持详解

RESTful 支持

1.1 需求

RESTful 方式商品修改、商品查询。

1.2 添加 DispatcherServlet rest 配置

<servlet>

<servlet-name>springmvc-servlet-rest</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/springmvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc-servlet-rest</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

1.3 URL  模板模式映射

@RequestMapping (value= "/ editItem/{ item_i d}" ) { ××× } 占位符,请求的 URL 可以是 / editItem / 1 ”或“ / editItem / 2 ”,通过在方法中使用 @PathVariable 获取 { ××× } 中的 ×××变量。

@RequestMapping("/ editItem/{item_id}") 

public String useredit(@PathVariable("item_id ") String id,Model model) throws Exception{

//方法中使用@PathVariable获取useried的值,使用model传回页面

model.addAttribute("userid", userid);

return"/user/useredit";

}

如果 RequestMapping 中表示为 " / editItem/{ i d} " id 和形参名称一致, @PathVariable 不用指定名称。

商品查询的 controller 方法也改为 rest 实现:

// 查询商品列表

@RequestMapping("/queryItem")

public ModelAndView queryItem() throws Exception {

// 商品列表

List<Items> itemsList = itemService.findItemsList(null);

 

// 创建modelAndView准备填充数据、设置视图

ModelAndView modelAndView = new ModelAndView();

 

// 填充数据

modelAndView.addObject("itemsList", itemsList);

// 视图

modelAndView.setViewName("item/itemsList");

 

return modelAndView;

}

1.4 静态资源访问 <mvc:resources>

spring mvc <mvc:resources mapping="" location=""> 实现对静态资源进行映射访问。

如下是对 js 文件访问配置:

<mvc:resources location="/js/" mapping="/js/**"/>


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming the Mobile Web

Programming the Mobile Web

Maximiliano Firtman / O'Reilly Media / 2010-07-23 / $44.99

* Learn how to use your existing skills to move into mobile web development * Discover the new possibilities of mobile web development, and understand its limitations * Get detailed coverage of ......一起来看看 《Programming the Mobile Web》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具