【问题标题】:javax.el.PropertyNotWritableException: Illegal Syntax for Set Operationjavax.el.PropertyNotWritableException:集合操作的非法语法
【发布时间】:2013-01-10 11:59:48
【问题描述】:

我正在 JSF 中为学校做一个项目,我对此几乎没有任何问题。

首先,当我想通过推送将自己重定向到项目的另一个页面时,h:commandButton 无法正常工作。显示错误:

javax.el.PropertyNotWritableException:设置操作的非法语法

index.jsp:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
  <head>
<title>Firma Komputerowa - Baza Danych</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/table-style.css" />
  </head>
     <body>
       <f:view>
         <h:form>   
        <h:panelGrid columns="3">
     <h:commandButton action="#{producentBean.redirect }" value="Dodaj"/>
</h:panelGrid>
<h:panelGrid>
    <h:dataTable value="#{producentBean.List}" var="p" 
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row" border="1" > 
        <h:column>
            <f:facet name="header">ID producenta</f:facet>
            <h:inputText value="#{p.getId}"/>
        </h:column>

        <h:column>
            <f:facet name="header">Nazwa producenta</f:facet>
            <h:inputText value="#{p.getNazwa}" />
        </h:column>

        <h:column>
            <f:facet name="header">NIP</f:facet>
            <h:inputText value="#{p.getNip}" />
        </h:column>

        <h:column>
            <f:facet name="header">REGON</f:facet>
            <h:inputText value="#{p.getRegon}" />
        </h:column>

        <h:column>
            <f:facet name="header">Miasto</f:facet>
            <h:inputText value="#{p.getMiasto}" />
        </h:column>

        <h:column>
            <f:facet name="header">Adres</f:facet>
            <h:inputText value="#{p.getAdres}" />
        </h:column>

        <h:column>
            <f:facet name="header">Telefon</f:facet>
            <h:inputText value="#{p.getTel}" />
        </h:column>

        <h:column>
            <f:facet name="header">E-mail</f:facet>
            <h:inputText value="#{p.getEmail}" />
        </h:column>
    </h:dataTable>
</h:panelGrid>
        </h:form>
       </f:view>
    ...
   </body>
</html>

ProducentBean.java:

    public class ProducentBean extends Polacz implements Serializable {

   public String redirect() {
    return "dodaj_pr";
}
}

我尝试了很多方法,但我无法处理它。

我还有另一个问题:在 JSF 中是否有可能从数据库中动态显示一个表,例如通过选择一个表名来选择一个选项?我的数据库包含三个表,每个表的列数不同。

如果有人给我一些建议,我会很高兴。


编辑:

好的,我尝试这样做:

private String dodajPrUrl = "dodaj_pr?faces-redirect=true";

public String getDodajPrUrl() {
    return this.dodajPrUrl;
}

public void setDodajPrUrl(String newUrl) {
    this.dodajPrUrl = newUrl;
}

在 index.jsp 中:

<h:commandButton action="#{producentBean.redirect }" value="Dodaj" />

它仍然给我同样的错误。

编辑2:

堆栈跟踪:

SEVERE: javax.faces.FacesException: javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
javax.faces.FacesException: javax.el.PropertyNotWritableException: Illegal Syntax for  Set Operation
    at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:142)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.component.UpdateModelException: javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
    at javax.faces.component.UIInput.updateModel(UIInput.java:853)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:735)
    at javax.faces.component.UIData.iterate(UIData.java:2001)
    at javax.faces.component.UIData.processUpdates(UIData.java:1253)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1231)
    at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    ... 26 more
Caused by: javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
    at com.sun.el.parser.AstValue.setValue(AstValue.java:207)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:291)
    at javax.faces.component.UIInput.updateModel(UIInput.java:818)
    ... 35 more

【问题讨论】:

  • redirect 方法应该返回 String 而不是 void。您收到异常是因为您尝试在视图中绑定 value 而不使用支持 bean 中的 javabean 约定(getter 和 setter)来暴露它
  • 你需要通过一些 JSF hello world。开始here
  • 我跟着本教程,尤其是 JSF 2.0 中的隐式导航部分,但我走哪条路都没有关系 - 仍然显示错误。我将 JSF2.0 与 Glassfish 3.1.2.2 和 Eclipse Juno 一起使用。该文件具有 .jsp 扩展名并映射到 .jsf。拜托,你能告诉我,问题是什么以及如何解决它?我真的做了我能做的一切。
  • 你能告诉我们&lt;form&gt;..&lt;/form&gt;中的完整代码,以及bean中的getter和setter吗?
  • @user2014064,至少你已经做了一些研究。在此处发布完整的堆栈跟踪以及您的 bean 代码

标签: jsf


【解决方案1】:

查看堆栈跟踪。这是最重要的部分:

Caused by: javax.faces.component.UpdateModelException: javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
    at javax.faces.component.UIInput.updateModel(UIInput.java:853)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:735)
    at javax.faces.component.UIData.iterate(UIData.java:2001)
    at javax.faces.component.UIData.processUpdates(UIData.java:1253)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1231)
    at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    ... 26 more

从下往上阅读。在更新模型值阶段,JSF 从UIViewRoot 组件(&lt;f:view&gt;)开始遍历组件树。在 UIForm 组件 (&lt;h:form&gt;) 中有一个 UIData 组件 (&lt;h:dataTable&gt;) 有一个需要处理的 UIInput 组件 (&lt;h:inputText&gt;)。 updateModel() 基本上需要将提交、转换和验证的值放入模型(托管bean)中。但是,该操作失败了,因为该属性不可写(即根据 EL 表达式语法找不到 setter 方法)。

好吧,让我们看一下&lt;h:inputText&gt; 组件的一般属性。

<h:inputText value="#{p.getId}"/>
...
<h:inputText value="#{p.getNazwa}" />
...
<h:inputText value="#{p.getNip}" />

等等,这很奇怪。他们都是从“get”开始的!这绝对不正常。然后,JSF 正在寻找像 setGetId()setGetNazwa() 等没有意义的方法(鉴于此例外,您可能也没有)。预计它们代表具体的属性名称,如“id”、“nazwa”、“nip”等,而不是完整的方法名称。

因此,在模型中:

private Long id;
private String nazwa;
private String nip;

// Add/generate getters and setters in the following syntax:

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

// ...

所以在视图中:

<h:inputText value="#{p.id}"/>
...
<h:inputText value="#{p.nazwa}" />
...
<h:inputText value="#{p.nip}" />

然而,这已经包含在有点理智的 JSF 书籍/教程/资源中。你绝对肯定你读的是正确的吗?您的问题还表明您将 JSF 2.0 与 已弃用 JSP 视图技术结合使用,而不是其后继 Facelets。从our JSF wiki page开始。

【讨论】:

  • 好的,我尝试重新制作我的 Bean 和索引文件。不幸的是,我不得不将 JSF 与 JSP 结合起来,因为那是我怪老师的“命令”。
  • 纠正我解决了我的问题,非常感谢您;)出于任何原因,我错过了您的 JSF Wiki 页面,并且我正在混合来自许多不同指南的建议。现在,是时候在我的项目中解决更大的问题了;)
  • 不客气。将问题标签悬停在黑框出现,然后单击 info 链接即可找到 wiki 页面。
【解决方案2】:

您在这里遇到的问题很少。首先,在您的输入组件中,您有类似p.getId 的值。在您的 bean 中,您可能拥有名为 getIdsetId 的方法,但这意味着您拥有名为 id 的属性,您可以对其进行读写。因此,将您的值更改为:p.idp.nazwap.nip... 您可以看到我使用的都是小写字母,而不是 Id 而是 id .

其次,您应该将您的操作方法更改为类似这样(如果您想要重定向):

public String redirect() {
  return "dodaj_pr?faces-redirect=true";
}

命令按钮标记如下:

<h:commandButton action="#{producentBean.redirect}" value="Dodaj" />

【讨论】:

    【解决方案3】:

    我也犯了同样的错误。错误是EL中inputext的value属性中}和"之间有空格,如下图。

    <h:inputText id="txtNroOficio" value="# {bean.nombre}  "   />
    

    我删除了空格,它起作用了。

    【讨论】:

    • 下面在哪里?我什么都没看到。您需要编辑您的答案
    • 对不起,这是代码。
    • 我还是改进了它。看看如何在答案中显示代码的差异
    • Kukeltje 谢谢
    【解决方案4】:

    不要在命令按钮中添加 ()。在您的 bean 方法中返回一些字符串并使用导航移动到另一个页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2011-06-04
      • 2017-07-02
      • 2012-03-18
      • 2017-02-21
      • 2014-08-13
      相关资源
      最近更新 更多