在今天做的工程中,我们修改的是商品的搜索。在之前的基础上,我们要在搜索的商品中实现添加默认、人气和价格的一个排序问题。我们在搜索得到的商品结果后返回list页面,在这个页面上,我们需要默认、人气和价格三个按钮来实现排序功能,这个功能的实现需要设置对应的5个链接,这5个链接的作用分别是用来区分,哪一个项被选中的样式,而且其中包括人气和价格的升降序按钮必须有一个显示在页面上,所以有个th:if的判断,保证某一个排序的两个升序和降序的链接必有一个出现。问题是存在于,我在设置对应的链接时,需要一个与之前页面上按照商品某一类的商品的对应的默认、人气、价格相区分,因为我们需要执行不同的controller,但是这里不执行对应的controller似乎也是可以的,不过要在对应的一个controller中区分开是此时的是按照商品类别分类,还是按照商品名称的相关性搜索得到的。
页面上的写法是:这里使用的是一个判断,判断字符是否为空,写法是:th:if=“${#strings.isEmpty(goodsForm.commodityName)}”,不空时执行的是:th:if="${not #strings.isEmpty(goodsForm.commodityName)}"
<span th:if="${#strings.isEmpty(goodsForm.commodityName)}">
<a th:href="@{initGoods(commodityTypeId=${goodsForm.commodityTypeId})}" th:class="${orderTypeId}==1?'btn btn-default btn-danger':'btn btn-default'"> 默 认 </a>
<a th:href="@{initGoodsByPopularDesc(commodityTypeId=${goodsForm.commodityTypeId})}" th:class="${orderTypeId}==3?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}!=2"> 人 气<i></i> </a>
<a th:href="@{initGoodsByPopular(commodityTypeId=${goodsForm.commodityTypeId})}" th:class="${orderTypeId}==2?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}==2"> 人 气<i class="up"></i> </a>
<a th:href="@{initGoodsByPriceDesc(commodityTypeId=${goodsForm.commodityTypeId})}" th:class="${orderTypeId}==5?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}!=4"> 价 格<i></i> </a>
<a th:href="@{initGoodsByPrice(commodityTypeId=${goodsForm.commodityTypeId})}" th:class="${orderTypeId}==4?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}==4"> 价 格<i class="up"></i> </a>
</span>
<span th:if="${not #strings.isEmpty(goodsForm.commodityName)}">
<a th:href="@{selectGoods1(commodityName=${goodsForm.commodityName})}" th:class="${orderTypeId}==1?'btn btn-default btn-danger':'btn btn-default'"> 默 认 </a>
<a th:href="@{selectGoodsByPopularDesc(commodityName=${goodsForm.commodityName})}" th:class="${orderTypeId}==3?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}!=2"> 人 气<i></i> </a>
<a th:href="@{selectGoodsByPopular(commodityName=${goodsForm.commodityName})}" th:class="${orderTypeId}==2?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}==2"> 人 气<i class="up"></i> </a>
<a th:href="@{selectGoodsByPriceDesc(commodityName=${goodsForm.commodityName})}" th:class="${orderTypeId}==5?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}!=4"> 价 格<i></i> </a>
<a th:href="@{selectGoodsByPrice(commodityName=${goodsForm.commodityName})}" th:class="${orderTypeId}==4?'btn btn-default btn-danger':'btn btn-default'" th:if="${orderTypeId}==4"> 价 格<i class="up"></i> </a>
</span>
使用的service是没变的,还是与之前分类的service分类一样,但是sql文中我们需要对应的修改,具体写法如下:
<select id="selectGoodsListByPopularDesc"
parameterClass="cn.agriculture.web.form.GoodsForm"
resultClass="cn.agriculture.web.form.GoodsForm">
SELECT commodity.commodity_id as commodityId,
commodity.type as type,
commodity_type.commodity_type_id as commodityTypeId,
commodity_type.commodity_type_name as commodityTypeName,
supplier.supplier_name as supplierName,
FROM commodity, supplier, brand, stock,commodity_type
WHERE commodity.commodity_id = stock.commodity_id
AND commodity.supplier_id = supplier.supplier_id
AND commodity.brand_id = brand.brand_id
AND commodity.type = commodity_type.commodity_type_id
<isNotEmpty property="commodityTypeId">
AND commodity_type.commodity_type_id=#commodityTypeId#
</isNotEmpty>
<isNotEmpty property="commodityName">
AND commodity.commodity_name LIKE '%$commodityName$%'
</isNotEmpty>
ORDER BY cast(stock.stock as signed) DESC
</select>
注意sql文中使用了isNotEmpty标签,这个的作用是判断相应的property对应的传入的值,即#commodityTypeId#与'%$commodityName$%'的值是否为空,来决定是否添加条件。
之前尝试将(AND commodity.type = commodity_type.commodity_type_id) 放入isNotEmpty中,因为之前我怀疑执行分类与这个有关(问题是commodityId的值一直不空导致的),所以放入然后导致上边的commodityType对应找不到,出现了这个与后边所有的属性进行了一个笛卡尔积的运算,每一个type都匹配了一个后边的属性,出现一个商品显示出现了多次。
在设置默认地址选中时,使用了th:checked属性,这个通过一个判断,条件为真时让该项选中,写法如下:
<input type="radio" name="addressId" class="vm" th:value="${receiveInfo.addressId}" th:checked="${addressDefault}==${receiveInfo.addressId}"/>
注意,在上边的代码中,radio这个单选按钮的传值问题解决,这个按钮传值通过th:value属性来实现的,我们循环中让它的值等于对应的addressId的值,这样选中后实现传相应的addressId值到controller中,但是注意单选按钮,它的name必须是一样的。
在商品购物篮初始化页面小数点设置时遇到无法实现对一个double型的数据选取小数点后两位作为有效数字。解决的办法是:通过BigDecimal这个数据类型,这个数据类型有一个setScale函数可以设置对应的的数据的小数点并实现四舍五入。代码具体如下:
BigDecimal sum=new BigDecimal(0);
for(int i=0;i<cartFormList.size();i++){
BigDecimal smallSumPrice=new BigDecimal(Double.toString(Double.valueOf(cartFormList.get(i).getCount())*Double.valueOf(cartFormList.get(i).getRetailPrice())));
cartFormList.get(i).setSmallSumPrice(String.valueOf(smallSumPrice.setScale(2, BigDecimal.ROUND_HALF_UP)));
sum.add(smallSumPrice);
}
cartForm.setSumPrice(String.valueOf(sum));
对于BigDecimal这个数据类型可以通过String.valueof()方法将其转化为一个字符串型的数据。
上面的代码实现的是在后台控制显示两位有效数字的办法,下面是前台实现控制显示两位有效数字的办法:
function check() {
var el = document.getElementsByTagName('input');
var xiaojiLen = document.getElementsByName("xiaoji");
var xiaojiArray=new Array();
var xiaojiInt = 0;
var allCheckFlag = true;
for (var key in el) {
if (el[key].type == 'checkbox') {
if(el[key].id !="checkAllId") {
xiaojiArray[xiaojiInt] = el[key].checked;
xiaojiInt++;
if (el[key].checked == false) {
allCheckFlag = false;
}}}}
document.getElementById("checkAllId").checked = allCheckFlag;
var sumXiaoji = 0;
var sumCount = 0;
for(var xiaoji in xiaojiLen) {
if (xiaojiLen[xiaoji].tagName == 'SPAN') {
if(xiaojiArray[xiaoji] == true) {
sumXiaoji = sumXiaoji + parseFloat(xiaojiLen[xiaoji].innerText);
sumCount = sumCount + 1;
}}}
document.getElementById("sumCount").innerText = sumCount;
document.getElementById("sumMoney").innerText = sumXiaoji.toFixed(2); //这里的数据原本是小数点后 } //超过2位的数字
选中商品的结算时问题;当我们没有选中商品结算会出问题,所以需要对应核对cartIds的值即可,但是问题是在cartIds的值设定在cartForm的cartId中,然后通过一个sql文被使用。
SELECT cart.cart_id as cartId,
cart.guest_id as guestId,
commodity.unit as unit,
commodity.benchmark_price as benchmarkPrice,
commodity.picture_id as pictureId
FROM cart, commodity, supplier, brand
WHERE cart.commodity_id = commodity.commodity_id
AND commodity.supplier_id = supplier.supplier_id
AND commodity.brand_id = brand.brand_id
AND cart.status = #status#
AND cart.cart_id in($cartId$)
注意这里的cartId是一个“xxxx,xxxx,xxxxx,xxxxx”这样的一个字符串,这里可以直接通过这样的字符串作为一个查询条件集合,通过关键字 in 来找出对应选中的商品。
对日期的格式,按照一定格式将一个日期格式化:
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cartForm.setUpdateTime(dateformat.format(date));
对一个数字格式化为字符:通过的方法是String.format方法,格式化一个11位的数字
String commodityId0 = (String) (frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee0));
转化格式的时候需要知道之前的格式才能正确转换(前台传到后台出现乱码进行转码操作):
String commodityName=new String(goodsForm.getCommodityName().getBytes("iso8859-1"),"utf-8");