1、搜索框完成站內搜索java
實現功能及思路:在首頁文本框中輸入任意字,點擊搜索按鈕將實現站內查詢,將含有輸入內容的商品顯示在首頁下方。web
思路:sql
1.在項目首頁中添加一個文本框和按鈕數據庫
userBar
app
<form action="serch" th:object="${goodsFrom}" method="post"> <li> <span><input name="commodityName" type="text" /></span> <span><button type="submit" name="sousuo">搜索</button></span> </li> </form>
2.在GoodsController中定義一個方法將存放在bean中的值取到result中傳給list輸出到首頁。ide
在這裏進行了購物車的實例化post
GoodsControllerui
@RequestMapping(value = "/serch", method = RequestMethod.POST) public String delete(GoodsForm goodsForm, Model model) { List<GoodsForm> rescult = goodsService.serchGoodsList(goodsForm); model.addAttribute("list", rescult); CartForm cartForm = new CartForm(); model.addAttribute("cartList", cartService.searchCartList(cartForm)); return "shop/index"; }
3.定義一個list的方法存放搜索出來的結果列表spa
GoodsService
code
public List<GoodsForm> serchGoodsList(GoodsForm frm) { List<GoodsForm> result = queryDao.executeForObjectList("Goods.selectList", frm); return result; }
4.要完成站內的搜索,就必須在數據庫中篩選出含有輸入內容的數據。則此時就必須注意,一個完整的項目都是各個表之間的相互調用,此時查詢關聯表的狀況下,表名爲多個。而模糊查詢輸入的內容對應的SQL文爲:
where name like %XX%
而今天項目中的是 where 表名.name like '%$ XXXXXX $%' 並且where中多個條件並存時用AND或者OR鏈接
GoodsSqlMap
<select id="selectList" 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$%' </select>
2、改JSP爲HTML頁面
思路:將JSP頁面改成HTML是方便前臺後期的修改,須要將JSP頁面中的全部標籤換成HTML中的表達方式
form表單中須要添加一項 th:value="${userBean} " 表示引用的是UserBean 中的數據
文本輸出要用 th:text="${MESSAGE}"
<a th:href="@{delete(userId=${user.userId})}" 其中@爲指令,userId=${user.userId}爲取值操做