2012-09-01 29 views
1

任何人有任何創造性的想法可以解決此警告?EL語法錯誤:表達式不能以二元運算符開頭

引起的下列代碼:

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

我的代碼看起來像在此之前:

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

但這造成日食生成以下警告:

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

您使用任何框架jsf或只有jstl \ –

+0

如何/何時評估el表達式?你把它包含在一個jsp頁面或其他? – dcernahoschi

+0

或者您只會得到一個eclipse警告,並且在運行時表達式正確計算? – dcernahoschi

回答

-1
"#{myController.deleteItemById(" + getId() + ")}" 
在這段代碼是什麼我明白是myController的是你的託管bean和deleteItemById是在實現你的方法

管理的bean,只需調用無論你在那個方法要你的方法和步驟,讓您的ID並在必要時實施的CRUD操作,如果它在一個DataTable,你想送您可以按以下使用對象:

  <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的陷阱」)完整的教程

1

您的el表達式,儘管在運行時有效。但Eclipse在開發/編譯時會顯示警告。因此它現在有辦法在編譯時檢查在運行時將返回getId以正確驗證el表達式。

對於這樣的運行時構建的表達式,如果他們打擾你,可以禁用IDE中的警告。我個人不會禁用它們,所以他們會永遠記住我,那是el表達式中特別的東西。

2

您可以使用@SupressWarnings("el-syntax")註釋該方法,Eclipse將停止嘮叨你。它會要求禁止未知的(對Java框架)supresswarnings等等。如果你不讓它禁止,它會給註釋添加一個警告。

但是,通過在運行時構建EL表達式,將您的應用程序暴露給EL Injection

有人可以通過注入方法調用和東西在您的服務器上運行惡意代碼。

檢查在EL中間的串的起源來自一個完全安全的起源,

sanitize your inputs,以防止它。

相關問題