在一个项目的首页添加一个检索功能7-22agriculture-mvc

咸利
2023-12-01

一、模糊查询的主要代码

1、

首先在UserBar.html中添加一个input输入框和button按钮
   <form action="selectGoods" th:object="${goodsForm}" method="post">
   <input type="text" name="commodityName"/>
   <input type="submit" value="检索"/>
   </form>

2、

在GoodsController里,下面的value和上面的action属性对应一致。

@RequestMapping(value = "selectGoods", method = RequestMethod.POST)
    public String  selectGoods(Model model,HttpSession session,GoodsForm goodsForm,Device device){
     log.info("商品信息");
     List<GoodsForm> list=goodsService.searchGoodsListrelative(goodsForm);
     model.addAttribute("list",list);
     UVO uvo = (UVO)session.getAttribute("UVO");
     if (uvo == null) {
      uvo = new UVO();
      session.setAttribute("UVO", uvo);
     }
     CartForm cartForm = new CartForm();
     cartForm.setGuestId(uvo.getGuestId());
     model.addAttribute("cartList", cartService.searchCartList(cartForm));
     if(device.isNormal()) {
      return "shop/index";                  返回PC
     } else {
      return "mobile/index";            返回手机
     }
    }
}

3、

在GoodsService里

public List<GoodsForm> searchGoodsListrelative(GoodsForm frm) {
  List<GoodsForm> result = queryDao.executeForObjectList("Goods.selectGoodsListrelative", frm);
  return result;
 }

4、

在GoodsSqlMap.xml中写查询语句

<select id="selectGoodsListrelative"
  parameterClass="cn.agriculture.web.form.GoodsForm"
  resultClass="cn.agriculture.web.form.GoodsForm">
  SELECT commodity.commodity_id as commodityId,
   commodity.type as type,
   supplier.supplier_name as supplierName,
   brand.brand_name as brandName,
   commodity.commodity_name as commodityName,
   commodity.weight as weight,
   commodity.is_gift as isGift,
   commodity.specifications as specifications,
   commodity.unit as unit,
   commodity.benchmark_price as benchmarkPrice,
   commodity.guide_price as guidePrice,
   commodity.retail_price as retailPrice,
   commodity.competition_level as competitionLevel,
   commodity.note as note,
   commodity.update_time as updateTime,
   commodity.update_user as updateUser,
   commodity.picture_id as pictureId,
   stock.stock as stock
  FROM commodity, supplier, brand, stock
  WHERE commodity.commodity_id = stock.commodity_id
  AND commodity.supplier_id = supplier.supplier_id
  AND commodity.brand_id = brand.brand_id
  AND commodity.commodity_name like '%$commodityName$%'
  <!-- contan("%",#commodityName#,"%") -->>

二、实现格式控制

  1、@Null  被注释的元素必须为 null

        @NotNull  被注释的元素必须不为 null

        @AssertTrue 被注释的元素必须为 true

        @AssertFalse 被注释的元素必须为 false

        @Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值

        @Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

        @DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值

        @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值

        @Size(max, min) 被注释的元素的大小必须在指定的范围内

        @Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内

        @Past 被注释的元素必须是一个过去的日期

        @Future 被注释的元素必须是一个将来的日期

        @Pattern(value) 被注释的元素必须符合指定的正则表达式hibernate对这个规范做了实现和扩展;

        @Email 被注释的元素必须是电子邮箱地址

        @Length 被注释的字符串的大小必须在指定的范围内

        @NotEmpty 被注释的字符串的必须非空

        @Range 被注释的元素必须在合适的范围内

    2、例:对QQ,Email,Zip实现控制

        @Email(message="{errors.email}")

     private String email;

     @Digits(fraction = 0, integer = 11,message="{errors.qq}")

     private String qq;

     @Digits(fraction = 0, integer = 6,message="{errors.zip}")

     @Length(min=6,max=6,message="{errors.zip}")

     private String zip;

转载于:https://my.oschina.net/u/2411762/blog/482555

 类似资料: