Vegard
Vegard

Reputation: 1942

EL out of attribute in IntelliJ

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

Upvotes: 4

Views: 3454

Answers (3)

wst
wst

Reputation: 4338

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.

Upvotes: 6

dbarton_uk
dbarton_uk

Reputation: 1067

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.

Upvotes: 4

rbento
rbento

Reputation: 11678

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.

Upvotes: 2

Related Questions