You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Kazuaki Miyauchi <mi...@gmail.com> on 2019/08/19 05:16:39 UTC

How to cast String to int in EL?

 To simple following table.
create table test (member_id int, member_name text);

 In JSP, I accessed as following.
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:param value="1"/>
 <sql:param value="Miyauchi"/>
</sql:update>

 I've gotten following error.(using Tomcat-9.0.22 and Postgres-11.5)
javax.servlet.jsp.JspException: insert into test values(?,?) : ERROR:
column "member_id" is of type integer but expression is of type
character varying Hint: You will need to rewrite or cast the
expression. Position: 27

 This JSP works under Tomcat-5.5.3 and Postgres-9.0.8.

And, I've gotten the same result using following JSP.

<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:param>1</sql:param>
 <sql:param value="Miyauchi"/>
</sql:update>

 How to cast String to int?

Of course, following JSP works correctly.

<sql:update var="stmt" dataSource="${kome}">
 insert into test values(1,?)
 <sql:param value="Miyauchi"/>
</sql:update>

Regards,                       Kazuaki Miyauchi, Japan

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


Re: How to cast String to int in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 This is also

<sql:param type="Integer" value="1"/>

is ideal interface for me.
Of course, default of type is String.

 Regards,                Kazuaki Miyauchi, Japan

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


Re: How to cast String to int in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 This is only amusing. I tried at first as following.

<c:set var="member_id" value="1" scope="session"/>

 <sql:param value="${${member_id}}"/>

 If EL supports nested description, this is graceful solution.

 Of course, it caused
javax.el.ELException: Failed to parse the expression [${${member_id}}]

 Regards,                Kazuaki Miyauchi, Japan

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


Re: How to cast String to int in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Just be careful. If there is no paean passed, I expect it silently
evaluates to 0.

On Tue, Aug 20, 2019, 05:09 Kazuaki Miyauchi <mi...@gmail.com> wrote:

>  Thank you, Stuart! This is what I want to know.
>
> 2019-08-20 16:48 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> > Ah, it is parameter casting you're after. That was unclear from your
> > initial request. What does:
> > <sql:param value="${0 + param.member_id}"/>
> > yield?
>
>  Following correctly works!!
>
> <c:set var="member_id" value="1" scope="session"/>
> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:param value="${0 + member_id}"/>
>  <sql:param value="Miyauchi"/>
> </sql:update>
>
>  Regards,                     Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>
>

Re: How to cast String to int in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 Thank you, Stuart! This is what I want to know.

2019-08-20 16:48 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> Ah, it is parameter casting you're after. That was unclear from your
> initial request. What does:
> <sql:param value="${0 + param.member_id}"/>
> yield?

 Following correctly works!!

<c:set var="member_id" value="1" scope="session"/>
<sql:setDataSource dataSource="jdbc/kome" var="kome"/>
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:param value="${0 + member_id}"/>
 <sql:param value="Miyauchi"/>
</sql:update>

 Regards,                     Kazuaki Miyauchi, Japan

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


Re: How to cast String to int in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Ah, it is parameter casting you're after. That was unclear from your
initial request. What does:
<sql:param value="${0 + param.member_id}"/>
yield?





On Tue, Aug 20, 2019, 02:07 Kazuaki Miyauchi <mi...@gmail.com> wrote:

>  Hi, Stuart
>
> 2019-08-19 20:32 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> > I can't speak to Tomcat 5, that's been years. However, the code you're
> > using is not passing an integer. If you take a look at the generated
> > Java source (which is a mess with taglibs) you may see why. Once more,
> > a quick google suggests some solutions, in particular, the trivial
> > one: <sql:param value="${1}"/>
>
>  Following source works correctly, of course.
>
> <c:set var="member_id" value="1"/>
> <% int member_id = (int)session.getAttribute("member_id");
>    session.setAttribute("member_id_num", member_id); %>
> <sql:setDataSource dataSource="jdbc/kome" var="kome"/>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:param value="${member_id_num}"/>
>  <sql:param value="Miyauchi"/>
> </sql:update>
>
>  Ordinary we get HTTP request parameter via EL using ${param.hogehoge}.
> So, It's troublesome to write following.
>     int member_id = Integer.valueOf(request.getAttribute("member_id");
>     session.setAttribute("member_id_num", member_id);
>
>  Instead of <sql:param value="${param.member_id}"/>
>
>  Isn't there more convenient way?
>
>  Regards,                             Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>
>

Re: How to cast String to int in EL?

Posted by Kazuaki Miyauchi <mi...@gmail.com>.
 Hi, Stuart

2019-08-19 20:32 GMT+09:00, Stuart Thiel <st...@gmail.com>:
> I can't speak to Tomcat 5, that's been years. However, the code you're
> using is not passing an integer. If you take a look at the generated
> Java source (which is a mess with taglibs) you may see why. Once more,
> a quick google suggests some solutions, in particular, the trivial
> one: <sql:param value="${1}"/>

 Following source works correctly, of course.

<c:set var="member_id" value="1"/>
<% int member_id = (int)session.getAttribute("member_id");
   session.setAttribute("member_id_num", member_id); %>
<sql:setDataSource dataSource="jdbc/kome" var="kome"/>
<sql:update var="stmt" dataSource="${kome}">
 insert into test values(?,?)
 <sql:param value="${member_id_num}"/>
 <sql:param value="Miyauchi"/>
</sql:update>

 Ordinary we get HTTP request parameter via EL using ${param.hogehoge}.
So, It's troublesome to write following.
    int member_id = Integer.valueOf(request.getAttribute("member_id");
    session.setAttribute("member_id_num", member_id);

 Instead of <sql:param value="${param.member_id}"/>

 Isn't there more convenient way?

 Regards,                             Kazuaki Miyauchi, Japan

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


Re: How to cast String to int in EL?

Posted by Stuart Thiel <st...@gmail.com>.
Hello Kazuaki,

I can't speak to Tomcat 5, that's been years. However, the code you're
using is not passing an integer. If you take a look at the generated
Java source (which is a mess with taglibs) you may see why. Once more,
a quick google suggests some solutions, in particular, the trivial
one: <sql:param value="${1}"/>

Three things:
 * Looking at the source for the version of sql:param might be
instructive in telling you what it's doing, how it's thinking about
taking what is passed.
 * fmt:parseNumber isn't something I've ever used, since I prepare my
data elsewhere before ever getting to a JSP, but it might answer your
question as stated (though I think not what you were looking for).
 * The change between Java versions, if exactly so, is a bit
bothersome. I'm unsure why it changed or the motivation for such a
change. It felt like Java (and in particular JSP and many standard
taglibs) were heading towards sensible intuition of intent, but
they've clearly veered back towards the explicit expression of intent,
which can seem bulky (I've noted it elsewhere as well). Maybe there
are ways to make it nicer, but I've been having to change my coding
style so it would bug me less (arguably following better practices).

On Mon, Aug 19, 2019 at 1:17 AM Kazuaki Miyauchi <mi...@gmail.com> wrote:
>
>  To simple following table.
> create table test (member_id int, member_name text);
>
>  In JSP, I accessed as following.
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:param value="1"/>
>  <sql:param value="Miyauchi"/>
> </sql:update>
>
>  I've gotten following error.(using Tomcat-9.0.22 and Postgres-11.5)
> javax.servlet.jsp.JspException: insert into test values(?,?) : ERROR:
> column "member_id" is of type integer but expression is of type
> character varying Hint: You will need to rewrite or cast the
> expression. Position: 27
>
>  This JSP works under Tomcat-5.5.3 and Postgres-9.0.8.
>
> And, I've gotten the same result using following JSP.
>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(?,?)
>  <sql:param>1</sql:param>
>  <sql:param value="Miyauchi"/>
> </sql:update>
>
>  How to cast String to int?
>
> Of course, following JSP works correctly.
>
> <sql:update var="stmt" dataSource="${kome}">
>  insert into test values(1,?)
>  <sql:param value="Miyauchi"/>
> </sql:update>
>
> Regards,                       Kazuaki Miyauchi, Japan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: taglibs-user-help@tomcat.apache.org
>


-- 
Stuart Thiel, P. Eng.

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