九、SpringMVC注解开发之商品修改功能

开发mapper

使用逆向工程 mybatis-generator生成代码。java

开发service

com.tianlang.service.ItemsService中添加两个接口数据库

/**
* 根据id查询商品信息
*/
ItemsCustom findItemsById(Integer id);

/**
* 修改商品信息
*/
void updateItems(Integer id,ItemsCustom itemsCustom);

com.tianlang.service.impl.ItemsServiceImpl中实现接口,增长itemsMapper属性json

@Autowired
private ItemsMapper itemsMapper;

public ItemsCustom findItemsById(Integer id) {
    Items items = itemsMapper.selectByPrimaryKey(id);
    //返回ItemsCustom
    ItemsCustom itemsCustom = new ItemsCustom();
    //将items的属性值拷贝到itemsCustom
    BeanUtils.copyProperties(items, itemsCustom);
    return itemsCustom;
}

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

开发controller

方法:浏览器

  • 商品信息修改页面显示
  • 商品信息修改提交
@Controller
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    //商品查询列表
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() {
        //调用service查找数据库,查询商品列表
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);
        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //至关于request的setAttribute方法,在jsp页面中经过itemsList取数据
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("items/itemsList");
        return modelAndView;
    }

    //商品信息修改页面显示
    @RequestMapping("/editItems")
	public ModelAndView editItems() {
		//调用service根据商品id查询商品信息
		ItemsCustom itemsCustom = itemsService.findItemsById(1);
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//将商品信息放到model
		modelAndView.addObject("itemsCustom", itemsCustom);
		//商品修改页面
		modelAndView.setViewName("items/editItems");
		return modelAndView;
	}

    //商品信息修改提交
    @RequestMapping("/editItemsSubmit")
    public ModelAndView editItemsSubmit(HttpServletRequest request, Integer id, ItemsCustom itemsCustom) {
        //调用service更新商品信息,页面须要将商品信息传到此方法
        itemsService.updateItems(id, itemsCustom);
        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //返回一个成功页面
        modelAndView.setViewName("success");
        return modelAndView;
    }
}

@RequestMapping

  • url映射

定义controller方法对应的url,进行处理器映射使用。安全

  • 窄化请求映射

为了对url进行分类管理 ,能够类上定义根路径,最终访问url是根路径+子路径mybatis

@Controller
@RequestMapping("/items")
public class ItemsController {
  • 限制http请求方法

出于安全性考虑,对http的连接进行方法限制。app

@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET})
public ModelAndView editItems()throws Exception {

若是限制请求为post方法,进行get请求,即将上面代码的注解改成@RequestMapping(value="/editItems",method={RequestMethod.POST})jsp

controller方法的返回值

  • 返回ModelAndView

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

  • 返回string

若是controller方法返回stringurl

1.表示返回逻辑视图名。

真正视图(jsp路径)=前缀+逻辑视图名+后缀

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model) {
    //调用service根据商品id查询商品信息
    ItemsCustom itemsCustom = itemsService.findItemsById(1);
    //经过形参中的model将model数据传到页面
    //至关于modelAndView.addObject方法
    model.addAttribute("itemsCustom", itemsCustom);
    return "items/editItems";
}

2.redirect重定向

商品修改提交后,重定向到商品查询列表。

redirect重定向特色:浏览器地址栏中的url会变化。修改提交的request数据没法传到重定向的地址。由于重定向后从新进行request(request没法共享)

//重定向到商品查询列表
return "redirect:queryItems.action";

3.forward页面转发

经过forward进行页面转发,浏览器地址栏url不变,request能够共享。

//页面转发
return "forward:queryItems.action";
  • 返回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串");
相关文章
相关标签/搜索