java - 通过 Java 从 Javascript 更改填充多个 HTML 语句

标签 java html ajax jsp

当我的网页发生更改时,我尝试填充两个单独的 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 似乎只允许我在每次更改时填充一个对象。

最佳答案

就您而言,发送两个 ajax 请求并使用两个 spring Controller 来处理每个请求并不是一个坏主意。随后,在 JSP 页面中,您将需要两个 javascript 回调来填充不同的 HTML 部分。

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

关于java - 通过 Java 从 Javascript 更改填充多个 HTML 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243998/

相关文章:

css - DIV 内容溢出到页脚,使页脚在页面上向上移动

php - 使用 AJAX 和 PHP 更新图像 src

Java Swing : KeyBinding for KEY_TYPED event

java - 如何构建一个正则表达式来匹配包含子字符串中所有字符的任何字符串?

java - Jtree:将背景颜色设置为最大单双击节点

javascript - 在 Ajax Success 中引用父 Div

php - 为什么我的 Ajax 发布脚本不起作用?

java - 添加用 Java 编译的依赖库总是安全的吗?

javascript - 在包含的 html 文件上定位图像

javascript - 谷歌地图不支持API