简体   繁体   中英

How to refresh/access model from a JSP

Can someone please let me know how to access a Model from a JSP.

This is my controller:

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

JSP code:

$('#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>

I cant get the form to refresh once i've added the string to the model. Any ideas?

When you make an ajax call based on user interaction the flow you are invoking has nothing to do with the original JSP you used to render the page.

You can either have the getSystemById method totally re-render the page (probably via form submit/POST) or you can alter your example code to actually return the necessary data to make the changes via JavaScript. Since you mentioned you are looking for dynamic updating the changes could look something like this:

@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.)
} 

The client ajax call will need to be setup to have a success function where you use the returned data to update the ui.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM