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

SpringMVC学习笔记(八)——商品修改功能开发

2018-04-02 13:54 316 查看

一、操作流程

1、进入商品查询列表页面。

2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询),要修改的商品从数据库查询,根据商品id(主键)查询商品信息。

3、在商品修改页面,修改商品信息,修改后,点击提交。

二、开发
mapper

mapper


根据id查询商品信息

根据id更新Items表的数据

不用开发了,使用逆向工程生成的代码。

三、开发
service

service
接口

package com.jiayifan.service;

import com.jiayifan.ssm.po.ItemsQueryVo;

import java.util.List;

import com.jiayifan.ssm.po.*;
/**
* 商品管理的Service
* @author 贾一帆
*
*/
public interface ItemsService {
//商品的查询列表
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
//根据id查询商品信息
public ItemsCustom findItemsById(Integer id) throws Exception;
//修改商品信息
/**
*
* @param id 要修改商品的id
* @param itemsCustom
* @throws Exception
*/
public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;

}


service
接口实现类

package com.jiayifan.ssm.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.jiayifan.service.ItemsService;
import com.jiayifan.ssm.mapper.ItemsMapper;
import com.jiayifan.ssm.mapper.ItemsMapperCustom;
import com.jiayifan.ssm.po.Items;
import com.jiayifan.ssm.po.ItemsCustom;
import com.jiayifan.ssm.po.ItemsQueryVo;

/**
* 商品管理
* @author 贾一帆
*
*/
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapperCustom itemsMapperCustom;
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通过ItemsMapperCustom查询数据库
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
@Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
//中间对商品信息进行业务处理
//业务处理代码
//返回扩展类
ItemsCustom itemsCustom = new ItemsCustom();
//将items的内容拷贝到itemsCustom中
BeanUtils.copyProperties(items, itemsCustom);
return itemsCustom;
}
@Override
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//添加业务校验,通常在service接口对关键参数进行校验
//校验id是否为空,如果为空我们就抛出异常

//更新商品信息,使用该方法可以根据id修改items表中的所有信息,包括大文本字段
//调用updateByPrimaryKeyWithBLOBs方法必须在参数对象中传入id
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}

}


四、开发
controller

package com.jiayifan.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jiayifan.service.ItemsService;
import com.jiayifan.ssm.po.Items;
import com.jiayifan.ssm.po.ItemsCustom;

/**
* 商品的Controller
* @author 贾一帆
*
*/
@Controller
public class ItemsController {
//商品查询列表
//@RequestMapping("/queryItems")实现的是对queryItems方法和url进行映射,一个方法对应一个url
//建议url和方法名一样
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
//调用service查找数据库,查询商品列表,这里先使用静态的数据模拟
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//想List中填充静态数据

//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute方法
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
//商品信息修改页面显示
@RequestMapping("/editItems")
public ModelAndView editItems() throws Exception {
//调用sercice查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(1);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//将商品信息放到Model
modelAndView.addObject("itemsCustom",itemsCustom);
//商品修改页面
modelAndView.setViewName("items/editItems");
return modelAndView;

de39
}
//商品信息修改提交
@RequestMapping("/editItemsSubmit")
public ModelAndView editItemsSubmit() throws Exception {
//调用service返回商品信息,页面需要将商品信息传到此方法
//此方法暂停

ModelAndView modelAndView = new ModelAndView();
//先返回一个成功页面
modelAndView.setViewName("success");
return modelAndView;
}
}


五、部署调试





六、
@RequestMapping
用法

1、通过
RequestMapping
注解可以定义不同的处理器映射规则。


@RequestMapping(value="/item")
@RequestMapping("/item")


value
的值是数组,可以将多个
url
映射到同一个方法

2、窄化请求映射

class
上添加
@RequestMapping(url)
指定通用请求前缀, 限制此类下的所有方法请求
url
必须以请求前缀开头,通过此方法对
url
进行分类管理。

//为了对我们的url进行分类管理,可以在这里定义一个根路径,最终访问url是根路径+子路径
//比如商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {


3、请求方法限定

(1)限定
GET
方法

@RequestMapping(method = RequestMethod.GET)


如果通过
Post
访问则报错:

HTTP Status 405 - Request method 'POST' not supported


例如:

@RequestMapping(value="/editItem",method=RequestMethod.GET)


(2)限定
POST
方法

@RequestMapping(method = RequestMethod.POST)


如果通过Post访问则报错:

HTTP Status 405 - Request method 'GET' not supported




(3)
GET
POST
都可以

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


七、
controller
方法返回值

1、返回
ModelAndView


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


2、返回
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串");


3、返回字符串

(1)表示返回逻辑视图名。

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

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model) throws Exception {
//调用sercice查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(1);
//通过参数中的model将数据传到页面
model.addAttribute("itemsCustom",itemsCustom);
return "items/editItems";
}


(2)
redirect
重定向

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

redirect
重定向特点:浏览器地址栏中的
url
会变化。修改提交的
request
数据无法传到重定向的地址。因为重定向后重新进行
request
request
无法共享)

@RequestMapping("/editItemsSubmit")
public String editItemsSubmit() throws Exception {
//重定向
return "redirect:queryItems.action";
}


(3)
forward
页面转发

通过
forward
进行页面转发,浏览器地址栏
url
不变,
request
可以共享。

@RequestMapping("/editItemsSubmit")
public String editItemsSubmit() throws Exception {
//重定向
return "forward:queryItems.action";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ssm整合
相关文章推荐