Java Spring Web MVC Cycle中formbackingobject和referencedata对象是如何操作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3588971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:14:31  来源:igfitidea点击:

How does formbackingobject and referencedata object operate in Spring Web MVC Cycle?

javaspringspring-mvc

提问by Rachel

I am new to Spring Framework and trying to understand the functionality of formBackingObjectand comparing it with referenceDatamethods, both these objects confuse me when am trying to relate them to HTTP Request Cycle from a Spring MVC point of view.

我是 Spring Framework 的新手,并试图了解formBackingObject的功能并将其与referenceData方法进行比较,当我试图从 Spring MVC 的角度将它们与 HTTP 请求周期相关联时,这两个对象都让我感到困惑。

I would really appreciate if someone can explain these two objects with reference to an example.

如果有人可以参考示例来解释这两个对象,我将不胜感激。

Q:What is the difference between formbacking objectand reference DataObject ?

问:formbacking objectreference DataObject 有什么区别?

采纳答案by Jacob Mattison

When you load a web page, you will want to pass data to it so that it can render.

当你加载一个网页时,你会想要将数据传递给它以便它可以呈现。

Some of this data will be purely informational, read-only; data that the page needs in order to render, but that isn't part of the current transaction. Examples: a list of countries to populate a drop-down, a list of possible products the user can buy.

其中一些数据将是纯粹的信息性、只读的;页面需要呈现的数据,但这不是当前事务的一部分。示例:要填充下拉列表的国家/地区列表,用户可以购买的可能产品列表。

Other data will be used for reading and writing: the contents of a form, say, must be populated with the current data, but can also be updated by the user. This set of data will be bound to the form; data sent to the page will render, data sent from the page (by the user) will cause an update. Examples: the user's name and address; the current order.

其他数据将用于读取和写入:例如,表单的内容必须用当前数据填充,但也可以由用户更新。这组数据将绑定到表单;发送到页面的数据将呈现,从页面(由用户)发送的数据将导致更新。示例:用户的姓名和地址;当前订单。

All of this data will typically be stored in one or more objects that the page needs access to.

所有这些数据通常会存储在页面需要访问的一个或多个对象中。

Objects containing informational data should be placed in the map provided by the referenceData()method. There can be as many such objects as you like.

包含信息数据的对象应放置在该referenceData()方法提供的地图中。可以有任意数量的此类对象。

The data to be bound to the form, the read/write data, must be contained in a single object. This object should be returned by the formBackingObject()method.

要绑定到表单的数据,即读/写数据,必须包含在单个对象中。这个对象应该由formBackingObject()方法返回。

I'll add that in more recent versions of Spring, annotations are used instead of these "built-in" methods.

我将补充一点,在较新版本的 Spring 中,使用注释代替这些“内置”方法。

回答by Arthur Ronald

Here goes formBackingObjectAPI

这里是formBackingObjectAPI

retrieve a backing object for the current form from the given request

从给定的请求中检索当前表单的支持对象

Some scenarios

一些场景

  • Avoids NullPointerException when traversing nested path
  • 遍历嵌套路径时避免 NullPointerException

...

...

 public class Command {

     private NestedClass nestedPath;

     // getter's and setter's

 }

Notice above nestedPath field has not been initialized. So if you try to retrieve its value on the form such as

注意上面的nestedPath 字段没有被初始化。因此,如果您尝试在表单上检索其值,例如

<spring:form path="nestedPath.someProperty"/> 

Because nestedPath has not been initialized, You will get NullPointerException when traversing some nestedPath property. To avoid NullPointException, overrides formBackingObject

因为nestedPath 没有被初始化,所以在遍历某些nestedPath 属性时会得到NullPointerException。为避免 NullPointException,覆盖 formBackingObject

 public Object formBackingObject(HttpServletRequest request) throws Exception {
     Command command = new Command();
     command.setNestedPath(new NestedClass());

     return command;
 }
  • Find-To-Update scenario
  • 查找更新方案

You submit the identifier of some command (usually by using GET method) to allow users to update it later

您提交某些命令的标识符(通常使用 GET 方法)以允许用户稍后更新它

public Object formBackingObject(HttpServletRequest request) throws Exception {
     if(request.getMethod().equalsIgnoreCase("GET")) {
         return commandRepository.findById(Integer.valueOf(request.getParameter("id")));
     }
 }

And referenceDataAPI

参考数据API

create a reference data map for the given request

为给定请求创建参考数据映射

You use referenceData to create data used by your form, for instance, a list of categories

您使用 referenceData 创建表单使用的数据,例如,类别列表

protected Map referenceData(HttpServletRequest request) throws Exception {
    return new ModelMap().addAttribute(categoryRepository.findAll()); 
}

On the form

在表格上

<label>Select category</label>
<form:select path="category">
    <form:option label="Select category" value=""/>
    <form:options items="${categoryList}"
                  itemLabel="WHICH_PROPERTY_OF_CATEGORY_SHOULD_BE_USED_AS_LABEL" 
                  itemValue="WHICH_PROPERTY_OF_CATEGORY_SHOULD_BE_USED_AS_VALUE"/>
</form:select>

回答by Vivek Goel

When you load a web page, you will want to pass data to it so that it can render.

当你加载一个网页时,你会想要将数据传递给它以便它可以呈现。

Some of this data will be purely informational, read-only; data that the page needs in order to render, but that isn't part of the current transaction. Examples: a list of countries to populate a drop-down, a list of possible products the user can buy.

其中一些数据将是纯粹的信息性、只读的;页面需要呈现的数据,但这不是当前事务的一部分。示例:要填充下拉列表的国家/地区列表,用户可以购买的可能产品列表。

Other data will be used for reading and writing: the contents of a form, say, must be populated with the current data, but can also be updated by the user. This set of data will be bound to the form; data sent to the page will render, data sent from the page (by the user) will cause an update. Examples: the user's name and address; the current order.

其他数据将用于读取和写入:例如,表单的内容必须用当前数据填充,但也可以由用户更新。这组数据将绑定到表单;发送到页面的数据将呈现,从页面(由用户)发送的数据将导致更新。示例:用户的姓名和地址;当前订单。

All of this data will typically be stored in one or more objects that the page needs access to.

所有这些数据通常会存储在页面需要访问的一个或多个对象中。

Objects containing informational data should be placed in the map provided by the referenceData() method.There can be as many such objects as you like.

包含信息数据的对象应放置在由 referenceData() 方法提供的地图中。可以有任意数量的此类对象。

The data to be bound to the form, the read/write data, must be contained in a single object. This object should be returned by the formBackingObject() method.

要绑定到表单的数据,即读/写数据,必须包含在单个对象中。该对象应由 formBackingObject() 方法返回。