0

当我的网页发生更改时,我尝试填充两个单独的 HTML 对象 - 一个是下拉列表,一个是表单。

所以在我的 JSP 代码中我有这个:

$('#uSystemList').change(function() {
    $.get("/live-application/systemById", {systemid: $("#uSystemList").val()}, displayChangedSystemResults, "html");
});

<script type="text/javascript">
function displayChangedSystemResults(html){
    $('#uList').empty();
    $('#uList').append(html);
}
</script>

在Java方面我有这个:

@RequestMapping(value = "/systemById", method = RequestMethod.GET)
public void getSystemById(@RequestParam("systemid") String systemid, OutputStream outputStream) throws IOException {
    StringBuilder refreshHtml = new StringBuilder();
    try {
        String html = "";
        System system = service.getSystemById(systemid));
        for (Value value: inewsSystem.getValues()) {
            html = html + "<option value='" + value.getId() + "'> " + value.getName() + "</>";
            }
        } 
        refreshHtml.append(html );
        outputStream.write(refreshHtml.toString().getBytes());
        outputStream.flush();
    } catch (Exception e) {
        outputStream.flush();
    }
}

所以这填充了我的uList下拉列表 - 但我如何告诉它填充其他内容(例如表单,但可以是其他任何内容作为示例......)。OutputStream似乎每次更改只能让我填充一个对象。

4

2 回答 2

1

在您的情况下,发送两个 ajax 请求并有两个弹簧控制器来处理每个请求并不是一个坏主意。随后,在 JSP 页面中,您将需要两个 javascript 回调来填充不同的 HTML 部分。

或者,如果您坚持只发出一个 AJAX 请求;一种解决方法是让 spring 控制器返回一个 JSON,其中包含用于填充这两个 HTML 部分的数据。但是使用这种方法显然需要更多的 javascript 工作。

于 2013-03-06T10:06:10.247 回答
0

这是 Javascript 而不是 JSP :)。您需要更新:

<script type="text/javascript">
function displayChangedSystemResults(html){
    $('#uList').empty();
    $('#uList').append(html);
    $('#someOtherElement').empty();
    $('#someOtherElement').append(html);
}
</script>
于 2013-03-06T09:59:26.300 回答