You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jim Menard <ji...@io.com> on 2004/01/02 15:35:41 UTC

Passing localized string from page to component

I'm trying to pass a localized string stored in a .properties file from 
the page to a component. Here's my attempt, which throws a 
NullPointerException. Any suggestions?

foo.properties:

   key1 = title text
   key2 = type text

foo.jwc:

   <component id="some_id" type="DirectLink">
     <binding name="listener" expression="listeners.myListenerMethod"/>
     <string-binding name="title" key="key1"/>
     <string-binding name="type" key="key2"/>
   </component>

foo.java:

   // I want to return "title text type text". This is called from
   // myListenerMethod. This is where the NullPointerException gets 
thrown.
   protected String theStringIWant() {
     return getBinding("title").getString() + ' '
          + getBinding("type").getString();
   }

Thank you for your help.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"I am sure that like Java, [C#] will be a 'no pointer' language, where 
the
most common runtime error will be a 'NULL pointer exception'."
    -- Jerry Kott, in comp.lang.smalltalk


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
I probably won't answer all of the question, but here is some partial 
info...

On Jan 2, 2004, at 9:35 AM, Jim Menard wrote:
> I'm trying to pass a localized string stored in a .properties file 
> from the page to a component. Here's my attempt, which throws a 
> NullPointerException. Any suggestions?
>
> foo.properties:
>
>   key1 = title text
>   key2 = type text
>
> foo.jwc:
>
>   <component id="some_id" type="DirectLink">
>     <binding name="listener" expression="listeners.myListenerMethod"/>
>     <string-binding name="title" key="key1"/>
>     <string-binding name="type" key="key2"/>
>   </component>


I assume you mean <static-binding> here.    DirectLink does not take a 
"title" and "type" binding - so I'm confused about why you are using 
them in this manner.

> foo.java:
>
>   // I want to return "title text type text". This is called from
>   // myListenerMethod. This is where the NullPointerException gets 
> thrown.
>   protected String theStringIWant() {
>     return getBinding("title").getString() + ' '
>          + getBinding("type").getString();
>   }

I'm even more confused by this.  If you want to keep the key names in 
the .jwc file, perhaps make them extensions?  Or making them properties 
is another option.  Or you could use an OGNL expression in a <binding> 
that glues getMesssage("title") together with getMessage("type").  I 
would recommend the OGNL expression approach so that your .java code is 
out of the loop for formatting concerns.

	Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by John Meredith <ps...@t-online.de>.
Hi Jim,

I seem to remember reading somewhere that getString() will be phased out
in favour of getMessage() - so use getMessage() instead. Not quite sure
why, although I presume it's as simply that "message" is just more
descriptive/intuitive than "string".


As to the parameters, it's just an element of your component spec. ie:

  <parameter name="title" type="java.lang.String" direction="in"/>

In your DirectLink component:

<component id="some_id" type="DirectLink">
  ...
  <binding name="title" expression="title"/>
  ...
</component>

You could then reference your component thusly:

<span jwcid="@foo" title="SomeString"/>

  - John

On Fri, 2004-01-02 at 16:51, Jim Menard wrote:
> John,
> 
> Thanks to you and everyone else for your answers.
> 
> > Nonetheless, you could can access the page properties via OGNL:
> >
> > <binding name="title" expression="page.getMessage('key1')"/>
> > <binding name="type" expression="page.getMessage('key2')"/>
> >
> > and in your java file:
> >
> > getPage().getMessage('key1') + ' ' + getPage().getMessage('key2');
> 
> I've discovered that AbstractComponent.getString() also works. What's 
> the difference between getMessage() and getString()?
> 
> 
> > Personally, I'd be inclined to setup 'title' and 'type' as parameters 
> > to
> > the "foo" component and use those instead.
> 
> Thanks. I haven't yet learned how to do that. I'll look into it, though 
> getString/getMessage seem to work just fine for me.
> 
> Jim
-- 
John Meredith <ps...@t-online.de>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by Jim Menard <ji...@io.com>.
John,

Thanks to you and everyone else for your answers.

> Nonetheless, you could can access the page properties via OGNL:
>
> <binding name="title" expression="page.getMessage('key1')"/>
> <binding name="type" expression="page.getMessage('key2')"/>
>
> and in your java file:
>
> getPage().getMessage('key1') + ' ' + getPage().getMessage('key2');

I've discovered that AbstractComponent.getString() also works. What's 
the difference between getMessage() and getString()?


> Personally, I'd be inclined to setup 'title' and 'type' as parameters 
> to
> the "foo" component and use those instead.

Thanks. I haven't yet learned how to do that. I'll look into it, though 
getString/getMessage seem to work just fine for me.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"You might do well to heed the bumper sticker on a laser I had once:
'do not look into laser with remaining eye.'" -- Daniel E. Macks


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by John Meredith <ps...@t-online.de>.
Hi Jim,

Umm, I'm pretty sure DirectLink doesn't have 'title' and 'type'
parameters (although it does allow informal params) - so I'm not
entirely sure what you're trying to do.

Nonetheless, you could can access the page properties via OGNL:

<binding name="title" expression="page.getMessage('key1')"/>
<binding name="type" expression="page.getMessage('key2')"/>

and in your java file:

getPage().getMessage('key1') + ' ' + getPage().getMessage('key2');

Personally, I'd be inclined to setup 'title' and 'type' as parameters to
the "foo" component and use those instead.

  - John

On Fri, 2004-01-02 at 15:35, Jim Menard wrote:
> I'm trying to pass a localized string stored in a .properties file from 
> the page to a component. Here's my attempt, which throws a 
> NullPointerException. Any suggestions?
> 
> foo.properties:
> 
>    key1 = title text
>    key2 = type text
> 
> foo.jwc:
> 
>    <component id="some_id" type="DirectLink">
>      <binding name="listener" expression="listeners.myListenerMethod"/>
>      <string-binding name="title" key="key1"/>
>      <string-binding name="type" key="key2"/>
>    </component>
> 
> foo.java:
> 
>    // I want to return "title text type text". This is called from
>    // myListenerMethod. This is where the NullPointerException gets 
> thrown.
>    protected String theStringIWant() {
>      return getBinding("title").getString() + ' '
>           + getBinding("type").getString();
>    }
> 
> Thank you for your help.
> 
> Jim
-- 
John Meredith <ps...@t-online.de>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by Harish Krishnaswamy <hk...@comcast.net>.
Assuming you are using Tapestry 3.0, if you want the localized string as the parameters in your link ...

foo.jwc

<component id="compId" type="DirectLink">
     <binding name="listener" expression="listeners.myListenerMethod"
     <message-binding name="title" key="key1">
     <message-binding name="type" key="key2">
</component>

foo.java

public void myListenerMethod(IRequestCycle cycle)
{
     String title = (String) cycle.getServiceParameters()[0];
     String type = (String) cycle.getServiceParameters()[1];
}

If you want to access the localized properties file directly from the listener method...

foo.java

public void myListenerMethod(IRequestCycle cycle)
{
     String title = getMessage("titleKey");
     String type = getMessage("typeKey");
}

-Harish

Jim Menard wrote:

> I'm trying to pass a localized string stored in a .properties file from 
> the page to a component. Here's my attempt, which throws a 
> NullPointerException. Any suggestions?
> 
> foo.properties:
> 
>   key1 = title text
>   key2 = type text
> 
> foo.jwc:
> 
>   <component id="some_id" type="DirectLink">
>     <binding name="listener" expression="listeners.myListenerMethod"/>
>     <string-binding name="title" key="key1"/>
>     <string-binding name="type" key="key2"/>
>   </component>
> 
> foo.java:
> 
>   // I want to return "title text type text". This is called from
>   // myListenerMethod. This is where the NullPointerException gets thrown.
>   protected String theStringIWant() {
>     return getBinding("title").getString() + ' '
>          + getBinding("type").getString();
>   }
> 
> Thank you for your help.
> 
> Jim


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Passing localized string from page to component

Posted by Jim Menard <ji...@io.com>.
Wow. Thank you for the sample and explanation. Tapestry is way groovy!

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"Hey, wait a minute. You've got both eyes! You're no special agent.
You're just some jerk who hates my moustache!" - The Tick


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Passing localized string from page to component

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
People have just been missing the mark on this.

<component id="..." type="DirectLink">
  <binding name="listener" expression="listeners.myListener">
  <binding name="parameters">
    {
      getMessage("key1"), 
      getMessage("key2")
    }
  </binding>
</component>

public void myListener(IRequestCycle cycle)
{
  Object[] params = cycle.getRequestParameters();
  String title = (String)params[0];
  String type = (String)params[1];

  ...
}


What's going on here?

The <binding> encloses an OGNL expression (this "long form" of the <binding> element is useful for
long or complex expressions).  The OGNL expression uses { ... } to construct a collection of values.
We use the getMessage() method on AbstractComponent to get the localized message value (isn't OGNL
darn useful?).

Inside the listener method, we get the parameters that were constructed at render time and cast the
values back to strings.  In Tapestry, service parameters maintain their types even when encoded as
strings in the URL (if they started as Integers or Dates or a custom type, you would cast to
Integer, Date, etc. in your listener method).

The Virtual Library demo has a number of links somewhat like this, for linking to Books and Persons.
For each of those, I created a custom component that took real parameters (in your case, "title" and
"type") and used those to construct the parameters for an ExternalLink.  Remember: divide (into
components) and conquer!


--
Howard M. Lewis Ship
Creator, Tapestry: Java Web Components
http://jakarta.apache.org/tapestry
http://jakarta.apache.org/commons/sandbox/hivemind/
http://javatapestry.blogspot.com

> -----Original Message-----
> From: Jim Menard [mailto:jimm@io.com] 
> Sent: Friday, January 02, 2004 9:36 AM
> To: Tapestry users
> Subject: Passing localized string from page to component
> 
> 
> I'm trying to pass a localized string stored in a .properties 
> file from 
> the page to a component. Here's my attempt, which throws a 
> NullPointerException. Any suggestions?
> 
> foo.properties:
> 
>    key1 = title text
>    key2 = type text
> 
> foo.jwc:
> 
>    <component id="some_id" type="DirectLink">
>      <binding name="listener" 
> expression="listeners.myListenerMethod"/>
>      <string-binding name="title" key="key1"/>
>      <string-binding name="type" key="key2"/>
>    </component>
> 
> foo.java:
> 
>    // I want to return "title text type text". This is called from
>    // myListenerMethod. This is where the NullPointerException gets 
> thrown.
>    protected String theStringIWant() {
>      return getBinding("title").getString() + ' '
>           + getBinding("type").getString();
>    }
> 
> Thank you for your help.
> 
> Jim
> -- 
> Jim Menard, jimm@io.com, http://www.io.com/~jimm/
> "I am sure that like Java, [C#] will be a 'no pointer' 
> language, where 
> the
> most common runtime error will be a 'NULL pointer exception'."
>     -- Jerry Kott, in comp.lang.smalltalk
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org