下拉选项页面 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。