开发者

Trying to print a session variable in a jsp causes error "equals expected"

开发者 https://www.devze.com 2023-01-19 07:25 出处:网络
I am trying to pass a value from the session object to a custom tag <l:LoginStatus userId=\"<% out.print((String)session.getAttribute(\"userId\")); %>\"/>

I am trying to pass a value from the session object to a custom tag <l:LoginStatus userId="<% out.print((String)session.getAttribute("userId")); %>"/>

Why does this lin开发者_如何学JAVAe give me the error:

org.apache.jasper.JasperException: /index.jsp(1,1) /header.jsp(64,131) equal symbol expected

When I pass a hard coded value like this <l:LoginStatus userId="4"/>

Everything works fine.

It doesn't make any sense to me, I thought using out.print would make = uneccessary.


An alternative is to just use EL. That yields much cleaner code.

<l:LoginStatus userId="${userId}" />


It should be:

<%= (String)session.getAttribute("userId") %>

In general it is much better practice to do things this way, instead of writing directly to a page. Besides, things don't work exactly as you seem to think they do.


As you're printing the value of the an expression, you should make the statement as

<l:LoginStatus userId="<%=out.print((String)session.getAttribute("userId"))%>"/>

or

<l:LoginStatus userId="<%=out.print(session.getAttribute("userId")).toString()%>"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消