java 如何正确使用 Spring MVC <form:select> 标记将特定对象字段的值显示到集合中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32606019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How correctly use the Spring MVC <form:select> tag to show the value of a specific object field into a collection?
提问by Jordi Castilla
I am pretty new in Spring MVC and I have some difficulties to understand how exactly works the tag.
我是 Spring MVC 的新手,我在理解 Spring MVC 的工作原理方面遇到了一些困难 标签。
So I have the following situation.
所以我有以下情况。
Into a controller I have this method:
进入控制器我有这个方法:
@RequestMapping(value = "/consultazioneMinisteriale", method = RequestMethod.GET)
public String consultazione(Locale locale, Model model) {
List<Twb1012Regione> listaRegioni = geograficaService.getListaRegioni();
System.out.println("Numero regioni: " + listaRegioni.size());
model.addAttribute("listaRegioni", listaRegioni);
return "utenteMinisteriale/consultazione";
}
As you can see this method retrieve a List of Twb1012Regioneobject and put it into the model object so it will be available into the consultazione.jsppage.
如您所见,此方法检索Twb1012Regione对象的列表并将其放入模型对象中,以便在Consultazione.jsp页面中可用。
So the Twb1012Regioneclass is a model object like this:
所以Twb1012Regione类是一个像这样的模型对象:
@Entity
@Table(name="anagrafiche.TWB1012_REGIONE")
@NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="COD_REG")
private String codReg;
@Column(name="DES_REG")
private String desReg;
.....................................
.....................................
OTHER FIELDS
.....................................
.....................................
}
Where the codRegfield univocally identify the object and the desRegcontain the value that I want to show as value into the tag.
其中codReg字段明确标识对象,而desReg包含我想显示为值的值标签。
Finnaly this is the code of my consultazione.jspview:
最后这是我的Consultazione.jsp视图的代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page session="false"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body class="azure">
<h1>Hello World</h1>
<!-- <div> ${listaRegioni} </div> -->
<div>
<label>Regioni:</label>
<form:select path="listaRegioni" items="${listaRegioni}"/>
</div>
</body>
</html>
The problem is that doing in this way I obtain the select dropdown but it show the reference of all my Twb1012Regione objectsand not the name of the desRegfield.
问题是这样做我获得了选择下拉列表,但它显示了我所有 Twb1012Regione 对象的引用,而不是desReg字段的名称。
This is the HTML rendered output:
这是 HTML 呈现的输出:
<select>
<option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924">it.myCompany.myProject.anagrafiche.Twb1012Regione@5a259924</option>
<option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3">it.myCompany.myProject.anagrafiche.Twb1012Regione@4a87c8d3</option>
<option value="it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a">it.myCompany.myProject.anagrafiche.Twb1012Regione@815b53a</option>
.................................................
.................................................
.................................................
</select>
Why? What am I missing? How can I shoe the value of the desRegfield of each Twb1012Regioneinstead the reference of the objects?
为什么?我错过了什么?如何将每个Twb1012Regione的desReg字段的值替换为对象的引用?
EDIT-1:
编辑-1:
I tryied to change into:
我试着改成:
<form:select path="regioni">
<form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>
But now when the page is rendered I obtain this error message into my stacktrace:
但是现在当页面呈现时,我在我的堆栈跟踪中获得了这个错误消息:
12:44:52,112 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WIFIPNSD].[jsp]] (http-localhost/127.0.0.1:8080-4) JBWEB000236: Servlet.service() for servlet jsp threw exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'regioni' available as request attribute
Why? What is wrong? How can I solve it?
为什么?怎么了?我该如何解决?
回答by Jordi Castilla
- In
<form:select>
tag you must put in thepath
the name of the bean attribute will keep the id of the selected item. - In
<form:options>
:items
: the bean attribute containing the list of selectable itemsitemLabel
: the description to be shown in the dropboxitemValue
: the field to be saved (usually id) in the path of the<form:select>
- 在
<form:select>
标签中,你必须path
输入 bean 的名称,属性将保留所选项目的 id。 - 在
<form:options>
:items
:包含可选项目列表的 bean 属性itemLabel
:要在保管箱中显示的描述itemValue
: 路径中要保存的字段(通常是id)<form:select>
<form:select path="regioni">
<form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>
Will show you a dropbox with all descriptions (desReg
) of the regions, and will keep the the codReg
of the selected item in the bean attribute regioni
desReg
将向您显示一个包含所有区域描述 ( ) 的保管箱,并将codReg
在 bean 属性中保留所选项目的regioni
回答by We are Borg
In your controller, add this line of code. Let me know :
在您的控制器中,添加这行代码。让我知道 :
model.addAttribute("regioni",new Twb1012Regione());
If it does not work, let me know.
如果它不起作用,请告诉我。