You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Warren Bell <wa...@clarksnutrition.com> on 2005/11/10 21:01:05 UTC

Messages as text only?

I would like to take messages and place them into some JavaScript less the
HTML. Is there a way to render messages as text only, or would I have to
create a custom rederer? Writing a custom renderer will not happen, I am way
to new to JSF.

Thanks,

Warren Bell


Re: Messages as text only?

Posted by Simon Kitching <sk...@obsidium.com>.
Simon Kitching wrote:
>
> 
>  <render-kit>
>    <renderer>
>      <component-family>javax.faces.Output</component-family>
>      <renderer-type>
>        org.apache.myfaces.renderkit.html.HtmlMessagesRenderer
>      </renderer-type>
>      <renderer-class>my.package.extend.MyMsgRenderer</renderer-class>
>     </renderer>
>  </render-kit>
> 

Sorry, I think the component-family should be:
   javax.faces.Messages
not
   javax.faces.Output.

See the "getComponentFamily" method for the relevant UIComponent.

Regards,

Simon

Re: Messages as text only?

Posted by Mike Kienenberger <mk...@gmail.com>.
I don't think you have to state what an acceptable value is.  
However, you can provide IDE tool hints about components in your
faces-config file, but these are optional.

On 11/11/05, Warren Bell <wa...@clarksnutrition.com> wrote:
> Do you have to state somewhere what is an acceptable value for a property. I
> can see where the property/attribute is listed in the tld, but there is no
> mention of what values can be used. The IDE I am using states that "table"
> and "list" are the two values that the property "layout" can use. I can not
> find these values listed anywhere accept in the rederer itself. I am
> thinking that my IDE (Eclipse with M7 NitroX plugin) has a configuration
> file of some sorts.
>
> >
> > Warren Bell wrote:
> > > I would like to just add another value for an existing property. I have
> > > extended the renderer for the messages component. Now I just need the
> > > component to use that renderer. And the component to pass to
> > the renderer
> > > the new property value. The property is layout and its accepted
> > values are
> > > table|list. I want to add 'text' as an additional value. I have included
> > > this new value in my new class.
> >
> > Dang. I didn't read carefully enough. Sorry. As the "layout" attribute
> > already exists, you can completely ignore my most recent reply :-(
> >
> > Yes all you need in the jsp page is:
> >    <t:messages layout="text">
> > and to configure a new renderer as my first reply showed.
> >
> > Looks to me like your new renderer will just need to reimplement this
> > inherited method (copy/paste/edit):
> >      protected void renderMessages(FacesContext facesContext,
> >                                    UIComponent messages)
> > and add a call to the "renderText" method when layout matches.
> >
> > Sorry for the confusion.
> >
> > Regards,
> >
> > Simon
> >
> > __________ NOD32 1.1283 (20051110) Information __________
> >
> > This message was checked by NOD32 antivirus system.
> > http://www.eset.com
> >
> >
>
>

Re: Messages as text only?

Posted by Mike Kienenberger <mk...@gmail.com>.
This probably won't help Warren, but in the interests of completeness
for future readers, here's another option.

If you're using facelets, I think you could simply define another tag
which uses your new renderer.  Ie, messagesAsText in addition to
messages.   I've not tested it, though.

   <tag>
        <tag-name>messages</tag-name>
        <component>
                <component-type>org.apache.myfaces.HtmlMessages</component-type>
            <renderer-type>org.apache.myfaces.Messages</renderer-type>
        </component>
    </tag>

   <tag>
        <tag-name>messagesAsText</tag-name>
        <component>
                <component-type>org.apache.myfaces.HtmlMessages</component-type>
            <renderer-type>mypackage.myRenderer</renderer-type>
        </component>
    </tag>

On 11/10/05, Simon Kitching <sk...@obsidium.com> wrote:
> Warren Bell wrote:
> > I would like to just add another value for an existing property. I have
> > extended the renderer for the messages component. Now I just need the
> > component to use that renderer. And the component to pass to the renderer
> > the new property value. The property is layout and its accepted values are
> > table|list. I want to add 'text' as an additional value. I have included
> > this new value in my new class.
>
> Dang. I didn't read carefully enough. Sorry. As the "layout" attribute
> already exists, you can completely ignore my most recent reply :-(
>
> Yes all you need in the jsp page is:
>    <t:messages layout="text">
> and to configure a new renderer as my first reply showed.
>
> Looks to me like your new renderer will just need to reimplement this
> inherited method (copy/paste/edit):
>      protected void renderMessages(FacesContext facesContext,
>                                    UIComponent messages)
> and add a call to the "renderText" method when layout matches.
>
> Sorry for the confusion.
>
> Regards,
>
> Simon
>

RE: Messages as text only?

Posted by Warren Bell <wa...@clarksnutrition.com>.
Do you have to state somewhere what is an acceptable value for a property. I
can see where the property/attribute is listed in the tld, but there is no
mention of what values can be used. The IDE I am using states that "table"
and "list" are the two values that the property "layout" can use. I can not
find these values listed anywhere accept in the rederer itself. I am
thinking that my IDE (Eclipse with M7 NitroX plugin) has a configuration
file of some sorts.

>
> Warren Bell wrote:
> > I would like to just add another value for an existing property. I have
> > extended the renderer for the messages component. Now I just need the
> > component to use that renderer. And the component to pass to
> the renderer
> > the new property value. The property is layout and its accepted
> values are
> > table|list. I want to add 'text' as an additional value. I have included
> > this new value in my new class.
>
> Dang. I didn't read carefully enough. Sorry. As the "layout" attribute
> already exists, you can completely ignore my most recent reply :-(
>
> Yes all you need in the jsp page is:
>    <t:messages layout="text">
> and to configure a new renderer as my first reply showed.
>
> Looks to me like your new renderer will just need to reimplement this
> inherited method (copy/paste/edit):
>      protected void renderMessages(FacesContext facesContext,
>                                    UIComponent messages)
> and add a call to the "renderText" method when layout matches.
>
> Sorry for the confusion.
>
> Regards,
>
> Simon
>
> __________ NOD32 1.1283 (20051110) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


Re: Messages as text only?

Posted by Simon Kitching <sk...@obsidium.com>.
Warren Bell wrote:
> I would like to just add another value for an existing property. I have
> extended the renderer for the messages component. Now I just need the
> component to use that renderer. And the component to pass to the renderer
> the new property value. The property is layout and its accepted values are
> table|list. I want to add 'text' as an additional value. I have included
> this new value in my new class.

Dang. I didn't read carefully enough. Sorry. As the "layout" attribute 
already exists, you can completely ignore my most recent reply :-(

Yes all you need in the jsp page is:
   <t:messages layout="text">
and to configure a new renderer as my first reply showed.

Looks to me like your new renderer will just need to reimplement this 
inherited method (copy/paste/edit):
     protected void renderMessages(FacesContext facesContext,
                                   UIComponent messages)
and add a call to the "renderText" method when layout matches.

Sorry for the confusion.

Regards,

Simon

RE: Messages as text only?

Posted by Warren Bell <wa...@clarksnutrition.com>.
I would like to just add another value for an existing property. I have
extended the renderer for the messages component. Now I just need the
component to use that renderer. And the component to pass to the renderer
the new property value. The property is layout and its accepted values are
table|list. I want to add 'text' as an additional value. I have included
this new value in my new class.

I think I am getting way in over my head. It looks like there is quite a bit
more involved than what I expected.

> -----Original Message-----
> From: Simon Kitching [mailto:skitching@obsidium.com]
> Sent: Thursday, November 10, 2005 4:15 PM
> To: MyFaces Discussion
> Subject: Re: Messages as text only?
>
>
> Warren Bell wrote:
> > Would I just use <t:messages layout="text"/> in my jsp where
> layout="text"
> > would be implemented in my my.package.extend.MyMsgRenderer class that
> > extends org.apache.myfaces.renderkit.html.HtmlMessagesRenderer?
>
> Ah, now you're wanting to add extra properties to a component rather
> than just changing the renderer, and things get *much* more complicated.
>
> First, you need to create a new .tld file with the new attribute defined
> in it, so that the JSP processor recognises the new attribute as valid.
> You define your own namespace for this new tag library, eg
> "warren.com/taglib1" then add references to this taglib in your .jsp
> files with some unique prefix.
>
> See tomahawk/tld/tomahawk.tld from the MyFaces source for an example tld.
>
> However JSP provides no mechanism to "inherit" attributes, so your new
> TLD entry needs to contain a copy-and-paste of the entire set of
> attribute definitions from the parent tag's tld. Ecch. MyFaces work
> around this by using XML entity definitions, but many servlet engines
> (incl. tomcat) don't correctly process entity references in .tld files
> so a preprocessing stage that uses XSLT to expand the entities is needed.
>
> Your tag class needs to have a setter for this new layout property, and
> update the component with it in its setProperties method.
>
> Having written your tag, you then need to subclass the UIComponent to
> add your new field. Well, actually you can be sneaky and skip this step,
>   as a component has an attributes map that stores any "properties" that
> aren't defined via setter/getter methods on the UIComponent. Generally
> that's bad form I think but probably ok in this case.
>
> Your custom renderer can then use component.getAttribute("layout") to
> determine what layout was defined via the JSP tag and do the right thing.
>
> About now is when you start thinking about using Facelets instead of
> JSP. I have only read the basic docs, but avoiding all this JSP tag
> garbage can't be bad.
>
> Regards,
>
> Simon
>
> __________ NOD32 1.1283 (20051110) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


Re: Messages as text only?

Posted by Simon Kitching <sk...@obsidium.com>.
Warren Bell wrote:
> Would I just use <t:messages layout="text"/> in my jsp where layout="text"
> would be implemented in my my.package.extend.MyMsgRenderer class that
> extends org.apache.myfaces.renderkit.html.HtmlMessagesRenderer?

Ah, now you're wanting to add extra properties to a component rather 
than just changing the renderer, and things get *much* more complicated.

First, you need to create a new .tld file with the new attribute defined 
in it, so that the JSP processor recognises the new attribute as valid.
You define your own namespace for this new tag library, eg 
"warren.com/taglib1" then add references to this taglib in your .jsp 
files with some unique prefix.

See tomahawk/tld/tomahawk.tld from the MyFaces source for an example tld.

However JSP provides no mechanism to "inherit" attributes, so your new 
TLD entry needs to contain a copy-and-paste of the entire set of 
attribute definitions from the parent tag's tld. Ecch. MyFaces work 
around this by using XML entity definitions, but many servlet engines 
(incl. tomcat) don't correctly process entity references in .tld files 
so a preprocessing stage that uses XSLT to expand the entities is needed.

Your tag class needs to have a setter for this new layout property, and 
update the component with it in its setProperties method.

Having written your tag, you then need to subclass the UIComponent to 
add your new field. Well, actually you can be sneaky and skip this step, 
  as a component has an attributes map that stores any "properties" that 
aren't defined via setter/getter methods on the UIComponent. Generally 
that's bad form I think but probably ok in this case.

Your custom renderer can then use component.getAttribute("layout") to 
determine what layout was defined via the JSP tag and do the right thing.

About now is when you start thinking about using Facelets instead of 
JSP. I have only read the basic docs, but avoiding all this JSP tag 
garbage can't be bad.

Regards,

Simon

RE: Messages as text only?

Posted by Warren Bell <wa...@clarksnutrition.com>.
Would I just use <t:messages layout="text"/> in my jsp where layout="text"
would be implemented in my my.package.extend.MyMsgRenderer class that
extends org.apache.myfaces.renderkit.html.HtmlMessagesRenderer?

> -----Original Message-----
> From: Simon Kitching [mailto:skitching@obsidium.com]
> Sent: Thursday, November 10, 2005 2:29 PM
> To: MyFaces Discussion
> Subject: Re: Messages as text only?
>
>
> Warren Bell wrote:
> > I am kinda playing around with extending
> > org.apache.myfaces.renderkit.html.HtmlMessagesRenderer. I found the
> > renderMessages method and see where I would call a new method that would
> > render the messages the way I want.
> >
> > private void renderText(FacesContext facesContext,
> >                             UIComponent messages,
> >                             MessagesIterator messagesIterator)
> >             throws IOException
> > {
> >   // format messages as text
> >   ...
> > }
> >
> > I am not quite sure how to proceed from there. Would I list the
> new class in
> > the faces-config file like this?
> >
> > <render-kit>
> >   	<render-kit-id>textMessages</render-kit-id>
> >
> <render-kit-class>my.package.extend.MyNewHtmlTextMessagesRenderer</render
> > -kit-class>
> > </render-kit>
>
> You don't want to override the *render kit* itself. You want to modify
> the html render kit's configuration. As the html render kit is the
> default render kit, you need:
>
>   <render-kit>
>     <renderer>
>       <component-family>javax.faces.Output</component-family>
>       <renderer-type>
>         org.apache.myfaces.renderkit.html.HtmlMessagesRenderer
>       </renderer-type>
>       <renderer-class>my.package.extend.MyMsgRenderer</renderer-class>
>      </renderer>
>   </render-kit>
>
>
> >
> > And then how would I actually use this in a page? Is there a good
> > tutorial/example of how to do this?
>
> The page doesn't need altering at all, as you haven't changed what JSP
> *tag* is being used, just what renderer is used.
>
> There's no tutorial on overriding renderers as far as I know. Maybe that
> would be a nice addition to:
>    http://wiki.apache.org/myfaces/
> even though it's really generic JSF stuff.
>
> The O'Reilly JavaServer Faces book is pretty good (I'd give it 7 out of
> 10) and does talk about this stuff.
>
> See class UIComponentBase method getRenderer for the real details of how
> a renderer is located for a component.
>
> Regards,
>
> Simon
>
> __________ NOD32 1.1283 (20051110) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


Re: Messages as text only?

Posted by Simon Kitching <sk...@obsidium.com>.
Warren Bell wrote:
> I am kinda playing around with extending
> org.apache.myfaces.renderkit.html.HtmlMessagesRenderer. I found the
> renderMessages method and see where I would call a new method that would
> render the messages the way I want.
> 
> private void renderText(FacesContext facesContext,
>                             UIComponent messages,
>                             MessagesIterator messagesIterator)
>             throws IOException
> {
>   // format messages as text
>   ...
> }
> 
> I am not quite sure how to proceed from there. Would I list the new class in
> the faces-config file like this?
> 
> <render-kit>
>   	<render-kit-id>textMessages</render-kit-id>
>   	<render-kit-class>my.package.extend.MyNewHtmlTextMessagesRenderer</render
> -kit-class>
> </render-kit>

You don't want to override the *render kit* itself. You want to modify 
the html render kit's configuration. As the html render kit is the 
default render kit, you need:

  <render-kit>
    <renderer>
      <component-family>javax.faces.Output</component-family>
      <renderer-type>
        org.apache.myfaces.renderkit.html.HtmlMessagesRenderer
      </renderer-type>
      <renderer-class>my.package.extend.MyMsgRenderer</renderer-class>
     </renderer>
  </render-kit>


> 
> And then how would I actually use this in a page? Is there a good
> tutorial/example of how to do this?

The page doesn't need altering at all, as you haven't changed what JSP 
*tag* is being used, just what renderer is used.

There's no tutorial on overriding renderers as far as I know. Maybe that 
would be a nice addition to:
   http://wiki.apache.org/myfaces/
even though it's really generic JSF stuff.

The O'Reilly JavaServer Faces book is pretty good (I'd give it 7 out of 
10) and does talk about this stuff.

See class UIComponentBase method getRenderer for the real details of how 
a renderer is located for a component.

Regards,

Simon

RE: Messages as text only?

Posted by Warren Bell <wa...@clarksnutrition.com>.
I am kinda playing around with extending
org.apache.myfaces.renderkit.html.HtmlMessagesRenderer. I found the
renderMessages method and see where I would call a new method that would
render the messages the way I want.

private void renderText(FacesContext facesContext,
                            UIComponent messages,
                            MessagesIterator messagesIterator)
            throws IOException
{
  // format messages as text
  ...
}

I am not quite sure how to proceed from there. Would I list the new class in
the faces-config file like this?

<render-kit>
  	<render-kit-id>textMessages</render-kit-id>
  	<render-kit-class>my.package.extend.MyNewHtmlTextMessagesRenderer</render
-kit-class>
</render-kit>

And then how would I actually use this in a page? Is there a good
tutorial/example of how to do this?


> -----Original Message-----
> From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> Sent: Thursday, November 10, 2005 12:15 PM
> To: MyFaces Discussion
> Subject: Re: Messages as text only?
>
>
> I think it would require a custom renderer, but I don't think it's as
> difficult as you suspect.
>
> You'd need to subclass either
> org.apache.myfaces.renderkit.html.HtmlMessagesRenderer or
> org.apache.myfaces.renderkit.html.HtmlMessageRenderer and override
> renderSingleFacesMessage (actually in
> org.apache.myfaces.renderkit.html.HtmlMessageRendererBase).
>
> The new method would just be stripping out all of the styling
> information and outputting the javascript.
>
> Then you specify your new renderer class in the faces-config file.
>
> I've never written a custom renderer myself, but the process looks
> pretty simple.
>
> I suppose another way to do it would be to retrieve the messages out
> of the request map, and iterate over them manually, but that seems
> like a lot more difficult task.   A custom renderer seems easier and
> more maintainable.
>
> On 11/10/05, Warren Bell <wa...@clarksnutrition.com> wrote:
> > I would like to take messages and place them into some
> JavaScript less the
> > HTML. Is there a way to render messages as text only, or would I have to
> > create a custom rederer? Writing a custom renderer will not
> happen, I am way
> > to new to JSF.
> >
> > Thanks,
> >
> > Warren Bell
> >
> >


Re: Messages as text only?

Posted by Mike Kienenberger <mk...@gmail.com>.
I think it would require a custom renderer, but I don't think it's as
difficult as you suspect.

You'd need to subclass either
org.apache.myfaces.renderkit.html.HtmlMessagesRenderer or
org.apache.myfaces.renderkit.html.HtmlMessageRenderer and override
renderSingleFacesMessage (actually in
org.apache.myfaces.renderkit.html.HtmlMessageRendererBase).

The new method would just be stripping out all of the styling
information and outputting the javascript.

Then you specify your new renderer class in the faces-config file.

I've never written a custom renderer myself, but the process looks
pretty simple.

I suppose another way to do it would be to retrieve the messages out
of the request map, and iterate over them manually, but that seems
like a lot more difficult task.   A custom renderer seems easier and
more maintainable.

On 11/10/05, Warren Bell <wa...@clarksnutrition.com> wrote:
> I would like to take messages and place them into some JavaScript less the
> HTML. Is there a way to render messages as text only, or would I have to
> create a custom rederer? Writing a custom renderer will not happen, I am way
> to new to JSF.
>
> Thanks,
>
> Warren Bell
>
>