有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java EL语法错误:表达式不能以二进制运算符开头

有人对我如何解决这个警告有什么创造性的想法吗

EL syntax error: Expression cannot start with binary operator

由以下代码引起:

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

我的代码以前是这样的:

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

但这导致eclipse生成以下警告:

EL syntax error: String is not closed

更新:

@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>

共 (3) 个答案

  1. # 1 楼答案

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

    在这段代码中,我理解的是myController是您的托管bean,deleteItemById是您在该托管bean中实现的方法,只需调用您的方法并在该方法中处理您想要的任何内容,获取您的id并在必要时实现CRUD操作,如果它位于数据表中,并且您希望发送对象,则可以按如下方式使用:

               <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>
    

    请参阅here(BalusC的帖子“@ViewScoped的优点和缺点”),以获取完整的教程

  2. # 2 楼答案

    您可以使用@SupressWarnings("el-syntax")对该方法进行注释,Eclipse将不再纠缠您。它将要求取消未知(Java框架)的SuppressWarnings等。如果您不让它取消,它将向注释添加一个警告

    但是通过在运行时构建EL表达式,您可以将应用程序公开给EL Injection

    有人可能通过注入方法调用和其他东西在您的服务器上运行恶意代码

    检查EL中间的字符串的起源是否来自一个完全安全的起源,

    或者sanitize your inputs来阻止它

  3. # 3 楼答案

    您的el表达式(尽管有效)是在运行时构造的。但是Eclipse在开发/编译时显示警告。因此,它现在可以在编译时检查在运行时返回getId的内容,以便正确验证el表达式

    对于这种运行时构造的表达式,如果IDE中的警告困扰您,您可以禁用它们。我个人不会禁用它们,所以他们会永远记住我,这是el表达式的特殊之处