我在jstl中使用条件运算符时遇到了麻烦。如果用户已登录,我想在表格单元格中显示用户名。代码如下:
<td width="22%"><div align="center"></div>
<c:out value="${username}" />
</td>
<td width="11%"><div align="center"></div>
${"${username != ''}" ? <a href="logout.jsp" title="Logout">Logout</a> : <a href="login.jsp" title="Login">Login</a> }
</td>
上面代码的正确语法是什么?
修改
抛出的语法异常是:
19:30:19,941 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/BooksPoint].[jsp]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: org.apache.el.parser.ParseException: Encountered " "?" "? "" at line 1, column 23.
Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
"<=" ...
"le" ...
"==" ...
"eq" ...
"!=" ...
"ne" ...
"&&" ...
"and" ...
"||" ...
"or" ...
"*" ...
"+" ...
"-" ...
"/" ...
"div" ...
"%" ...
"mod" ...
at org.apache.el.parser.ELParser.generateParseException(ELParser.java:2217) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.ELParser.jj_consume_token(ELParser.java:2099) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.ELParser.DynamicExpression(ELParser.java:150) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.ELParser.CompositeExpression(ELParser.java:46) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:123) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:184) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:229) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:67) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:934) [jbossweb-7.0.13.Final.jar:]
at org.apache.jsp.register_jsp._jspService(register_jsp.java:92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_45]
答案 0 :(得分:0)
您不能嵌套EL,但这应该适用于三元运算符:
${username != ''
? '<a href=\"logout.jsp\" title=\"Logout\">Logout</a>'
: '<a href=\"login.jsp\" title=\"Login\">Login</a>'}