2

Anyone have any creative ideas for how I can solve this warning?

EL syntax error: Expression cannot start with binary operator

caused by the following code:

String.format("#{myController.deleteItemById(%d)}", getId())

My code looked like this before:

"#{myController.deleteItemById(" + getId() + ")}"

but this caused eclipse to generate the following warning:

EL syntax error: String is not closed

UPDATE:

@ManagedBean(name = "myController")
@ViewScoped
public class MyController implements Serializable {

  private long id;
  private HtmlPanelGroup panel;

  public long getId() {return this.id; }

  private void getPanel() {

  /// bunch of code for programatically creating a form

    HtmlCommandButton deleteButton = new HtmlCommandButton();
    deleteButton.setId(id);
    deleteButton.setValue(value);
    deleteButton.setActionExpression(/* EL expression used here */);
   }
}

<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <!-- some other elements removed for the sake of clarity -->      
    <h:body>
        <h:panelGroup binding="#{myController.panel}" />
    </h:body>
</html>
Mindwin Remember Monica
  • 1,469
  • 2
  • 20
  • 35
auser
  • 6,307
  • 13
  • 41
  • 63

3 Answers3

2

You can annotate with a @SupressWarnings("el-syntax") to the method, and Eclipse will stop nagging you. It will ask to supress the unknnown (to the Java Framework) supresswarnings, etc etc. If you don't let it supress, it will add a warning to the annotation instead.

.

But by building EL expressions at runtime you expose your application to EL Injection.

.

Someone could run malicious code on your server by injecting method calls and stuff.

Check if the origin of the string in the middle of the EL comes from a completely safe origin,

or sanitize your inputs to prevent it.

Mindwin Remember Monica
  • 1,469
  • 2
  • 20
  • 35
1

Your el expression, although valid is constructed at runtime. But Eclipse displays warnings at development/compile time. So it has now way to check at compile time what will return getId at run time in order to correctly validate the el expression.

For such a run time constructed expressions you can disable warnings from the IDE if they bother you. I would personally not disable them so they will always remember me that is something special with that el expression.

dcernahoschi
  • 14,968
  • 5
  • 37
  • 59
-1
"#{myController.deleteItemById(" + getId() + ")}"

in this piece of code what i understand is myController is your managed bean and deleteItemById is your method implemented in that managed Bean, just call your method and process whatever you want in that method, get your id and implement your CRUD operations if necessary, if it's in a datatable and you want to send the object you can use as following:

           <h:dataTable value="#{bean.list}" var="item">
                <h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
                <h:column><f:facet name="header">Value</f:facet>#{item.value}</h:column>
                <h:column><h:commandButton value="edit" action="#{bean.edit(item)}" /></h:column>
                <h:column><h:commandButton value="delete" action="#{bean.delete(item)}" /></h:column>
            </h:dataTable>

please refer here (BalusC's post "The benefits and pitfalls of @ViewScoped") for a complete tutorial

berkay
  • 3,907
  • 4
  • 36
  • 51