简体   繁体   English

Spring MVC - 如何在部分绑定到表单时不丢失字段值

[英]Spring MVC - how not to lose field values when binding to a form partially

I want to make an update form for a bean X. This bean lets say it has fields A, B, C, D. In my form I want to update just fields A, B and let C and D untouched. 我想为bean X创建一个更新表单。这个bean可以说它有字段A,B,C,D。在我的表单中,我想只更新字段A,B并让C和D不变。 Is there a way to bind bean X to the update form with just fields A and B, so that when i submit the form C and D won't be changed ? 有没有办法将bean X绑定到只有字段A和B的更新表单,这样当我提交表单时C和D将不会被更改?

I know i can add hidden fields for C and D but what if these are not primitive fields, they are other beans or collections. 我知道我可以为C和D添加隐藏字段但是如果这些不是原始字段,它们是其他bean或集合。

I know another solution would be to create a XUpdateBean that will have only fields A and B and after form submit copy the fields from XUpdateBean to my X bean. 我知道另一个解决方案是创建一个只有字段A和B的XUpdateBean,并在表单提交后将XUpdateBean中的字段复制到我的X bean。

Is there another way to this update better in Spring 3 MVC? 在Spring 3 MVC中有更好的更新方式吗?

You could have a command-Object/form-barking-Bean that contains only the fields you need. 你可以拥有一个只包含你需要的字段的命令-Object / form-b​​arking-Bean。

In the controller you have to load the bean X, and need to update its fields with the one from the commandObject. 在控制器中,您必须加载bean X,并且需要使用commandObject中的字段更新其字段。

May you can also think of not having an extra class for the commandObject, instead use class BeanX. 也许您还可以想到没有为commandObject添加额外的类,而是使用类BeanX。 But of course you need two instances of BeanX, one for the commandObject and one for bean x. 但是当然你需要两个BeanX实例,一个用于commandObject,一个用于bean x。

The correct way, in my mind, especially when dealing with Optimistic Concurrency Control (@Version) is to store the model attribute in session temporarily. 在我看来,正确的方法,特别是在处理乐观并发控制(@Version)时,暂时将模型属性存储在会话中。

@Controller
@SessionAttributes("x")
public class MyController {

    @Autowired
    private XRepository xRepository;

    @InitBinder
    void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields("id", "c", "d"); 
    }

    @RequestMapping("/x/{id}")
    String myForm(@PathVariable("id") long id, Model model) {

        X x = xRepository.findOne(id);
        model.addAttribute("x", x);

        return "x-edit";
    }

    @RequestMapping(value="/x/{id}", method= RequestMethod.POST)
    String save(@PathVariable("id") long id, @ModelAttribute X x, SessionStatus sessionStatus) {

        xRepository.save(x);

        sessionStatus.setComplete();
        return "x-edit";
    }
}

Sorry i don't know SpringMVC so my answer may be wrong. 对不起,我不知道SpringMVC,所以我的回答可能是错的。

With another binding framework called Stripes, we usually "hydrate" the data before the binding. 使用另一个名为Stripes的绑定框架,我们通常在绑定之前“保湿”数据。 This means you first load the bean from the db, and only then you'll bind the A and B value to it! 这意味着您首先从db加载bean,然后才将A和B值绑定到它! But it still has C and D original values since the bean comes from the DB (usually a JPA entity). 但它仍然具有C和D原始值,因为bean来自DB(通常是JPA实体)。 Thus you don't need hidden C and D fields! 因此,您不需要隐藏的C和D字段!

It seems possible with SpringMVC: Spring MVC 3.0: How do I bind to a persistent object SpringMVC似乎可能: Spring MVC 3.0:我如何绑定到持久对象

Notice that you could also load bind to a "non-DB bean" like you actually do, (so you'll have C and D fields empty i guess if you don't use hidden fields). 请注意,您也可以像实际那样加载绑定到“非DB bean”(因此,如果您不使用隐藏字段,那么您将使C和D字段为空)。 Then you can simply load the bean you want to modify from the DB, and do a merge between the binded bean, and the db bean, of the fields you want (here, you'll merge only A and B so that the C and D fields in the DB bean won't be modified) 然后你可以简单地从DB加载你想要修改的bean,并在你想要的字段的binded bean和db bean之间进行合并(这里,你只会合并A和B,以便C和DB bean中的D字段不会被修改)

You can find some interesting stuff here about data binding. 你可以在这里找到一些关于数据绑定的有趣内容。 For me in some tricky cases, binding directly to DB objects can be dangerous: How to use a binding framework efficiently 对于我来说,在一些棘手的情况下,直接绑定到DB对象可能很危险: 如何有效地使用绑定框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC-将空白表单值绑定到原始Bean字段 - Spring MVC - Binding Blank Form Values to Primitive Bean Fields 如何在PHP表单上不丢失数组字段文本? - How to not lose an array field text on PHP form? 将绑定自动填充列表发布到表单Spring MVC - Issue Binding AutoPopulating List to a form Spring MVC Spring MVC编辑对象时如何处理绑定 - Spring MVC how to handle binding when editing objects Spring MVC和表单绑定:如果子对象的所有字段为空,如何告诉Spring不要实例化子对象? - Spring MVC and form binding : how to tell Spring not to instantiate child objects if all their fields are empty? Spring MVC如何将表单值转换为对象? - How does Spring MVC turn form values into objects? 如何在形式的commandObject / modelAttribute中的spring mvc中传递jstl值 - how to pass jstl values in spring mvc in commandObject/modelAttribute of form 从String转换为Spring MVC表单的自定义对象数据绑定? - Converting from String to custom Object for Spring MVC form Data binding? Spring MVC Form将一组两个或多个字段绑定到一个对象列表中 - Spring MVC Form binding a group of two or more fields into a list of objects 为什么在发布带有file_field的表单时丢失对“提交”按钮的跟踪 - Why do I lose trace of the submit button when I post a form with a file_field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM