Java SpringJSP:modelAttribute为的Spring表单有问题&引用;和路径=”&引用;
当只有一个字符串要传递给控制器时,使用modelAttribute=“”标记和path=“”标记对我来说没有意义。然而,当一个表单有多个文本框时,为它们建立一个对象模型是有意义的。这样,modelAttribute标记表示对象,即“Employee”,路径标记表示字段,即“firstName”、“lastName”、“salary” 当你只想传递一个字符串时,你会怎么做?我不应该创建一个带有“Key”字段的“Key”类,其中包含getKey()和setKey(),或者任何类似的疯狂行为,只是为了将字符串传递给控制器方法,对吗?在这种情况下,公约是什么 如果我在加载页面时只执行Java SpringJSP:modelAttribute为的Spring表单有问题&引用;和路径=”&引用;,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,当只有一个字符串要传递给控制器时,使用modelAttribute=“”标记和path=“”标记对我来说没有意义。然而,当一个表单有多个文本框时,为它们建立一个对象模型是有意义的。这样,modelAttribute标记表示对象,即“Employee”,路径标记表示字段,即“firstName”、“lastName”、“salary” 当你只想传递一个字符串时,你会怎么做?我不应该创建一个带有“Key”字段的“Key”类,其中包含getKey()和setKey(),或者任何类似的疯狂行为,只是为了
model.addAttribute(“key”,”)
,我会得到:
org.springframework.beans.NotReadablePropertyException: Invalid property 'key'
of bean class [java.lang.String]: Bean property 'key' is not readable or has an invalid
getter method: Does the return type of the getter match the parameter type of the setter?
如果我删除modelAttribute=“key”标记,我会得到:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for
bean name 'command' available as request attribute
JSP
<form:form method="post" action="myAction" modelAttribute="key">
<td>
<form:input path="key" value="${myValue}"/>
<input type="submit" value="Submit"/>
</td>
</form:form>
提交表单时,向控制器方法传递单个字符串,而不必创建新类的约定是什么?我刚想到这个主意,但我真的不知道一般来说是否可取。毕竟,这只是一个你想要避免的过程的内联版本。不管怎样,我说: 在备份bean中,向模型中添加字符串,如下所示:
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
Object theTempBean = new Object(){
public String key = "blahblahblah";
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
};
model.addAttribute("theTempBean", theTempBean);
String viewName = "home";
return new ModelAndView(viewName, "command", model);
}
<form:form action="/myAction" modelAttribute="theTempBean">
<form:input path="key" />
<input
type="submit" value="Submit" />
</form:form>
JSP应该如下所示:
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model) {
Object theTempBean = new Object(){
public String key = "blahblahblah";
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
};
model.addAttribute("theTempBean", theTempBean);
String viewName = "home";
return new ModelAndView(viewName, "command", model);
}
<form:form action="/myAction" modelAttribute="theTempBean">
<form:input path="key" />
<input
type="submit" value="Submit" />
</form:form>
我测试了这个简单的示例,它在Spring版本4.2.0.RELEASE和Jetty Maven插件版本9.3.2.v20150730中正常工作
编辑
有一只虫子。如果您决定这样做,您必须在任何请求中设置“theTempBean”(也许它可以成为@modeldattribute
。同样,它只是一个额外bean类的内联版本)。这是固定操作处理程序方法:
@RequestMapping(path = "/myAction")
public String myAction(@RequestParam("key") String param
, Model model){
logger.info(param);
Object theTempBean = new Object(){
public String key = param;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
};
model.addAttribute("theTempBean", theTempBean);
return jspViewName("home");
}