tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hans Bergsten <h...@gefionsoftware.com>
Subject Re: How to maintain collections with JSTL?
Date Mon, 18 Nov 2002 17:06:11 GMT
Zaretzke, Peter wrote:
> Hello Hans
> 
> Thanks for your reply.
> 
> I get an 
> 
>       java.lang.NullPointerException
> 	at
> org.apache.taglibs.standard.tag.common.core.SetSupport.doEndTag(SetSupport.j
> ava:172)
> 	at
> org.apache.jsp.beantest_jsp._jspx_meth_c_set_0(beantest_jsp.java:396)
> 	at
> org.apache.jsp.beantest_jsp._jspx_meth_c_if_1(beantest_jsp.java:356)
> 	at org.apache.jsp.beantest_jsp._jspService(beantest_jsp.java:150)
> 	at
> org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> 	at
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:2
> 04)
> 	at
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
> 
> In case I have this method in my bean
> 
> 	public void setList ( String elem ) {
> 		list.add ( elem );
> 	}
> 
> It remains the same with "Object elem".
> 
> After removing the getter method from my bean
> 
> 	public List getList() {
> 		return list;
> 	}
> 
> I get no exception anymore but also not the data from my list ;-)
> Could it be a problem in the introspection mechanism of the bean?
> When I change the setter method to 
> 
> 	public void setList ( List elem ) {
> 		list.add ( elem );
> 	}
> 
> I get this (expected) exception
> 
>       javax.servlet.ServletException: Attempt to convert String "paul" to
> type "java.util.List", 
>                                           but there is no PropertyEditor for
> that type
> 	at [...]
> 
> Any further ideas?

Ah, I missed a detail: you already have a getList() method that returns
a List. The set and get methods must use the same type (since they
represent different types of access to the _same_ property). Changing
the setter to type String or Object breaks this rule.

If you want to have a getter method that returns the List and a setter
method that adds to the list, you need to use different methods names
(i.e. define different properties), e.g.:

   public List getList() {
     return list;
   }

   public void setListItem(Object o) {
     list.add(o);
   }

You can then add to the list with

   <c:set target="${myList}" property="listItem" value="${myValue}" />

Hans

> -----Original Message-----
> From: Hans Bergsten [mailto:hans@gefionsoftware.com] 
> Sent: Freitag, 15. November 2002 17:34
> To: Tag Libraries Users List
> Subject: Re: How to maintain collections with JSTL?
> 
> 
> Zaretzke, Peter wrote:
> 
>>[...]
>>But how to add a new items to the collections? I tried this
>>
>>	<c:set target="${testbean}" property="list" value="${myvalue}"/>
>>
>>But this bean method
>>
>>	public void setList( String value ) {
>>		list.add ( value );
>>	}
>>
>>does not work because it does not meet the parameter expectations ( 
>>List vs. String ) for beans. I know that I can write more setZZZ() 
>>methods to add something to the List but it breaks somehow the naming 
>>conventions for beans (getXxx, setXxx). And how to add a new entry to 
>>the map?
> 
> 
> It should work as long as the value can be converted to a String (which is
> almost always the case, see the EL type coersion rules). What kind of error
> do you see? If you want to avoid convertion to String, change the
> setList() method to take an Object instead of a String.
> 
> Hans

-- 
Hans Bergsten                                <hans@gefionsoftware.com>
Gefion Software                       <http://www.gefionsoftware.com/>
Author of O'Reilly's "JavaServer Pages", covering JSP 1.2 and JSTL 1.0
Details at                                    <http://TheJSPBook.com/>


--
To unsubscribe, e-mail:   <mailto:taglibs-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-user-help@jakarta.apache.org>


Mime
View raw message