java jsp 如何让页面提交,页面刷新后,保持下拉框中的值不变

1、把下拉框的值传到后台,存入session中

//前台
function changePageSize() {
        //获取下拉框的值
        var pageSize = $("#changePageSize").val();

        //向服务器发送请求,改变没页显示条数
        location.href = "${pageContext.request.contextPath}/product/findByNameAndDesc.do?selectValue="+pageSize+"&productName=${search_productName}&productDesc=${search_productDesc}&page=1&pageSize="+ pageSize;
    }
    
//后台
//多个模糊查询
    @RequestMapping("/findByNameAndDesc.do")
    public ModelAndView findByNameAndDesc(@RequestParam(name="productName",required = true) String productName,
                                          @RequestParam(name = "productDesc",required = true) String productDesc,
                                          @RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                          @RequestParam(name = "pageSize",required = true,defaultValue = "4")int size,
                                          @RequestParam(name = "selectValue",required = false)String selectValue,
                                          HttpSession session) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findByNameAndDesc(productName,productDesc,page,size);
        PageInfo pageInfo = new PageInfo(products);
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("search_productName",productName);
        mv.addObject("search_productDesc",productDesc);
        if (selectValue != null){
            session.setAttribute("selectValue",selectValue);
        }
        mv.setViewName("product-list1");
        return mv;
    }

2、在JSP页面中使用< c:if>< /c:if >判断

<select class="form-control" id="changePageSize" onchange="changePageSize()">
                                <c:if test="${selectValue=='1'}">
                                    <option selected>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                    <option>5</option>
                                </c:if>
                                <c:if test="${selectValue=='2'}">
                                    <option>1</option>
                                    <option selected>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                    <option>5</option>
                                </c:if>
                                <c:if test="${selectValue=='3'}">
                                    <option>1</option>
                                    <option>2</option>
                                    <option selected>3</option>
                                    <option>4</option>
                                    <option>5</option>
                                </c:if>
                                <c:if test="${selectValue=='4'}">
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option selected>4</option>
                                    <option>5</option>
                                </c:if>
                                <c:if test="${selectValue=='5'}">
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                    <option selected>5</option>
                                </c:if>

注意if标签的写法:<c:if test="${selectValue=='5'}">

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值