如何添加值由JSP引用一个ArrayList:useBean的? [英] How to add values to an ArrayList referenced by jsp:useBean?

查看:651
本文介绍了如何添加值由JSP引用一个ArrayList:useBean的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSP / JSTL,我怎么能对类的useBean的设定值=java.util.ArrayList中。

如果我尝试使用C:设置属性或值,我得到以下错误:
javax.servlet.jsp.JspTagException:空

:在无效的属性
解决方案

这是不能直接成为可能。还有的 c为C:集> < JSP:的setProperty> 标签,它允许你在设置属性通过一个se​​tter方法​​fullworthy的javabean。但是,列表接口没有一个二传手,只是一个添加()方法。

A 解决方法将包装清单在一个真正的javabean像这样:

 公共类ListBean {    私人列表<对象>名单=新的ArrayList<对象>();    公共无效setChild(Object对象){
        list.add(对象);
    }    公开名单<对象>的GetList(){
        返回列表;
    }
}

设置

 < JSP:useBean的ID =listBean级=com.example.ListBean范围=请求/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =富/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =酒吧/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =WAA/>

但是,这没有什么意义。如何解决它正确地依赖于单一功能需求。如果您想preserve一些列表在一个GET请求,那么你就应该使用preprocessing的servlet。创建一个servlet,它确实在的doGet)以下(方法:

 列表<串GT;清单= Arrays.asList(富,酒吧,WAA);
了request.setAttribute(清单,清单);
的request.getRequestDispatcher(/ WEB-INF / page.jsp)向前(请求,响应)。

当您通过URL调用Servlet,则列表在转发的JSP可以通过

  $ {}列表

需要老式< JSP:useBean的> 标记。在一个servlet你都自由编写Java code中的常用方法。 useBean的> 标记&LT这样,您就可以使用JSP纯presentation只,无需狼吞虎咽/破解一些preprocessing逻辑p>

参见:

In JSP/JSTL, how can I set values for a usebean of class="java.util.ArrayList".

If I try using c:set property or value, I get the following error: javax.servlet.jsp.JspTagException: Invalid property in : "null"

解决方案

That isn't directly possible. There are the <c:set> and <jsp:setProperty> tags which allows you to set properties in a fullworthy javabean through a setter method. However, the List interface doesn't have a setter, just an add() method.

A workaround would be to wrap the list in a real javabean like so:

public class ListBean {

    private List<Object> list = new ArrayList<Object>();

    public void setChild(Object object) {
        list.add(object);
    }

    public List<Object> getList() {
        return list;
    }
}

and set it by

<jsp:useBean id="listBean" class="com.example.ListBean" scope="request" />
<jsp:setProperty name="listBean" property="child" value="foo" />
<jsp:setProperty name="listBean" property="child" value="bar" />
<jsp:setProperty name="listBean" property="child" value="waa" />

But that makes little sense. How to solve it rightly depends on the sole functional requirement. If you want to preserve some List upon a GET request, then you should be using a preprocessing servlet. Create a servlet which does the following in doGet() method:

List<String> list = Arrays.asList("foo", "bar", "waa");
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

When you invoke the servlet by its URL, then the list is in the forwarded JSP available by

${list}

without the need for old fashioned <jsp:useBean> tags. In a servlet you've all freedom to write Java code the usual way. This way you can use JSP for pure presentation only without the need to gobble/hack some preprocessing logic by <jsp:useBean> tags.

See also:

这篇关于如何添加值由JSP引用一个ArrayList:useBean的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆