【问题标题】:Thymeleaf not sending object to controller from html where html element is being created Dynamically?Thymeleaf 没有从动态创建 html 元素的 html 将对象发送到控制器?
【发布时间】:2018-06-06 09:51:52
【问题描述】:

我有一个通过 JavaScript 创建的 HTML 元素。 我在同一个 html 元素中输入了 Thymeleaf 对象。这是我的 js 代码:-

$("#subcategoryNameDiv").append(''+
                         '<div class="form-group margindate">'+
                        '<label class="control-label col-sm-3 datesubscription nowrap">Category Name:</label>'+
                         '<div class="col-sm-9 ">'+
                             '<select id="categorySelect" th:field="${category}" class="date_dropdown date_input form-control" onchange="loadSubcategories()">'+
                             '</select>'+
                        '</div>'+
                     '</div>'+
                       '<div class="form-group margindate" >'+
                        '<label class="control-label col-sm-3 datesubscription nowrap">Subcategory Name:</label>'+
                         '<div class="col-sm-9 ">'+
                            '<select id="subcategorySelect" th:field="*{subcategory}" class="date_dropdown date_input form-control" required>'+
                             '</select>'+
                        '</div>'+
                     '</div>');

Thymeleaf is working properly while creating normal HTML element and sending Object to Controller but as per our Project's requirement this element is being generated when a specific option is Selected.

注意 - 在这里,loadSubcategories() 函数正常工作并将值附加到 HTML 元素 id="subcategorySelect"。但是在 Spring Boot Controller 上提交“类别”和“子类别”值之后为空。

【问题讨论】:

    标签: javascript java html spring-boot thymeleaf


    【解决方案1】:

    你需要在服务器端处理这个html,然后使用jquery追加。

    使用ajax获取数据的url

    @RequestMapping(value = "/abc", method = RequestMethod.GET, produces = "application/json")
    private ResponseBody test( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse )
    {
        Response response = new Response<String>();
        WebContext webContext = new WebContext( httpServletRequest, httpServletResponse, servletContext );
    
    
    
        response.setData( generateTemplate( webContext, viewResolver, "pathTohtmlSection") );
        response.setMessage( "Success" );
        response.setStatus(SUCCESS );
    
        return response;
    
    }
    

    将 html 生成为字符串

      public static String generateTemplate( WebContext ctx, ViewResolver viewResolver, String baseTemplate)
    {
    
        TemplateEngine engine = ( ( ThymeleafViewResolver ) viewResolver ).getTemplateEngine();
    
        String renderedHtml = engine.process( baseTemplate, ctx );
        return renderedHtml;
    
    }
    

    现在使用 jquery 如下。

     $.ajax({
      dataType: "json",
      url: "abc",
      data: data,
      success: function(response){
       $("#subcategoryNameDiv").html(response.data);
      };
    });
    

    【讨论】:

    • 你好,Supun 谢谢你的回复,但是,我有大约 22-25 个不同的元素,就像我的问题一样动态生成,所以在这种情况下很难得到它解决。有没有替代的问题。??另外,我在 JSP 中做了同样的事情,它在 JSP 中运行良好。
    • AFAIK,这是最好的解决方案。您可以发送多个 ajax 请求来呈现这些元素。
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 2019-09-29
    • 1970-01-01
    • 2022-01-02
    • 2015-09-04
    • 2015-07-02
    • 1970-01-01
    • 2019-05-20
    相关资源
    最近更新 更多