Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to define an Integer using <bean:define>

 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please check THIS link.

I think you need property attribute in your tag.
 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try below,
 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic