Isn't this solution supposed to be the standard solution as shown in the documentation? Here is what the documentation on mapped properties says:
The idea of "mapped properties" as opposed to "indexed properties" is that the property represents a "map" type, as opposed to an array or collection type. The signature of the "get" and "set" methods for a mapped property are different from the same methods for an indexed property. In particular, instead of an "int" for the index, there is a "String" for the key.
The previous example for indexed properties can be changed to the following to demonstrate mapped properties:
package org.apache.struts.webapp.exercise;
import java.util.HashMap;
import org.apache.struts.action.ActionForm;
public class StringBean3 extends ActionForm {
private String strAry[] = { "String 0", "String 1", "String 2", "String 3", "String 4" };
private HashMap map = new HashMap();
public StringBean() {
map.put("zero", strAry[0]);
map.put("one", strAry[1]);
map.put("two", strAry[2]);
map.put("three", strAry[3]);
map.put("four", strAry[4]);
}
public Object getStringMapped(String key) {
return map.get(key);
}
public void setStringMapped(String key, Object value) {
map.put(key, value);
}
}
Note the "get" and "set" methods to represent the mapped property.
<!-- indexedtest3.jsp -->
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<jsp:useBean id="bean" class="org.apache.struts.webapp.exercise.StringBean"/>
<bean:write name="bean" property="stringMapped(two)"/>
Note the property value of "stringMapped(two)". This will reference the mapped property "stringMapped", using the key value of "two".
When this page is executed, it will print just the string "String 2", which is the string stored in the HashMap with the key "two".
So, this is what I do. Why doesn't the define attribute of the bean tag do this for me?
At 09:29 AM 4/23/2004, Hubert Rabago wrote:
Those tags are meant to work with JavaBeans-style classes. I think there's been lots of questions about accessing stuff like SiteConstant.HOST and SiteConstant.ECLIPSE_BANNER, but I don't remember seeing a solution for that. One way around it (not that it's the best way) is to provide accessors that directly give you what (Page)host.getPage(SiteConstant.ECLIPSE_BANNER) returns.
Hubert
--- Michael McGrady <[EMAIL PROTECTED]> wrote: > > Thanks, Herbert, let me state more particularly what I am doing. I want to use Struts tags rather than > JSP. I know how to do things in JSP easily. With the tags, outside of > ActionForms, I am confused. In JSP, I get my results as follows: > > > <% > Host host = (Host)session.getAttribute(SiteConstant.HOST); > Page key = (Page)host.getPage(SiteConstant.ECLIPSE_BANNER); > %> > > <%= key.getValue("bannerLeftBackgroundColor") %> > > I want to do with the Struts bean tag what I do now with JSP tags. How do > I do that? This should be simple, but something is not working. I am > either erring with the "define" or the "write" attribute and all the > combinations I try don't work. People have indicated that I need to > retrieve a Map object with getMap(). I don't see why, given what I read in > > the literature. If you could help, I would be eternally (or a very long > time) grateful. The classes are given below: > > public class Host { > private Map pages; > > public Host() { > int size = 89; > this.pages = Collections.synchronizedMap(new HashMap(size)); > } > > public void setMap(Map pages) { > this.pages = pages; > } > > public Map getMap() { > return pages; > } > > public void setPage(Object key, Object page) { > pages.put(key,page); > } > > public Object getPage(Object key) { > return pages.get(key); > } > } ///;-) Michael McGrady > > > public class Page { > private Map keys; > > public Page() { > int size = 89; > this.keys = Collections.synchronizedMap(new HashMap(size)); > } > > public void setMap(Map keys) { > this.keys = keys; > } > > public Map getMap() { > return keys; > } > > public void setValue(Object key, Object value) { > keys.put(key,value); > } > > public Object getValue(Object key) { > Object value = keys.get(key); > com.crackwillow.log.StdOut.redirect("log.VALUE: " + value); > if(value == null) { > return "? [" + (String)key + "] ?"; > } else { > return value; > } > } > } ///;-) Michael McGrady > >
__________________________________ Do you Yahoo!? Yahoo! Photos: High-quality 4x6 digital prints for 25¢ http://photos.yahoo.com/ph/print_splash
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]