Spring Roo MVC:JSTL 打印对象数组

发布于 2024-12-23 03:05:13 字数 2676 浏览 2 评论 0原文

我有一个具有相关方法的工作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-12-30 03:05:13

它应该按照您尝试的方式工作: ${someitem.category} 将打印方法 News.getCategory() 返回的对象的 toString() 的结果

您描述的行为看起来像 getCategory() 返回 null (或更不可能:toString() 方法返回空字符串)

It should work exatly how you tried: ${someitem.category} will print the result from toString() of the object returned by method News.getCategory().

The behavior you desciped looks like getCategory() returns null (or more unlikely: the toString() method return an empty string)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文