Spring MVC的AJAX再重新描绘的用户界面重复用户界面、再重新、Spring、MVC

2023-09-10 21:00:06 作者:乍见之欢不如处之不厌

首先我主要是用 JSF 背景。

First of all I am mainly with a JSF background.

我最近开始学习 Spring MVC的。有一件事情困扰我的是阿贾克斯重新渲染时,使用 Spring MVC的 JQuery的

I have started recently studying Spring MVC. One thing that is bothering me is the ajax re-rendering when using Spring MVC and JQuery.

让我们想象一下,我已经定义了一个复杂的形式,我的 people.jsp 查看:

Let's imagine that I have defined a complex form in my people.jsp view:

<c:forEach var="person" items="${people}">
   <table>
       <tr class="trPersonClass">...</tr>
       <tr>...</tr>
   </table>
</c:forEach>

和我有一个刷新下面的按钮。当单击刷新按钮,我想与 AJAX 重新呈现。

And I have a refresh button below. When the refresh button is clicked I want with ajax the people to rerender.

function refreshButtonClicked() {
    $.ajax({
       type: "GET",
       url: "ajax/loadPeople.do"
    }).done(function( msg ) {   
        //WHAT SHOULD I DO HERE???            
    } 
});

所以,我该怎么办呢?我已经定义了人们怎样呈现愿与 C:的forEach 标记在我的的jsp 。我不想再说了。我不想重复的用户界面code在两个地方 - 在 JQuery的完成回调,以 JSP 标记在我的意见。这在我看来是容易出错的。

So what I should do there? I have already defined how the people rendering should like with the c:forEach tag in my jsp. I don't want to repeat it again. I don't want to duplicate user interface code at both places - in the JQuery done callback and with JSP tags in my views. This is error prone in my opinion.

请好心给我解释一下什么,我在这里失踪。

Please explain me kindly what I am missing here.

推荐答案

首先,Spring MVC的非常灵活。您可以返回一个视图引擎生成的HTML后台处理程序,你可以有一个处理程序返回的JSON / XML / ProtocolBuffers /等。和使用客户端呈现引擎如髭等,以在浏览器中显示该页,也可以在同一应用程序两者结合起来。

First of all, Spring MVC is very flexible. You can have backend handlers that return HTML generated by a view engine, you can have a handlers that returns JSON/XML/ProtocolBuffers/etc. and use client side rendering engines like Mustache etc. to display the page in the browser, or you can combine the two in the same application.

如果你想生成的HTML服务器上,Spring MVC的,您可以使用不同的模板引擎来做到这一点。您可以使用JSP,Freemarker的,速度等。为了做到这一点,它使用一个ViewResolver抽象,并在code你只需要处理的ModelAndView API。

If you want to generate HTML on the server, Spring MVC allows you to use different template engines to do that. You can use JSP, Freemarker, Velocity etc. In order to do that, it uses a ViewResolver abstraction, and in your code you only have to deal with the ModelAndView API.

在解析器的更多细节可以在这里找到: HTTP:/ /static.springsource.org/spring/docs/3.0.x/reference/mvc.html

More details on ViewResolver can be found here: http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html

根据你的问题听起来好像你有使用JSP创建HTML服务器端后端。为了只更新表,当用户点击一个刷新按钮不会重新加载整个页面,例如,你可以有一个处理程序,只返回HTML表像这样:

Based on your question it sounds like you have a backend that use JSP to create the html server side. In order to update only the table and not reload the entire page when a user clicks a refresh button, you could for example have a handler that returns only the html table like so:

@RequestMapping("/table")
public ModelAndView renderTable() {
   List<People> people = peopleService.findAllPeople();
   return new ModelAndView("/people", "people", people);
}

我也假设你有另一个处理程序返回所在的表是通过AJAX嵌入主页。

I'm also assuming you have another handler that returns the main page where the table is embedded via ajax.

<body>
    ...
    /* Content of div will be populated via ajax*/
    <div id="myTableContainer" />
    ...
</body>

的JavaScript这将是这样的:

The javaScript for this would be something like:

$(function() {

  var myTableContainer = $("#myTableContainer");

  var renderTable = function(container) { 
    $.get("/table", function(data) {
      container.empty().html(data);
    }
  };

  /* This is called on document ready */
  renderTable(myTableContainer);

  /* Use the same renderTable function when the refresh button is clicked */
  $("#refreshButton").click(function() {
      renderTable(myTableContainer);
  });
}