【问题标题】:How to refresh/access model from a JSP如何从 JSP 刷新/访问模型
【发布时间】:2013-03-06 15:03:03
【问题描述】:

谁能告诉我如何从 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>

将字符串添加到模型后,我无法刷新表单。有任何想法吗?

【问题讨论】:

  • 你能解释一下你到底想要做什么吗?您的 JSP 是与控制器关联的视图吗?是否要在 #uINewsSystemList 更改后动态更新表单?
  • 是的,一旦他们从 uINewsSystemList 中选择了一个项目,它应该适当地更改表单中的值。是的,JSP 是与控制器关联的视图

标签: java javascript spring jsp


【解决方案1】:

当您基于用户交互进行 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);
    });
);  

【讨论】:

  • 如果您的控制器已经设置为期望字符串作为视图名称,您还需要在 getSystemById 方法上使用 @ResponseBody 注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 2018-09-25
  • 2016-11-24
  • 2013-05-21
  • 2017-05-07
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多