如何正确使用Spring MVC< form:select>标签以显示集合中特定对象字段的值? [英] How correctly use the Spring MVC <form:select> tag to show the value of a specific object field into a collection?
问题描述
我在Spring MVC中还很陌生,很难理解 标签的工作原理.
所以我有以下情况.
我有一个进入控制器的方法:
@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";
}
如您所见,此方法检索 Twb1012Regione 对象的列表并将其放入模型对象,以便可以在 consultazione.jsp 页面中使用.
>所以 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
.....................................
.....................................
}
codReg 字段唯一标识对象,而 desReg 在 标记中包含我要显示为值的值.
最后,这是我的 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>
问题是,这样做后我获得了select下拉列表,但它显示了我所有Twb1012Regione对象的引用,而不是 desReg 字段的名称. /p>
这是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>
为什么?我想念什么?我该如何计算每个 Twb1012Regione 的 desReg 字段的值,而不是对象的引用?
EDIT-1:
我试图将其更改为:
<form:select path="regioni">
<form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>
但是现在呈现页面时,我在我的堆栈跟踪中获得了以下错误消息:
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
为什么?怎么了?我该怎么解决?
- 在
<form:select>
标记中,您必须将path
放在bean属性的名称中,将保留所选项目的ID . - 在
<form:options>
中:-
items
:包含可选项目列表的bean属性 -
itemLabel
:要在保管箱中显示的说明 -
itemValue
:要保存在<form:select>
路径中的字段(通常为id)
-
<form:select path="regioni">
<form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
</form:select>
将向您显示一个具有所有区域说明(desReg
)的保管箱,并将所选项目的codReg
保留在bean属性regioni
I am pretty new in Spring MVC and I have some difficulties to understand how exactly works the 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.
So the Twb1012Regione class is a model object like this:
@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 tag.
Finnaly this is the code of my consultazione.jsp view:
<%@ 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.
This is the HTML rendered output:
<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?
EDIT-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?
- 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="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
这篇关于如何正确使用Spring MVC< form:select>标签以显示集合中特定对象字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!