Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用@modeldattribute的目的是什么_Spring_Model View Controller_Spring Mvc - Fatal编程技术网

Spring 使用@modeldattribute的目的是什么

Spring 使用@modeldattribute的目的是什么,spring,model-view-controller,spring-mvc,Spring,Model View Controller,Spring Mvc,嗨,我想知道使用@modeldattribute 为什么使用 @RequestMapping(value=”“) 公共字符串索引(@modeldattribute(“before”)在b之前){ System.out.println(b.getbeforesting()); 返回“主页/索引”; } @ModelAttribute(“之前”) 在……之前公开{ 返回新的Before(); } 我什么时候可以用 @RequestMapping(value=”“) 公共字符串索引(){ b之前=新

嗨,我想知道使用
@modeldattribute
为什么使用

@RequestMapping(value=”“)
公共字符串索引(@modeldattribute(“before”)在b之前){
System.out.println(b.getbeforesting());
返回“主页/索引”;
}
@ModelAttribute(“之前”)
在……之前公开{
返回新的Before();
}
我什么时候可以用

@RequestMapping(value=”“)
公共字符串索引(){
b之前=新的之前();
System.out.println(b.getbeforesting());
返回“主页/索引”;
}

@modeldattribute
用于将内容填充到MVC模型中,以便在视图中后续使用。它不是将内容放入模型中的唯一方法(也可以通过代码直接完成),但它是添加
@RequestMapping
方法参数或方法返回值等的方便方法

在同一控制器类中调用的每个
@RequestMapping
方法之前,调用带有
@modeldattribute
注释的方法。这通常用于对表单的只读组件(例如选择列表的值)进行弹出式上载。()-或用于在绑定传入请求值之前准备bean(例如从数据存储加载)

所以,在你的第一个例子中

@ModelAttribute("before")
public Before before() {
  return new Before();
}
@RequestMapping(value="")
public String index(@ModelAttribute("before") Before b) {
    System.out.println(b.getBeforeString());
    return "home/index";
}
。。。将首先调用
before()
方法,并在模型中放置一个名为“before”的
before
实例。接下来,将调用
index(…)
方法,并接收与
Before
实例完全相同的
实例(因为它还使用模型属性名称“Before”)——但现在将使用来自请求的值填充它,其中请求参数名称映射到Before属性名称。(旁白:这可能不是你想要的。如果这真的是你想要的,你应该考虑添加一个<代码> BindingResult <代码>作为索引方法的第二个ARG来捕捉任何绑定错误(弹头17)。
在此之后,
索引(…)
中的
@modeldattribute
注释还将导致
Before
实例以名称“Before”呈现给模型,以便在下一个视图中使用:“/home/index”

(值得注意的是:如果您根本没有
before()
方法,您的
index()
方法将接收一个使用其无参数构造函数创建的
before
实例,然后使用传入的请求值进行上传。随后,它仍会将其放入名为“before”的模型中。)供下一个视图使用。)

您的第二个代码段

@RequestMapping(value="")
public String index() {
    Before b = new Before();
    System.out.println(b.getBeforeString());
    return "home/index";
}
。。。实际上根本没有将
之前的
添加到模型中,因此它在视图中不可用。如果您想在此处的模型中使用:

@RequestMapping(value="")
public String index(Model uiModel) {
    Before b = new Before();
    System.out.println(b.getBeforeString());
    uiModel.addAttribute("beforeName", b);  // will add to model with name "beforeName"
    uiModel.addAttribute(b)    // will add to model with a default name (lower-cased simple type name by convention), ie: "before"
    return "home/index";
}

HTH

@modeldattribute
用于将内容填充到MVC模型中,以便在视图中后续使用。它不是将内容放入模型中的唯一方法(也可以通过代码直接完成),但它是添加
@RequestMapping
方法参数或方法返回值等的方便方法

在同一控制器类中调用的每个
@RequestMapping
方法之前,调用带有
@modeldattribute
注释的方法。这通常用于对表单的只读组件(例如选择列表的值)进行弹出式上载。()-或用于在绑定传入请求值之前准备bean(例如从数据存储加载)

所以,在你的第一个例子中

@ModelAttribute("before")
public Before before() {
  return new Before();
}
@RequestMapping(value="")
public String index(@ModelAttribute("before") Before b) {
    System.out.println(b.getBeforeString());
    return "home/index";
}
。。。将首先调用
before()
方法,并在模型中放置一个名为“before”的
before
实例。接下来,将调用
index(…)
方法,并接收与
Before
实例完全相同的
实例(因为它还使用模型属性名称“Before”)——但现在将使用来自请求的值填充它,其中请求参数名称映射到Before属性名称。(旁白:这可能不是你想要的。如果这真的是你想要的,你应该考虑添加一个<代码> BindingResult <代码>作为索引方法的第二个ARG来捕捉任何绑定错误(弹头17)。
在此之后,
索引(…)
中的
@modeldattribute
注释还将导致
Before
实例以名称“Before”呈现给模型,以便在下一个视图中使用:“/home/index”

(值得注意的是:如果您根本没有
before()
方法,您的
index()
方法将接收一个使用其无参数构造函数创建的
before
实例,然后使用传入的请求值进行上传。随后,它仍会将其放入名为“before”的模型中。)供下一个视图使用。)

您的第二个代码段

@RequestMapping(value="")
public String index() {
    Before b = new Before();
    System.out.println(b.getBeforeString());
    return "home/index";
}
。。。实际上根本没有将
之前的
添加到模型中,因此它在视图中不可用。如果您想在此处的模型中使用:

@RequestMapping(value="")
public String index(Model uiModel) {
    Before b = new Before();
    System.out.println(b.getBeforeString());
    uiModel.addAttribute("beforeName", b);  // will add to model with name "beforeName"
    uiModel.addAttribute(b)    // will add to model with a default name (lower-cased simple type name by convention), ie: "before"
    return "home/index";
}

HTH

因此,如果
@modeldattribute
将对象传递给视图,我如何在视图中读取此对象、此方法?模型中的所有内容都带有名称。在
前面加上
@modeldattribute(“before”),名称为
前面的
(小写)。在JSP中,使用EL
${before}
访问此对象,使用EL
${before.beforesting}
访问其道具,因此如果
@modeldattribute
将对象传递给视图,我如何在视图中读取此对象、此方法?模型中的所有内容都带有名称。在
前面加上
@modeldattribute(“before”),名称为
前面的
(小写)。在JSP中,使用EL
${before}
访问此文件,并使用
${before.beforesting}