简体   繁体   English

尝试在jsp中打印会话变量会导致错误“等于预期”

[英]Trying to print a session variable in a jsp causes error “equals expected”

I am trying to pass a value from the session object to a custom tag <l:LoginStatus userId="<% out.print((String)session.getAttribute("userId")); %>"/> 我正在尝试将值从会话对象传递到自定义标记<l:LoginStatus userId="<% out.print((String)session.getAttribute("userId")); %>"/>

Why does this line give me the error: 为什么这行给我错误:
org.apache.jasper.JasperException: /index.jsp(1,1) /header.jsp(64,131) equal symbol expected org.apache.jasper.JasperException:/index.jsp(1,1)/header.jsp(64,131)应该等于符号

When I pass a hard coded value like this <l:LoginStatus userId="4"/> 当我传递像这样的硬编码值时<l:LoginStatus userId="4"/>

Everything works fine. 一切正常。

It doesn't make any sense to me, I thought using out.print would make = uneccessary. 对我来说这没有任何意义,我认为使用out.print会使=不必要。

An alternative is to just use EL . 另一种选择是只使用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()%>"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM