参考应用ch4创建应用practice44。在应用practice44中创建两个视图页面addGoods.jsp和goodsList.jsp。addGoods.jsp页面的来源gaodaimacom搞#代%码网显示效果如图4.5所示,goodsList.jsp页面的显示效果如图4.6所示。
图4.5 添加商品页面
图4.6 商品显示页面
具体要求:
1.商品类型由控制器类GoodsController的方法inputGoods进行初始化。GoodsController类中共有三个方法:inputGoods、addGoods和listGoods。
2.使用Goods模型类封装请求参数。
3.使用Service层,在Service的实现类中,使用静态集合变量模拟数据库存储商品信息,[email protected]
4.通过地址http://localhost:8080/practice44/goods/input访问addGoods.jsp页面。
5.其他的注意事项参见应用ch4。
1.配置web.xml
springmvc_10org.springframework.web.servlet.DispatcherServlet contextConfigLocationclasspath:springmvc_servlet.xml springmvc_10/ characterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilter encodingUTF-8 forceEncodingtrue characterEncodingFilter/*
2.在resources目录下配springmvc_servlet.xml
3.在WEB-INF目录下新建jsp文件夹
4.在java目录下新建com.sxau2包并在子目录下新建Controller、pojo、servlet包
Controller包下新建GoodsController.java
package com.sxau2.controller; import com.sxau2.pojo.Goods; import com.sxau2.servlet.GoodsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; @Controller @RequestMapping("/goods") public class GoodsController { @Autowired private GoodsService goodsService; @RequestMapping("/add") public String add(Model model){ Goods goods = new Goods(); model.addAttribute("goods",goods); model.addAttribute("goodsTypes",new String[]{"电器","食品","家居","数码"}); return "goodsAdd"; } @RequestMapping("/save") public String save(@ModelAttribute Goods goods,Model model){ if (goodsService.addGoods(goods)){ return "redirect:/goods/list"; }else return "/goods/add"; } @RequestMapping("/list") public String list(Model model){ ArrayList goods = goodsService.listGoods(); System.out.println(goods.toString()); model.addAttribute("listgoods",goods); return "goodsList"; } }
pojo包下新建Goods.java实体类
package com.sxau2.pojo; public class Goods { String goodsName; String goodsPrice; String goodsType; public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public String getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(String goodsPrice) { this.goodsPrice = goodsPrice; } public String getGoodsType() { return goodsType; } public void setGoodsType(String goodsType) { this.goodsType = goodsType; } @Override public String toString() { return "Goods{" + "goodsName='" + goodsName + '\'' + ", goodsPrice='" + goodsPrice + '\'' + ", goodsType='" + goodsType + '\'' + '}'; } }
service业务层新建GoodsService.java
package com.sxau2.servlet; import com.sxau2.pojo.Goods; import java.util.ArrayList; public interface GoodsService { boolean addGoods(Goods goods); ArrayList listGoods(); }
service业务层新建Impl包并在包下新建GoodsServiceImpl.java
package com.sxau2.servlet.impl; import com.sxau2.pojo.Goods; import com.sxau2.servlet.GoodsService; import org.springframework.stereotype.Service; import java.util.ArrayList; @Service public class GoodsServiceImpl implements GoodsService { private static ArrayList goodsList = new ArrayList(); @Override public boolean addGoods(Goods goods) { goodsList.add(goods); return true; } @Override public ArrayList listGoods() { return goodsList; } }
5.编写前端首页index.jsp页面
欢迎来到首页
跳转到商品首页
6.在jsp文件夹下新建goodsAdd.jsp、goodsList.jsp goodsAdd.jsp
商品名称: | |
商品价格: | |
商品类型: | 请选择 |
goodsList.jsp
商品展示继续添加
商品名称 | 商品价格 | 商品类型 |
---|---|---|
${goods.goodsName} | ${goods.goodsPrice} | ${goods.goodsType} |
7.运行注意事项:
地址栏:http://localhost:8080/
增加商品栏地址:http://localhost:8080/goods/add
商品展示地址:http://localhost:8080/goods/list
运行截图:
总结