[英]How correctly use the Spring MVC <form:select> tag to show the value of a specific object field into a collection?
I am pretty new in Spring MVC and I have some difficulties to understand how exactly works the 我是Spring MVC的新手,我很难理解究竟是如何工作的 tag.
标签。
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 Twb1012Regione object and put it into the model object so it will be available into the consultazione.jsp page. 正如您所看到的,此方法检索Twb1012Regione对象的List并将其放入模型对象中,以便可以在consultazione.jsp页面中使用。
So the Twb1012Regione class 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 codReg field univocally identify the object and the desReg contain the value that I want to show as value into the codReg字段唯一地标识对象, desReg包含我想要显示为值的值 tag.
标签。
Finnaly this is the code of my consultazione.jsp view: Finnaly这是我的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 objects and not the name of the desReg field. 问题是,这样做我获得了选择下拉列表,但它显示了我所有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 desReg field of each Twb1012Regione instead 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?
我该如何解决?
<form:select>
tag you must put in the path
the name of the bean attribute will keep the id of the selected item . <form:select>
标记中,您必须在path
放置bean属性的名称将保留所选项的ID 。 <form:options>
: <form:options>
:
items
: the bean attribute containing the list of selectable items items
:包含可选项列表的bean属性 itemLabel
: the description to be shown in the dropbox itemLabel
:要在dropbox中显示的说明 itemValue
: the field to be saved (usually id) in the path of the <form:select>
itemValue
:要在<form:select>
的路径中保存的字段(通常为id) <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
会告诉你所有的描述(Dropbox的
desReg
的地区),并且将保持在codReg
所选项目的在bean属性regioni
In your controller, add this line of code. 在您的控制器中,添加以下代码行。 Let me know :
让我知道 :
model.addAttribute("regioni",new Twb1012Regione());
If it does not work, let me know. 如果它不起作用,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.