有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在SpringMVC中用JSP初始化菜单项选择表单数据的最佳方法

我有一些菜单正在打电话。jsp文件,我的菜单相关代码如下:

<li class="nav-item dropdown">
 <a class="nav-link dropdown-toggle text-white" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">menu2</a>
    <div class="dropdown-menu ">
        <a href="drop1.jsp" class="dropdown-item">drop1</a>
        <a href="drop2.jsp" class="dropdown-item">drop2</a>
        <a href="drop3.jsp" class="dropdown-item">drop3</a>
    </div>
</li>

要求:[1]当用户选择任何菜单项时,例如:“drop1”,我想从数据库中获取一些数据,以及那些获取的数据,我想在drop1中显示。jsp
注意:我使用的是spring security5。3.2、JSP、Java8和Spring 5.2.6


共 (2) 个答案

  1. # 1 楼答案

    我没有得到你的第一个要求

    大约2秒,你可以使用一个简单的Ajax调用控制器,并在同一页上返回响应消息,该页将不会得到刷新,你将在同一页上,所有stuf都在后台完成

  2. # 2 楼答案

    我使用的是Ajax,它在页面加载时调用spring controller,例如:
    在我的jsp页面中:-

    <script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            type: 'GET',
            url: "categories",
            success: function(data){
                var slctcat=$('#selectdCategory'), option="";
                slctcat.empty();
                option= option + "<option value=''>- Select Catagory -</option>";
                for(var i=0; i<data.length; i++){
                    option = option + "<option value='"+data[i].categoryId + "'>"+data[i].categoryName + "</option>";
                }
                slctcat.append(option);
            },
            error:function(){
                alert("error");
            }
    
        });
    });
    </script>
    

    在我的控制器中:

    @GetMapping(value = "/categories")
    public @ResponseBody  List<ProductCategoriesEntity> getAllCategories() {
        productCategories= productCategoriesService.getAllProductCategories();
     return productCategories;
    }