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'}">

JSP中实现下拉框选择后通过Ajax技术刷新页面并保留所选,通常的做法如下: 1. **HTML部分**: 创建一个`<select>`标签,包含你想让用户选择的选项,并给下拉框的选择项添加一个`value`属性,表示其数据ID。 ```html <select id="mySelect" onchange="updatePage(this)"> <option value="">请选择</option> <option value="1">选项A</option> <option value="2">选项B</option> <!-- 更多选项 --> </select> ``` 2. **JavaScript部分 (通常是jQuery或其他库)**: 使用`onchange`事件监听下拉框的变化,然后发送一个Ajax请求到服务器更新页面内容。同时,将当前选择的作为参数传递。 ```javascript function updatePage(selectElement) { var selectedValue = selectElement.value; // 获取选中的 $.ajax({ url: "yourActionServlet", // 替换为你的后端处理动作的URL type: "POST", data: { option: selectedValue }, // 将序列化为参数 success: function(response) { // 页面成功更新后,可以操作document.getElementById('targetDiv')替换内容 document.getElementById('targetDiv').innerHTML = response; }, error: function(xhr, status, error) { console.error("Error: ", error); } }); } ``` 3. **后端处理 (如Servlet)**: 接收到请求后,根据传来的进行相应的操作,并返回新的页面内容或者只是部分动态生成的内容。 ```java // YourServlet.java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String selectedOption = request.getParameter("option"); // 根据selectedOption进行业务处理,例如从数据库查询结果 String content = getContentForSelectedOption(selectedOption); // 写入响应 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print(content); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值