我想使用jsf在数据库中插入记录,但它给出了javax.faces.FacesException的错误

时间:2014-08-21 07:42:12

标签: jsf primefaces

我想使用jsf在数据库中插入记录但是它给出了javax.faces.FacesException的错误。

控制台窗口: -

javax.faces.FacesException: #{privilegeManagedBean.insertPrivilegeBean}:     javax.el.MethodNotFoundException: /privilegepage.xhtml @39,59 action="#{privilegeManagedBean.insertPrivilegeBean}": Method not found: com.bis.ManagedBean.PrivilegeManagedBean@e37689.insertPrivilegeBean()
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:110)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
Caused by: javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /privilegepage.xhtml @39,59 action="#{privilegeManagedBean.insertPrivilegeBean}": Method not found: com.bis.ManagedBean.PrivilegeManagedBean@e37689.insertPrivilegeBean()
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:92)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 26 more
Caused by: javax.el.MethodNotFoundException: /privilegepage.xhtml @39,59 action="#{privilegeManagedBean.insertPrivilegeBean}": Method not found: com.bis.ManagedBean.PrivilegeManagedBean@e37689.insertPrivilegeBean()
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 27 more

我的bean:-Privilege.java

public class Privilege {

private int id;
private String privilege;

public Privilege() {
}

public Privilege(int id, String privilege) {
    this.id = id;
    this.privilege = privilege;
}

public int getId() {
    return id;
}

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

public String getPrivilege() {
    return privilege;
}

public void setPrivilege(String privilege) {
    this.privilege = privilege;
}

}

PrivilegeDao.java

public class PrivilegeDao implements Serializable {
List<Privilege> lst;
List<Privilege> userList;
private static final long serialVersionUID = 1L;

public PrivilegeDao() {

}

public int addPrivilege(String privilege) {
    PreparedStatement preparedStatement = null;
    String sqlprivilege;
    Connection dbConnection = null;
    int pinsert = 0;
    try {
        sqlprivilege = "insert into privilege(privilege) values(?)";
        dbConnection = ConnectionDao.getDBConnection();
        preparedStatement = dbConnection.prepareStatement(sqlprivilege);
        preparedStatement.setString(1, privilege);
        pinsert = preparedStatement.executeUpdate();
        System.out.println("privilege is add and name is:- " +privilege);
    } catch (SQLException e) {

        System.out.println(e.getMessage());

    } finally {

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (dbConnection != null) {
            try {
                dbConnection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return pinsert;
}
}

ManagedBean: - PrivilegeManagedBean.java

@ManagedBean(name = "privilegeManagedBean", eager = true)
@SessionScoped
public class PrivilegeManagedBean {
private int id;
private String privilege;
private PrivilegeDao pdao;
@SuppressWarnings("unused")
private int insertPrivilegeBean;

public PrivilegeManagedBean() {
    pdao = new PrivilegeDao();
    insertPrivilegeBean=0;
}

public int getId() {
    return id;
}

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

public String getPrivilege() {
    return privilege;
}

public void setPrivilege(String privilege) {
    this.privilege = privilege;
}
public int getInsertPrivilegeBean() {
    return this.insertPrivilegeBean=pdao.addPrivilege(privilege);
}

public void setInsertPrivilegeBean(int insertPrivilegeBean) {
    this.insertPrivilegeBean = insertPrivilegeBean;
}

}

Privilege.xhtml

<h:panelGrid id="pnlgrdlogin">
        <p:column headerText="Add Privileges">
            <p:inputText value="#{privilegeManagedBean.privilege}" name="txtprivilege" id="txtprivilege"/>
            <br></br>
            <br></br>
            <p:commandButton name="btmaddprivilege" value="Add Privilege" id="btmaddprivilege"
                action="#{privilegeManagedBean.insertPrivilegeBean}">

            </p:commandButton>
        </p:column>
    </h:panelGrid>

我尝试在数据库中插入记录但是它给出了javax.faces.FacesException错误 我无法理解错误。

1 个答案:

答案 0 :(得分:0)

问题是,ManagedBean中没有名为insertPrivilegeBean的方法。

Caused by: javax.el.MethodNotFoundException: 
/privilegepage.xhtml @39,59 action="#{privilegeManagedBean.insertPrivilegeBean}":
Method not found: 
com.bis.ManagedBean.PrivilegeManagedBean@e37689.insertPrivilegeBean()

显然,您希望对数据库执行插入操作。因此,只需在ManagedBean中创建一个处理此操作的void方法,并在commandButton中调用此方法,如下所示:

在ManagedBean中:

public void doInsert() {
    this.pdao.addPrivilege(this.privilege);
}

在JSF文件中:

<p:commandButton 
    name="btmaddprivilege" 
    value="Add Privilege" 
    id="btmaddprivilege"
    action="#{privilegeManagedBean.doInsert()}"
/>