首页>代码>基于SSM框架简单的商城购物车项目实例>/超市项目/shopcart/src/cn/sjzedu/controller/GoodsController.java
package cn.sjzedu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.sjzedu.pojo.Goods;
import cn.sjzedu.service.GoodsService;

@Controller
/*@RequestMapping("/Goods")
*/public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	//前台商品展示刷新展示商品界面
	@RequestMapping("showgoods")
	public ModelAndView UserLogin(HttpServletRequest req,HttpServletResponse rsp) throws Exception{
		ModelAndView modelAndView=new ModelAndView();
		/*增加参数i的原因
		 * 由于需要从别的controller转发到这个controller进行商品的刷新展示
		 * 但是有需要有提示语句,所有通过传过来的不同参数,在展示商品界面展示不同的提示语
		 * */
		String i = req.getParameter("i");
		if(i!=null&&i.length()>0){
			int r = Integer.parseInt(i);
			//从Cart/addgoods.action加入购物车中跳转过来
			if(r==1){
				modelAndView.addObject("alert", "请输入合理的数量");
			}
			if(r==2){
				modelAndView.addObject("alert", "请输入数量");

			}
			//从Cart/accountcash。action结算中跳转过来
			if(r==3){
				modelAndView.addObject("alert", "结算成功请继续购物");

			}
		}
		List<Goods> goodslist = goodsService.selectAllGoods();
		modelAndView.addObject("goodslist", goodslist);
		modelAndView.setViewName("showgoods");	
		return modelAndView;
		
	}
	
	/*从这里开始是后台商品管理的action*/	
	
	//后台管理中的添加商品
		@RequestMapping("insert")
		public String insert(Goods record,RedirectAttributes attr){
			ModelAndView modelAndView=new ModelAndView();
			String url = "";
			int i=goodsService.insertGoods(record);
			if(i>0){
				attr.addAttribute("alert", "添加成功");
				url="redirect:goodsList.action";
			}else{
				modelAndView.addObject("alert", "文本框不能为空");
				url="goods/insertGoods";
			}
			return url;
		}
		
		//后台管理中的删除商品
		@RequestMapping("/delete")
		public ModelAndView delete(Integer[] id,HttpServletRequest req,HttpServletResponse res,RedirectAttributes attr){
			ModelAndView modelAndView=new ModelAndView();
			if(req.getParameter("id")!=null&&req.getParameter("id").length()>0){
			int gid=Integer.parseInt(req.getParameter("id"));
			if(gid>0){
			    goodsService.deleteById(id);
				attr.addFlashAttribute("alert", "删除成功");
				modelAndView=new ModelAndView("redirect:goodsList.action");		
			}else{
				 attr.addFlashAttribute("alert", "删除失败");
				 modelAndView=new ModelAndView("redirect:goodsList.action");
			}
			}else{
				 attr.addFlashAttribute("alert", "请选择要删除的商品");
				 modelAndView=new ModelAndView("redirect:goodsList.action");
			}
			return modelAndView;
		}
		
		//后台管理中的更新商品
		@RequestMapping("/update")
		public ModelAndView update(Goods record,RedirectAttributes attr){
			ModelAndView modelAndView=new ModelAndView();
			int goodsid = record.getId();
			Goods goods = goodsService.selectGoodsById(goodsid);
			modelAndView.addObject("goods", "goods");
			modelAndView.setViewName("goods/updateGoods");
			return modelAndView;
		}
		//后台管理刷新展示商品
		@RequestMapping("/goodsList")
		public ModelAndView goodsList(HttpServletRequest req,HttpServletResponse rsp) throws Exception{
			ModelAndView modelAndView=new ModelAndView();
			List<Goods> goodslist = goodsService.selectAllGoods();
			modelAndView.addObject("goodslist", goodslist);
			modelAndView.setViewName("goods/goodsList");
			return modelAndView;	
		}
		//后台管理根据名称模糊查询商品
		@RequestMapping("/selectName")
		public ModelAndView selectName(HttpServletRequest req,HttpServletResponse rsp)throws Exception{
			String goodsname = req.getParameter("goodsname");
			List<Goods> goodslist=goodsService.selectGoodByName(goodsname);
			ModelAndView modelAndView=new ModelAndView();
			modelAndView.addObject("goodslist",goodslist);
			modelAndView.setViewName("goods/goodsList");
			return modelAndView;
		}
}
最近下载更多
15342201772  LV9 2024年11月12日
Xppasdf  LV3 2024年10月29日
taoshen95  LV16 2024年3月22日
gecongkai  LV8 2023年6月22日
yzshabzbbdvw  LV4 2023年4月15日
malike  LV3 2022年12月14日
lsglsg9  LV23 2022年11月26日
微信网友_6200823191523328  LV3 2022年11月11日
admin0108  LV9 2022年10月12日
1020132795  LV5 2022年9月25日
最近浏览更多
xiaoaitx  LV8 1月1日
hcghhh 2024年12月28日
暂无贡献等级
微信网友_7310057461567488 2024年12月24日
暂无贡献等级
三秋桂子  LV1 2024年12月22日
Ella0902 2024年12月18日
暂无贡献等级
n1234560090 2024年12月11日
暂无贡献等级
sjwc88  LV3 2024年12月4日
Xppasdf  LV3 2024年10月29日
zouzou123  LV3 2024年10月18日
lyh1989  LV34 2024年7月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友