You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by nsg <ns...@trans-cosmos.com.cn> on 2002/03/14 07:07:56 UTC

Where and when do struts call the method named toString()?

Hi all,
in struts-example, Subscription.java file contains the following method:

/**
     * Return a String representation of this object.
     */
    public String toString() {

        StringBuffer sb = new StringBuffer("Subscription[username=");
        sb.append(username);
        if (host != null) {
            sb.append(", host=");
            sb.append(host);
        }
        if (user != null) {
            sb.append(", user=");
            sb.append(user.getUsername());
        }
        sb.append(", autoConnect=");
        sb.append(autoConnect);
        sb.append("]");
        return (sb.toString());

    }

I  can understand the method,but where and when does struts use it?


Thanks in advance

Laker 

Re: Where and when do struts call the method named toString()?

Posted by keithBacon <ke...@yahoo.com>.
If you do a <bean:write on an object & it's not a String struts will call it's
toString() method. Same in other situations where struts expects a String.
It's like when you do a System.out.println() java calls toString() on the
object if it's not a String already.
The default toString inherited from Object just prints classname@memoryaddress.
I'm a bit dubious about overriding toString() as sometimes it's good to see the
memory address of objects (like trying to determine if 2 objects are the same
or clones) so being able to use the default toString() is handy.
I use a print() method instead - probably my unique hang-up because it's common
to use toString like here.

 

--- nsg <ns...@trans-cosmos.com.cn> wrote:
> Hi all,
> in struts-example, Subscription.java file contains the following method:
> 
> /**
>      * Return a String representation of this object.
>      */
>     public String toString() {
> 
>         StringBuffer sb = new StringBuffer("Subscription[username=");
>         sb.append(username);
>         if (host != null) {
>             sb.append(", host=");
>             sb.append(host);
>         }
>         if (user != null) {
>             sb.append(", user=");
>             sb.append(user.getUsername());
>         }
>         sb.append(", autoConnect=");
>         sb.append(autoConnect);
>         sb.append("]");
>         return (sb.toString());
> 
>     }
> 
> I  can understand the method,but where and when does struts use it?
> 
> 
> Thanks in advance
> 
> Laker 
> 


=====
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Search the archive:-
http://www.mail-archive.com/struts-user%40jakarta.apache.org/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Keith Bacon - Looking for struts work - South-East UK.
phone UK 07960 011275

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>