您好,登錄后才能下訂單哦!
下拉選項頁面 common/options.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:forEach var="o" items="${options}"> <option value="${o.optionKey}" ${selected == o.optionKey?"selected":"" }>${o.optionValue}</option> </c:forEach>
頁面調用代碼
<select name="brand_id"> <c:set value="${pageParam.brand_id}" var="selected" /> <c:import url="/options/brand" /> </select>
Controller
import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * @author liujun * @since 2018/1/19. * @see Controller * 下拉選項數據列表 */ @Controller @RequestMapping(value = "/options") public class OptionsController { @Autowired OptionService optionService; @RequestMapping(value = "/goodsType") public String goodsTypeOtions(HttpServletRequest request ,Model model){ String condition = request.getParameter("condition"); String order = request.getParameter("order"); Map<String,Object> param = new HashMap<String, Object>(); if(StringUtils.isNotBlank(condition)){ param.put("condition",condition); } if(StringUtils.isNotBlank(condition)){ param.put("order",order); } model.addAttribute("options",this.optionService.goodsTypeOption(param)); return "common/options"; } @RequestMapping(value = "/brand") public String brandOtions(HttpServletRequest request ,Model model){ String condition = request.getParameter("condition"); String order = request.getParameter("order"); Map<String,Object> param = new HashMap<String, Object>(); if(StringUtils.isNotBlank(condition)){ param.put("condition",condition); } if(StringUtils.isNotBlank(condition)){ param.put("order",order); } model.addAttribute("options",this.optionService.brandOption(param)); return "common/options"; } }
service接口
import java.util.List; import java.util.Map; /** * @author liujun * @since 2018/1/19. */ public interface OptionService { public List<Map<String,Object>> goodsTypeOption(Map<String,Object> param); public List<Map<String,Object>> brandOption(Map<String,Object> param); }
service實現類
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 下拉選項數據列表 * @author liujun * @since 2018/1/19. * @see OptionService * @see Service * */ @Service public class OptionServiceImpl implements OptionService{ @Autowired GenericMybatisDao dao; @Override public List<Map<String, Object>> goodsTypeOption(Map<String, Object> param) { return findData(OptionQueryEnum.GOODS_TYPE_OPTION.getQuery(),param); } @Override public List<Map<String, Object>> brandOption(Map<String, Object> param) { return findData(OptionQueryEnum.BRAND_OTION.getQuery(),param); } public List<Map<String, Object>> findData(String querySql,Map<String,Object> param){ StringBuilder sql = new StringBuilder(querySql); Map<String,Object> map = new HashMap<String, Object>(); if(param != null){ if(param.get("condition")!=null){ String[] conditions = param.get("condition").toString().split(","); for(String condition : conditions){ sql.append(" and "); sql.append(condition); } } if(param.get("order")!=null){ sql.append(" order by "); sql.append(param.get("order")); } } map.put("sqltext",sql); return this.dao.list(map); } enum OptionQueryEnum{ GOODS_TYPE_OPTION(" select cat_id as optionKey,cat_name as optionValue from ecs_goods_type where 1=1 "), BRAND_OTION(" select brand_id as optionKey,brand_name as optionValue from ecs_brand where 1=1 "); private String query; private OptionQueryEnum(String query){ this.query = query; } public String getQuery() { return query; } public void setQuery(String query) { this.query = query; } } }
mybatisDao
import java.util.List; import java.util.Map; /** * Created by liujun on 2018/1/10. */ public interface GenericMybatisDao { /** * 查詢數據通用接口 * @param param 查詢數據參數 * @return 查詢結果集 */ public List<Map<String,Object>> list(Map<String,Object> param); /** * 保存數據通用接口 * @param param 參數 * @return 修改記錄數量 */ public Integer update(Map<String,Object> param); /** * 保存數據通用接口 * @param param 參數 * @return 修改記錄數量 */ public Integer insert(Map<String,Object> param); /** * 刪除數據通用接口 * @param param 參數 * @return */ public Integer delete(Map<String,Object> param); }
mybatis Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jeff.tianti.dao.mybatis.GenericMybatisDao"> <select id="list" statementType="PREPARED" resultType="java.util.LinkedHashMap" parameterType="map"> ${sqltext} </select> <update id="update" statementType="PREPARED" parameterType="map"> ${sqltext} </update> <insert id="insert" statementType="PREPARED" parameterType="map"> ${sqltext} </insert> <delete id="delete" statementType="PREPARED" parameterType="map"> ${sqltext} </delete> </mapper>
實際效果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。