This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes How to define an Integer using <bean:define> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to define an Integer using <bean:define>" Watch "How to define an Integer using <bean:define>" New topic
Author

How to define an Integer using <bean:define>

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Hi,
Iam trynig to define an integer variable in JSP using the following struts tag

<bean:define id="intVar" type="java.lang.Integer" value="1" />
<bean:write name="intVar" />

Iam getting following error while runningt the JSP

org.apache.jasper.JasperException: java.lang.String
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Am i doing any mistake here.
Please let me know how to declare and use an integer variable using struts <bean:define> tag?

Thanks.

Regards
Senthil Kumar Sekar



Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Hi,

Please check THIS link.

I think you need property attribute in your tag.


Thanks,
Shailesh
Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Iam not sure if you had got my clarification correct.If not its this.

I need to declare an Integer variable in JSP using <bean:define>.

I think the property tag is used to refer a property(instance variable) name of a bean.

Please correct me if iam wrong and let me know the solution for this.

Thanks.

Regards
Senthil Kumar Sekar
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Please check the link provided in previous post in that also there is no instance variable, but still they are using property tag. I guess you would try with property attribute.
Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Actually i don't have any issue in declaring a String variable using

<bean:define id="stringVar" type="java.lang.String" value="Hello" />
<bean:write name="stringVar" />

The value "Hello" will be written to the output without any issues.

But the issue iam facing is, i am not able to declare an integer variable using

<bean:define id="intVar" type="java.lang.Integer" value="1" />
<bean:write name="intVar" />

Iam getting following error while running the jsp.
org.apache.jasper.JasperException: java.lang.String
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


You got my issue which iam facing here?

Thanks.

Regards
Senthil Kumar Sekar
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Please try below,
Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

@Rajagopal

I think that will work, but it will take it as a String and when we try to assign to an integer variable, it will throw a ClassCastException.

Thanks.

Regards
Senthil Kumar Sekar
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

It won't throw ClassCastException since default type would be java.lang.Object. Moreover, if you want to just display numeric, you can use this code syntax.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Hi,

Actually in defineTag value attribute is of String type (not an object).due to that while setting that value in scope (In your case it is a page scope) it is set as a String object with value as "1".
but while defining bean: define tag, in servlet created by server for that jsp there is code like below


due to above code, you are getting ClassCastException.(as value in page scope is of String type)

you can do one thing if you want to resolve this issue, try below code,



but as in 2011 that is no good practice to use scriptlet in your JSP page, you can create Integer in any scope using JSTL, as I am not much aware of JSTL I have done this using a scriptlet.

 
GeeCON Prague 2014
 
subject: How to define an Integer using <bean:define>