您的位置:首页 > 编程语言 > Java开发

SpringMVC学习--功能完善

2016-06-07 22:07 435 查看
简介

  在基本的项目中,无非就是基本的增删改查,前面我们已经实现了一个简单的查询功能,现在来实现增删改功能,来了解实际开发中的运用,以修改功能为例,因为修改功能基本覆盖了增加和删除的运用。

  前面我们实现了查询列表的功能,现在根据查询列表进入到商品详情,然后修改商品信息然后再返回商品列表页面。

修改功能

  1、开发Mapper:根据id查询商品信息、根据id更新Items表的数据

  这个可以使用逆向工程实现,也可以自己实现。

  2、开发service

  service接口:

// 根据id查询商品信息
public ItemsCustom findItemsById(Integer id) throws Exception;
// 修改商品信息
public void updateItems(Integer id, ItemsCustom itemsCustom)
throws Exception;


  service实现类:

public ItemsCustom findItemsById(Integer id) throws Exception {

Items items = itemsMapper.selectByPrimaryKey(id);
// 中间对商品信息进行业务处理
// ....
// 返回ItemsCustom
ItemsCustom itemsCustom = new ItemsCustom();
// 将items的属性值拷贝到itemsCustom
BeanUtils.copyProperties(items, itemsCustom);

return itemsCustom;

}

@Override
public void updateItems(Integer id, ItemsCustom itemsCustom)
throws Exception {
// 添加业务校验,通常在service接口对关键参数进行校验
// 校验 id是否为空,如果为空抛出异常

// 更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段
// updateByPrimaryKeyWithBLOBs要求必须转入id
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}


  3、开发Controller

@Controller
@RequestMapping("/items")
public class ItemController {
@Autowired
private ItemsService itemsService;
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam里边指定request传入参数名称和形参进行绑定。
//通过required属性指定参数是否必须要传入
//通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
//调用service根据商品id查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom);

return "items/editItems";
}
// 商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request, Integer id,
ItemsCustom itemsCustom) throws Exception {
// 调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id, itemsCustom);
// 重定向到商品查询列表
//return "redirect:queryItems.action";
// 页面转发
return "forward:queryItems.action";
//return "success";
}
}


  从这两个方法中有很多可以总结的:

  1、在类前面加@RequestMapping("/items"),可以窄化请求,是请求根据类的url和方法的url拼接,这样可以按控制器进行分类来实现不同的调用。

  2、在方法前面加@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}),这样可以对方法的访问进行限制,只要get和post的方法可以访问。

  3、Controller的方法的返回类型有多种,可以是ModelAndView、void或string。

    (1)、返回ModelAndView

        需要方法结束时,定义ModelAndView,将model和view分别进行设置。

    (2)、返回string

        表示返回逻辑视图名:真正视图(jsp路径)=前缀+逻辑视图名+后缀

        redirect重定向:"redirect:queryItems.action"

        forward页面转发:"forward:queryItems.action"

     (3)、返回void:

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

        1、使用request转向页面,如下:

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

        2、通过response页面重定向:

        response.sendRedirect("url")

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

        response.setCharacterEncoding("utf-8");

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

        response.getWriter().write("json串");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: