model.addAttribute 不适用于 jquery



Ajax 将数据发送到控制器。方法在单击按钮时使用该数据。我的方法model.addatribute.Alert发布null值。为什么?

我的控制器

@RequestMapping(value = "index", method = RequestMethod.POST)
@ResponseBody
public List<String> field(@RequestParam("tName") String name,
                          @RequestParam("dName") String dname, Model model) {
    String tn = name;
    String dn = dname;
    List<String> coname = new ArrayList<>();
    try {
        Connection mycon2 = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "1996");
        DatabaseMetaData metaData2 = mycon2.getMetaData();
        ResultSet res4 = metaData2.getColumns(dn, null, tn, "%");
        coname = new ArrayList<>();
        while (res4.next()) {
            String column_name = res4.getString("COLUMN_NAME");
            coname.add(column_name);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return coname;
}

索引网页

<div id="RightPanel" style="width: 200; height: 100%; float: right;">
  <table class="table table-striped">
    <input type='text' id='tablehead'/>
    <tr>
      <th>Column name</th>
    </tr>
    <tr th:each="name : ${cname}">
      <td> th:text="${name}"</td>
    </tr>
  </table>
</div>

索引 html 中的 JavaScript

<script th:inline="javascript">
    function onclk(a, b) {
        var search = {
            "tName": a,
            "dName": b
        };
        $.ajax({
            type: "POST",
            url: "http://localhost:8092/index",
            data: search,
            dataType: "json",
            success: function (data) {
                var modelAttributeValue = [[${coname}]];
                alert(modelAttributeValue);
            },
            error: function () {
                alert('got error');
            }
        });
    }
</script>

试试这个:

<script type="text/javascript" th:inline="javascript">
  /*<![CDATA[*/
   function onclk( a,b) {
        var search = {
            "tName" : a,
            "dName" :b
        }
        $.ajax({
        type: "POST",
        url:  "http://localhost:8092/index",
        data: search,
        dataType: "json",
        success: function (data) {
            var modelAttributeValue = [[${coname}]];
            alert(modelAttributeValue); },
        error: function(){
            alert('got error');
        }
    }); 
  /*]]>*/
</script>

最新更新