0

我是 Spring MVC 的新手。我想将模型bean附加到绑定值并将其传递给控制器​​的表单。所以我做了以下方式

在jsp中

<form:form modelattribute="model">
<form:input path="var1"/>
</form:form>

在控制器中

pulic void method(@modelattribute("model")Bean bean)
{

//my code
}

但是当我访问表单时它在渲染jsp时抛出错误作为异常,名称模型不存在这样的bean

如何解决这个问题?帮我

4

5 回答 5

2

假设您的模型类如下所示:

public class MyModel{
    private String propOne;
    private String porpTwo;

    /*Skipping getters and setters*/
}

使用@ModelAttribute将用户输入映射到您的表单 bean:

@RequestParam("/myPage")
public String myController(@ModelAttribute MyModel myModel){
    /*Do your processing here*/
}

jsp页面上,只需为您的输入字段提供与要映射的 bean 内的属性相同的名称(Html 属性:名称):

<form:input name="propOne" class="xyz" />
<form:input name="propTwo" class="xyz" />

这样做就完成了您的 bean 映射。

于 2013-06-21T17:35:55.880 回答
0

您需要在渲染页面之前保存模型。

uiModel.addAttribute("model", new Bean());
于 2013-06-21T17:09:26.740 回答
0

在 Spring MVC 中,最好在控制器中提供模型的方法上使用 @ModelAttribute 注释。这将在您的 JSP 被渲染之前被调用并自动添加到模型中。

像这样的东西

@ModelAttribute
public Model model(){
  return new Model();
}

我建议你好好阅读 Spring MVC文档

于 2013-06-21T17:16:18.767 回答
0

您必须在 GET 请求期间将表单实例添加到模型中

@RequestMapping(value = "/foo", method = RequestMethod.GET)
public String handler(final Model uiModel)
        uiModel.addAttribute("model", new Bean());

//做一些事情并返回一个视图路径,可能}

并在处理程序方法中处理 POST 请求

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String handler(final Bean form)
        // process your form bean here and return a view path, probably
}

文档可在此处获得:http ://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

于 2013-06-21T17:24:51.833 回答
-1

请看一下 Vaibhav 方法,我已经对其进行了编辑,现在它工作正常

于 2014-02-01T07:32:20.050 回答