Spring Mvc ModelAttribute与引用名称不工作? [英] Spring Mvc ModelAttribute with referencing name is not working?

查看:207
本文介绍了Spring Mvc ModelAttribute与引用名称不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一个控制器中创建不同的 @Entity 实体。

  @RequestMapping(value =create,method = RequestMethod.GET)
public String GET(Model model)throws InstantiationException,IllegalAccessException
{
Class& clazz = ???? //一个随机POJO被选中,我想使用POJO!

对象object = clazz.newInstance();
model.addAttribute(object,object);
returncreate;
}

@RequestMapping(value =create,method = RequestMethod.POST)
public @ResponseBody对象POST(@ModelAttribute(value =object
{
System.out.println(POST!got type:+ object.getClass()。getName());
return object;
}

在Post方法中, @ModelAttribute (value =object)对象对象



如果我将其更改为 @ModelAttribute )realType object 它工作完全正常。但我还不知道类型。



我认为 @ModelAttribute 对象,但显然不是。

解决方案

没有实际的模型对象名为 object 当你提交时,spring基于参数类型构造它,并将相应地绑定属性。



您有两个选择可以使其工作


  1. 会话

  2. 使用 @ModelAttribute 注释的方法


$ b b

如果这两个都不存在,spring将简单地查看方法参数,并使用反射来构造该类的实例。因此,在您的情况下,它只会是 Object ,然后绑定将失败。



会话

  @Controller 
@SessionAttributes(object)
public class MyController {...}

确保在完成后调用 对象上的 setComplete()方法。



使用 @ModelAttribute 注释方法



而不是在请求处理方法中创建和添加对象,而是为其创建一个特定的方法。

  @ ModelAttribute(object)
public Object formBackingObject(){
Class<?> clazz = ???? //一个随机POJO被选中,我想使用POJO!

对象object = clazz.newInstance();
return object;
}

此方法将在每个请求处理方法之前调用,构造将被用于绑定的对象。


I want to to create different @Entity entities within the same Controller.

@RequestMapping(value="create", method=RequestMethod.GET)
public String  GET(Model model) throws InstantiationException, IllegalAccessException
{
    Class<?> clazz = ????; // a Random POJO is chosen, i want to use POJOs!!

    Object object = clazz.newInstance();
    model.addAttribute("object", object);
    return "create";        
}

@RequestMapping(value="create", method=RequestMethod.POST)
public @ResponseBody Object POST(@ModelAttribute(value="object") Object object)
{
    System.out.println("POST! got type: " + object.getClass().getName());
    return object;
}

In the Post Method I get NULL for @ModelAttribute(value="object") Object object

If I change it to @ModelAttribute(value="object") realType object it is working perfectly fine. But I don't know the type yet.

I thought the @ModelAttribute can achieve this anyway with the name "object" but apparently not. What am I missing?

解决方案

There is no actual model object named object when you submit, spring constructs it based on the parameter type and will bind the properties accordingly.

You have 2 choices to make it work

  1. Store the object in the session
  2. Use a @ModelAttribute annotated method

If neither of these are there spring will simply look at the method argument and use reflection to construct an instance of that class. So in your case it will only be Object and after that binding will fail.

Store object in the session

@Controller
@SessionAttributes("object")
public class MyController { ... }

Make sure that when you are finished that you call the setComplete() method on a SessionStatus object.

Use a @ModelAttribute annotated method

Instead of creating and adding the object in a request handling method create a speficic method for it.

@ModelAttribute("object")
public Object formBackingObject() {
    Class<?> clazz = ????; // a Random POJO is chosen, i want to use POJOs!!

    Object object = clazz.newInstance();
    return object;
}

This method will be called before each request handling method, so that a newly fresh object is constructed which will be used for binding.

这篇关于Spring Mvc ModelAttribute与引用名称不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆