如何从JSP刷新/访问模型

How to refresh/access model from a JSP

本文关键字:访问 模型 刷新 JSP      更新时间:2023-09-26

有人能告诉我如何从JSP访问模型吗?

这是我的控制器

@RequestMapping(value = "/systemById", method = RequestMethod.GET)
public void getSystemById(Model model, OutputStream outputStream) throws IOException {
     model.addAttribute("fSystemName", "Test name");
     name = system.getName();
} 
JSP代码:

$('#uINewsSystemList').change(function() {
    $.get("/application/systemById");
);  
<form:form id="systemForm" commandName="systemForm">
<tr>
    <td valign="top"><form:input path="fSystemName" value="${fSystemName}" size="20" />&nbsp;</td>                      
</tr>

我不能得到的形式刷新,一旦我添加了字符串模型。什么好主意吗?

当您基于用户交互进行ajax调用时,所调用的流与用于呈现页面的原始JSP无关。

您可以使用getSystemById方法完全重新呈现页面(可能通过表单提交/POST),或者您可以更改示例代码以实际返回必要的数据,以便通过JavaScript进行更改。既然你提到你正在寻找动态更新,更改可能看起来像这样:

@RequestMapping(value = "/systemById/${id}", method = RequestMethod.GET)
public String getSystemById(@PathVariable String id) throws IOException {
     //lookup new system data by id
     Model model = someService.getModelById(id);
     return model.getName(); //you can return more than just name, but then you will need some sort of conversion to handle that data (json, xml, etc.)
} 

客户端ajax调用需要设置一个成功函数,其中使用返回的数据来更新ui。

$('#uINewsSystemList').change(function() {
    var id = $(this).val();
    $.get("/application/systemById/" + id, function(returnedData){
        //use returnedData to refresh the ui.
        $('selectorForSystemNameField').val(returnedData);
    });
);