You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Phan, Hienthuc T" <hi...@merck.com> on 2008/07/17 23:04:42 UTC

Struts2: display int in JSP

Hi,

In my action, I have several variables of type int.  When the variable
is not initialized, it's displayed as 0 in the JSP.  I would like
un-initialized variable of type int to be display as blank/empty.  What
is the best way to accomplish this?  Thanks.

Public class XXAction extends ActionSupport
{
	private int age;
	private String name = new String("xyz");
	private String email;

	//getters & setters
}


JSP page would display:

Name:	xyz
Email:	
Age:	0


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.

Re: Struts2: display int in JSP

Posted by "Alberto A. Flores" <aa...@gmail.com>.
Please remember that because of java's autboxing, you shouldn't have 
much problems (other than the cases when you are expecting zero). So 
changing the type (from int to Integer) is really not a dramatic change.

Phan, Hienthuc T wrote:
> That works.  Thanks.  I'm migrating from Struts 1 & was hoping not have
> to change the data type.   
> 
> 
> -----Original Message-----
> From: Jishnu Viswanath [mailto:jishnu.viswanath@tavant.com] 
> Sent: Thursday, July 17, 2008 2:24 PM
> To: Struts Users Mailing List
> Subject: RE: Struts2: display int in JSP
> 
> Use Integer instead of int
> 
> Regards,
> 
> Jishnu Viswanath
> 
> Software Engineer
> 
> *(+9180)41190300 - 222(Ext) ll * ( + 91 ) 9731209330ll
> 
> Tavant Technologies Inc.,
> 
> www.tavant.com
> 
> PEOPLE :: PASSION :: EXCELLENCE
> 
> -----Original Message-----
> From: Phan, Hienthuc T [mailto:hienthuc_phan@merck.com] 
> Sent: Friday, July 18, 2008 2:35 AM
> To: Struts Users Mailing List
> Subject: Struts2: display int in JSP
> 
> 
> Hi,
> 
> In my action, I have several variables of type int.  When the variable
> is not initialized, it's displayed as 0 in the JSP.  I would like
> un-initialized variable of type int to be display as blank/empty.  What
> is the best way to accomplish this?  Thanks.
> 
> Public class XXAction extends ActionSupport
> {
> 	private int age;
> 	private String name = new String("xyz");
> 	private String email;
> 
> 	//getters & setters
> }
> 
> 
> JSP page would display:
> 
> Name:	xyz
> Email:	
> Age:	0
> 
> 
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
> New Jersey, USA 08889), and/or its affiliates (which may be known
> outside the United States as Merck Frosst, Merck Sharp & Dohme or
> MSD and in Japan, as Banyu - direct contact information for affiliates
> is
> available at http://www.merck.com/contact/contacts.html) that may be
> confidential, proprietary copyrighted and/or legally privileged. It is
> intended solely for the use of the individual or entity named on this
> message. If you are not the intended recipient, and have received this
> message in error, please notify us immediately by reply e-mail and
> then delete it from your system.
> Any comments or statements made in this email are not necessarily those
> of Tavant Technologies.
> The information transmitted is intended only for the person or entity to
> which it is addressed and may 
> contain confidential and/or privileged material. If you have received
> this in error, please contact the 
> sender and delete the material from any computer. All e-mails sent from
> or to Tavant Technologies 
> may be subject to our monitoring procedures.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
> New Jersey, USA 08889), and/or its affiliates (which may be known
> outside the United States as Merck Frosst, Merck Sharp & Dohme or
> MSD and in Japan, as Banyu - direct contact information for affiliates is
> available at http://www.merck.com/contact/contacts.html) that may be
> confidential, proprietary copyrighted and/or legally privileged. It is
> intended solely for the use of the individual or entity named on this
> message. If you are not the intended recipient, and have received this
> message in error, please notify us immediately by reply e-mail and
> then delete it from your system.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores



RE: Struts2: display int in JSP

Posted by Jishnu Viswanath <ji...@tavant.com>.
Its not because of struts, int can not contain null, so nearest possible
brother is 0 so they put 0. 


Ok, if you still want to use int, then put getter setter as String and
parse the input and output to integer and string.

int age = -1;


public String getAge(){
	if(age<0)
		return null;
	else
		return ""+age;
}

public String setAge(String age){
	if(age == null)
		this.age = 0
	else
		this.age = Integer.parseInt(age);
}


I think this solves your change of data type solves,

By the way one lesson I learned from experience was never use int,
float, boolean etc instead use Integer Float and Boolean etc.
Regards,

Jishnu Viswanath

Software Engineer

*(+9180)41190300 - 222(Ext) ll * ( + 91 ) 9731209330ll

Tavant Technologies Inc.,

www.tavant.com

PEOPLE :: PASSION :: EXCELLENCE


-----Original Message-----
From: Phan, Hienthuc T [mailto:hienthuc_phan@merck.com] 
Sent: Friday, July 18, 2008 4:28 AM
To: Struts Users Mailing List
Subject: RE: Struts2: display int in JSP


That works.  Thanks.  I'm migrating from Struts 1 & was hoping not have
to change the data type.   


-----Original Message-----
From: Jishnu Viswanath [mailto:jishnu.viswanath@tavant.com] 
Sent: Thursday, July 17, 2008 2:24 PM
To: Struts Users Mailing List
Subject: RE: Struts2: display int in JSP

Use Integer instead of int

Regards,

Jishnu Viswanath

Software Engineer

*(+9180)41190300 - 222(Ext) ll * ( + 91 ) 9731209330ll

Tavant Technologies Inc.,

www.tavant.com

PEOPLE :: PASSION :: EXCELLENCE

-----Original Message-----
From: Phan, Hienthuc T [mailto:hienthuc_phan@merck.com] 
Sent: Friday, July 18, 2008 2:35 AM
To: Struts Users Mailing List
Subject: Struts2: display int in JSP


Hi,

In my action, I have several variables of type int.  When the variable
is not initialized, it's displayed as 0 in the JSP.  I would like
un-initialized variable of type int to be display as blank/empty.  What
is the best way to accomplish this?  Thanks.

Public class XXAction extends ActionSupport
{
	private int age;
	private String name = new String("xyz");
	private String email;

	//getters & setters
}


JSP page would display:

Name:	xyz
Email:	
Age:	0


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates
is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.
Any comments or statements made in this email are not necessarily those
of Tavant Technologies.
The information transmitted is intended only for the person or entity to
which it is addressed and may 
contain confidential and/or privileged material. If you have received
this in error, please contact the 
sender and delete the material from any computer. All e-mails sent from
or to Tavant Technologies 
may be subject to our monitoring procedures.


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

Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates
is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.


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

Any comments or statements made in this email are not necessarily those of Tavant Technologies.
The information transmitted is intended only for the person or entity to which it is addressed and may 
contain confidential and/or privileged material. If you have received this in error, please contact the 
sender and delete the material from any computer. All e-mails sent from or to Tavant Technologies 
may be subject to our monitoring procedures.


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


RE: Struts2: display int in JSP

Posted by "Phan, Hienthuc T" <hi...@merck.com>.
That works.  Thanks.  I'm migrating from Struts 1 & was hoping not have
to change the data type.   


-----Original Message-----
From: Jishnu Viswanath [mailto:jishnu.viswanath@tavant.com] 
Sent: Thursday, July 17, 2008 2:24 PM
To: Struts Users Mailing List
Subject: RE: Struts2: display int in JSP

Use Integer instead of int

Regards,

Jishnu Viswanath

Software Engineer

*(+9180)41190300 - 222(Ext) ll * ( + 91 ) 9731209330ll

Tavant Technologies Inc.,

www.tavant.com

PEOPLE :: PASSION :: EXCELLENCE

-----Original Message-----
From: Phan, Hienthuc T [mailto:hienthuc_phan@merck.com] 
Sent: Friday, July 18, 2008 2:35 AM
To: Struts Users Mailing List
Subject: Struts2: display int in JSP


Hi,

In my action, I have several variables of type int.  When the variable
is not initialized, it's displayed as 0 in the JSP.  I would like
un-initialized variable of type int to be display as blank/empty.  What
is the best way to accomplish this?  Thanks.

Public class XXAction extends ActionSupport
{
	private int age;
	private String name = new String("xyz");
	private String email;

	//getters & setters
}


JSP page would display:

Name:	xyz
Email:	
Age:	0


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates
is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.
Any comments or statements made in this email are not necessarily those
of Tavant Technologies.
The information transmitted is intended only for the person or entity to
which it is addressed and may 
contain confidential and/or privileged material. If you have received
this in error, please contact the 
sender and delete the material from any computer. All e-mails sent from
or to Tavant Technologies 
may be subject to our monitoring procedures.


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

Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.


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


RE: Struts2: display int in JSP

Posted by Jishnu Viswanath <ji...@tavant.com>.
Use Integer instead of int

Regards,

Jishnu Viswanath

Software Engineer

*(+9180)41190300 - 222(Ext) ll * ( + 91 ) 9731209330ll

Tavant Technologies Inc.,

www.tavant.com

PEOPLE :: PASSION :: EXCELLENCE

-----Original Message-----
From: Phan, Hienthuc T [mailto:hienthuc_phan@merck.com] 
Sent: Friday, July 18, 2008 2:35 AM
To: Struts Users Mailing List
Subject: Struts2: display int in JSP


Hi,

In my action, I have several variables of type int.  When the variable
is not initialized, it's displayed as 0 in the JSP.  I would like
un-initialized variable of type int to be display as blank/empty.  What
is the best way to accomplish this?  Thanks.

Public class XXAction extends ActionSupport
{
	private int age;
	private String name = new String("xyz");
	private String email;

	//getters & setters
}


JSP page would display:

Name:	xyz
Email:	
Age:	0


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or
MSD and in Japan, as Banyu - direct contact information for affiliates
is
available at http://www.merck.com/contact/contacts.html) that may be
confidential, proprietary copyrighted and/or legally privileged. It is
intended solely for the use of the individual or entity named on this
message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and
then delete it from your system.
Any comments or statements made in this email are not necessarily those of Tavant Technologies.
The information transmitted is intended only for the person or entity to which it is addressed and may 
contain confidential and/or privileged material. If you have received this in error, please contact the 
sender and delete the material from any computer. All e-mails sent from or to Tavant Technologies 
may be subject to our monitoring procedures.


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