3

In intelliJ I get the error message "EL out of attribute" for the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:body>
    <h1>JSF and Spring</h1>
    #{helloBean.hello()}
</h:body>
</html>

apparently this is nonstandard usage of EL extension, but I am having a hard time understanding how I should do this instead. The code I have works just fine, but I like use the "correct" way, and warnings in IntelliJ probably means there is something I am missing.

How should I have written this to be "correct" JSF 2?

enter image description here

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vegard
  • 1,802
  • 2
  • 21
  • 34

3 Answers3

6

I might be wrong here, but out of attribute suggest you shouldn't use EL in any random place in your template, but within tag's attribute only.

Here's bean:

@ViewScoped
@ManagedBean
public class TestBean {
    private String message = "Hello world";

    public TestBean() {
        System.out.println("TestBean instantiated.");
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String fetchMsg() {
        return "Msg fetched: " + message;
    }
}

Here's XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>JSF Tutorial!</title>
</h:head>
<h:body>
    <!--No warning message-->
    <h:outputText value="#{testBean.message}"/>
    <h:outputText value="#{testBean.fetchMsg()}"/>

    <!--Warning message-->
    #{testBean.fetchMsg()}
</h:body>
</html>

Using Idea 14.

wst
  • 4,040
  • 4
  • 41
  • 59
4

I had the same issue. I replaced the # with a $ and the IntelliJ no longer complained. $ can be used in the case of read only attributes - I guess IntelliJ enforces a stricter syntax.

dbarton_uk
  • 980
  • 11
  • 17
  • 2
    Nope. IntelliJ is just not as intelligent as it pretends to be. No one IDE is, by the way. Basically every single Java targeted IDE has quite some epic failures as to how it validates EL. Just read EL spec at jcp.org to learn how to write valid EL and then disable EL validation in IDE to avoid annoyances. – BalusC Sep 15 '14 at 06:33
  • 2
    more reading: http://stackoverflow.com/questions/8791328/what-are-the-differences-between-and – jordanpg Jun 27 '15 at 18:33
2

Strictly answering your question, to be 'correct' you would write something like this:

A bean:

@ViewScoped
public class HelloBean {
    public String getHello() {
        return "Whoa!";
    }
}

And in your view:

...
<h:body>
    #{helloBean.hello}
</h:body>
...

Although you can call methods in EL 2.1+ like you did, previous versions of EL would not allow this.

In this example you may notice that for a method called getHello in a bean, one may use it without the get like in #{helloBean.hello} because EL will find the method and implicitly call the get.

About the IDE showing warning for seemingly correct code, you could check if the project's library versions that are configured match the version you actually want to work on.

For instance, you want to work with JSF 2.x but the project is set to work on 1.x. This may happen.

I hope it helps.

rbento
  • 9,919
  • 3
  • 61
  • 61
  • so what I wrote originally is according to the standard? but IntelliJ is wrongly saying it is not? I tried what you said, but IntelliJ does not like that approach either. (see updated screenshot) – Vegard Jan 26 '14 at 20:12
  • Do you have a HelloBean with a getHello() method? I'll update my answer to make it clearer – rbento Jan 26 '14 at 20:17
  • yes, I have, and the code also works, but I am wondering if I am doing this in a messy way since IntelliJ complains it is out of attribute. – Vegard Jan 26 '14 at 20:21
  • Sorry, I noticed you're using Spring. I've never used Spring but I use IntelliJ for JSF and never had this problem. Maybe it's something about your project setup. – rbento Jan 26 '14 at 20:40