EL equivalent of <%= object.method(parameter) %> in WAS 8?

假如想象 提交于 2019-12-08 13:06:01

问题


The documentation says parameters are supported but the example uses hl and # which are unknown to me where I use c: and $ instead of this which is from the docs:

<h:inputText value="#{userNumberBean.userNumber('5')}">

All I know about $ vs # in EL is that is has to do with rvalue and lvalue which I can need a further explanation when to use the # style. I'm looking for how an EL expression of typ $... can take a parameter and how to call non-getters non-setters with a parameter for example fetching a constant static string from a bean in the request context.

What does # do compared to $? How do I use parameters in EL if I want to use the $? The scriptlets I want to migrate to EL are something like <%= Constants.CONSTANT %>and <%= object.method(parameter) %> that I want to do in EL.


回答1:


The #{} syntax is supposed to be used in the Java EE's MVC framework JSF only. See also Difference between JSP EL, JSF EL and Unified EL. Just stick to ${} syntax in old JSP.

As to invoking methods with arguments, this is introduced in Servlet 3.0 / EL 2.2. WAS 8 is a Servlet 3.0 container, so it should support it out the box. You only need to make sure that your web.xml is declared conform Servlet 3.0 specification version. Otherwise the container will run in fallback modus matching the version indicated by web.xml. The proper Servlet 3.0 web.xml declaration look like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->

</web-app>

This way the ${object.method(param)} must work.

Referencing constants in EL is a different story. This will be introduced in the upcoming EL 3.0, which is part of the future Java EE 7 / Servlet 3.1 (and likely the future WAS 9 or 10 would support it). There are solutions in flavor of custom taglibs or by keeping them in a Map, all explained in detail in this answer: How to reference constants in EL?

See also:

  • Our EL wiki page


来源:https://stackoverflow.com/questions/10742174/el-equivalent-of-object-methodparameter-in-was-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!