Re: How to maintain collections with JSTL?

2002-11-18 Thread Hans Bergsten
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:[EMAIL PROTECTED]] 
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[EMAIL PROTECTED]
Gefion Software   http://www.gefionsoftware.com/
Author of O'Reilly's JavaServer Pages, covering JSP 1.2 and JSTL 1.0
Details athttp://TheJSPBook.com/


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




How to maintain collections with JSTL?

2002-11-15 Thread Zaretzke, Peter
Dear friends of JSTL

I'm new to JSTL and looking for some ideas how to maintain collections with
JSTL and EL.

I have a bean with a List and a Map type property like this:

public class TestBean  {
private List list = new ArrayList();
private Map map = new HashMap();

public TestBean () {
list.add(initial value 1);
list.add(initial value 2);
map.put (simple value,the initial simple value);
map.put (listvalue, list );
}

public List getList() {
return list;
}

public Map getMap() {
return map;
}   
}


Getting the values from my collections is easy and elegant:

c:forEach var=litem items=${testbean.listitems} 
  c:out value=${litem}/br/
/c:forEach

c:forEach var=mitem items=${testbean.map} 
  c:out value=${mitem.key}/ = c:out
value=${mitem.value}/br/
/c:forEach

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? 


Thanks in advance
Peter

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




Re: How to maintain collections with JSTL?

2002-11-15 Thread Shawn Bayern
On Fri, 15 Nov 2002, Zaretzke, Peter wrote:

   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?

Unless you're happy with writing a new setXxx() method that modifies a
collection (which is a clever idea but still something of a hack because
it violates the intent of the JavaBeans specification), it's actually
quite hard to manage collections in this way with JSTL.  Since JSTL is
focused on presentation -- on reading data structures rather than writing
them -- it's likely going to be easier to modify collections in a back-end
servlet (or with a new custom tag you write -- one that's specific to your
own custom data).

Shawn


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




Re: How to maintain collections with JSTL?

2002-11-15 Thread Hans Bergsten
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[EMAIL PROTECTED]
Gefion Software   http://www.gefionsoftware.com/
Author of O'Reilly's JavaServer Pages, covering JSP 1.2 and JSTL 1.0
Details athttp://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