Spring Roo MVC:JSTL 打印对象数组
我有一个具有相关方法的工作 Spring 控制器类:
@RequestMapping(value="shownews", method = RequestMethod.GET)
public String getNews(Model model) {
// test
ArrayList<String> a =new ArrayList<String>();
a.add("aa");
a.add("bb");
model.addAttribute("someA", a);
// real data
model.addAttribute("newsS", News.getAllNews()); // returns a valid List<News>
return "shownews";
}
然后我想在我的 .jsp 页面中显示这些值,代码:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
...
VALUES: ${someA} // output: '[aa, bb]'
VALUES: ${newsS} // output: '[Category: categ1, Contents: cont1, Dateposted: 2011-
12-22 00:00:00.0, Id: 1, IdWho ........]'
<c:forEach items="${newsS}" var="someitem">
<p> Category: ${someitem.category} </p> // output: 'Category: ', instead of 'Category: categ1'
</c:forEach>
看来数组已正确提交以查看... 答案是:如何列出每个对象及其属性? (使用 forEach 标签) 谢谢你!
更新:
我的 News.java 文件仅包含字段声明(没有 getter 或 setter)。
一些 Roo 生成的 AspectJ 文件: News_Roo_Entity.aj:
privileged aspect News_Roo_Entity {
....
public static List<News> News.findAllNews() {
return entityManager().createQuery("SELECT o FROM News o", News.class).getResultList();
}
}
News_Roo_JavaBean.aj:
privileged aspect News_Roo_JavaBean {
public String News.getCategory()
{
return this.category; // it's working 100%
}
... other get() methods
}
已解决
我在我的项目中禁用了Roo(变成了一个简单的Spring)但没有结果......
[琐碎]问题是: 我的 view.jsp 有标题:
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<div xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:page="urn:jsptagdir:/WEB-INF/tags/form" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:table="urn:jsptagdir:/WEB-INF/tags/form/fields" version="2.0">
....
</div>
已删除
<div xmlns:jsp= .... >
并替换为一个简单的标题:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1" isELIgnored="false" %>
可能只是从 .jspx 文件复制和粘贴以及 xmlns 和 taglib 的混合...
感谢您的建议!
I have a working Spring Controller class with the relevant method:
@RequestMapping(value="shownews", method = RequestMethod.GET)
public String getNews(Model model) {
// test
ArrayList<String> a =new ArrayList<String>();
a.add("aa");
a.add("bb");
model.addAttribute("someA", a);
// real data
model.addAttribute("newsS", News.getAllNews()); // returns a valid List<News>
return "shownews";
}
Then I want to display those values in my .jsp page, code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
...
VALUES: ${someA} // output: '[aa, bb]'
VALUES: ${newsS} // output: '[Category: categ1, Contents: cont1, Dateposted: 2011-
12-22 00:00:00.0, Id: 1, IdWho ........]'
<c:forEach items="${newsS}" var="someitem">
<p> Category: ${someitem.category} </p> // output: 'Category: ', instead of 'Category: categ1'
</c:forEach>
It seems that the array is properly submitted to view...
The answer is: how can I list every object with its properties? (using forEach tag)
Thank you!
UPDATE:
My News.java file contains only field declarations (no getters or setters).
Some Roo generated AspectJ files:
News_Roo_Entity.aj:
privileged aspect News_Roo_Entity {
....
public static List<News> News.findAllNews() {
return entityManager().createQuery("SELECT o FROM News o", News.class).getResultList();
}
}
News_Roo_JavaBean.aj:
privileged aspect News_Roo_JavaBean {
public String News.getCategory()
{
return this.category; // it's working 100%
}
... other get() methods
}
SOLVED
I disabled Roo in my proj (became a simple Spring) but no results...
The [trivial] problem was:
My view.jsp had the header:
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<div xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:page="urn:jsptagdir:/WEB-INF/tags/form" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:table="urn:jsptagdir:/WEB-INF/tags/form/fields" version="2.0">
....
</div>
Removed
<div xmlns:jsp= .... >
and replaced with a simple one:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1" isELIgnored="false" %>
Probably it was just copy&paste from a .jspx file and a mix of xmlns and taglib...
Thanks for suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该按照您尝试的方式工作:
${someitem.category}
将打印方法News.getCategory() 返回的对象的
。toString()
的结果您描述的行为看起来像
getCategory()
返回null
(或更不可能:toString()
方法返回空字符串)It should work exatly how you tried:
${someitem.category}
will print the result fromtoString()
of the object returned by methodNews.getCategory()
.The behavior you desciped looks like
getCategory()
returnsnull
(or more unlikely: thetoString()
method return an empty string)