我试图通过命令按钮将主键“userId”作为EL方法参数发送到我的托管bean来删除数据库条目。
我当前的代码看起来像
豆:
@ManagedBean
@RequestScoped
public class HibernateUserHandling
{
...
public void deleteUser(String userIdViaXhtml)
{
Session session = sessionFactory.openSession();
session.beginTransaction();
session.createQuery("DELETE FROM AddressDetails WHERE USERID = :userIdViaXhtml");
session.createQuery("DELETE FROM UserDetails WHERE USERID = :userIdViaXhtml");
session.getTransaction().commit();
session.close();
}
...
}
查看:
<html>
<body>
<ui:composition template="./generalTemplate.xhtml"
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">
<ui:define name="content">
<h:outputStylesheet library="css" name="cssHibernate.css" />
<h:form id="main">
<h:dataTable value="#{hibernateDataOutput.persons}" var="list"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
...
<f:facet name="header">Delete</f:facet>
<h:commandButton id="userId" action="#{hibernateUserHandling['deleteUser(list.userId)']}" image="delete.jpg" />
</h:column>
</h:dataTable>
...
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
我测试了更多方法,例如通过像在inputText中那样将值传递给bean中的单独变量,但没有任何效果。
我认为我的基本方法完全错误。
我收到以下错误:
/hibernate.xhtml @39,137 action="#{hibernateUserHandling.deleteUser(list.userId)}" Failed to parse the expression [#{hibernateUserHandling.deleteUser(list.userId)}]
堆栈追踪:
javax.faces.view.facelets.TagAttributeException: /hibernate.xhtml @39,137 action="#{hibernateUserHandling.deleteUser(list.userId)}" Failed to parse the expression [#{hibernateUserHandling.deleteUser(list.userId)}]
at com.sun.faces.facelets.tag.TagAttributeImpl.getMethodExpression(TagAttributeImpl.java:232)
at com.sun.faces.facelets.tag.jsf.ActionSourceRule$ActionMapper2.applyMetadata(ActionSourceRule.java:104)
at com.sun.faces.facelets.tag.MetadataImpl.applyMetadata(MetadataImpl.java:81)
at javax.faces.view.facelets.MetaTagHandler.setAttributes(MetaTagHandler.java:129)
at javax.faces.view.facelets.DelegatingMetaTagHandler.setAttributes(DelegatingMetaTagHandler.java:102)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.doNewComponentActions(ComponentTagHandlerDelegateImpl.java:402)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:159)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:103)
at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:178)
at com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(DefaultFaceletContext.java:395)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:366)
at com.sun.faces.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:108)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:308)
at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:367)
at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:346)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:155)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:769)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.el.ELException: Failed to parse the expression [#{hibernateUserHandling.deleteUser(list.userId)}]
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:146)
at org.apache.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:172)
at org.apache.el.lang.ExpressionBuilder.createMethodExpression(ExpressionBuilder.java:223)
at org.apache.el.ExpressionFactoryImpl.createMethodExpression(ExpressionFactoryImpl.java:57)
at com.sun.faces.facelets.tag.TagAttributeImpl.getMethodExpression(TagAttributeImpl.java:222)
... 61 more
Caused by: org.apache.el.parser.ParseException: Encountered " "(" "( "" at line 1, column 35.
Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
"<=" ...
"le" ...
"==" ...
"eq" ...
"!=" ...
"ne" ...
"&&" ...
"and" ...
"||" ...
"or" ...
"*" ...
"+" ...
"-" ...
"/" ...
"div" ...
"%" ...
"mod" ...
at org.apache.el.parser.ELParser.generateParseException(ELParser.java:2142)
at org.apache.el.parser.ELParser.jj_consume_token(ELParser.java:2024)
at org.apache.el.parser.ELParser.DeferredExpression(ELParser.java:113)
at org.apache.el.parser.ELParser.CompositeExpression(ELParser.java:40)
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:114)
... 65 more
答案 0 :(得分:2)
您的Facelet构图错误。在最终视图中忽略<ui:composition>
之外的任何内容,模板客户端内容需要进入<ui:define>
。您在<h:form>
外面模板定义<ui:define>
,因此它不包含在最终视图中,因此按钮没有任何表单可供提交,他们只是赢了似乎根本不起作用。
按如下方式重新排列Facelet模板客户端:
<ui:composition template="./generalTemplate.xhtml"
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">
<ui:define name="content">
<h:outputStylesheet library="css" name="cssHibernate.css" />
<h:form id="main">
<h:dataTable value="#{hibernateDataOutput.persons}" var="list"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header">Delete</f:facet>
<h:commandButton id="userId" action="#{hibernateUserHandling.deleteUser(list.userId)}" image="delete.jpg" />
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
无关,Hibernate代码存在缺陷。但这是一个不同的问题。
更新:根据您之后在问题中编辑的堆栈跟踪,
Caused by: javax.el.ELException: Failed to parse the expression [#{hibernateUserHandling.deleteUser(list.userId)}]
显然您的环境根本不支持EL 2.2。您需要确保web.xml
声明符合Servlet 3.0 API并且您要部署到Servlet 3.0兼容的servlet容器(例如Tomcat 7,Glassfish 3,JBoss AS 6等)。
答案 1 :(得分:0)
hibernate代码不正确。 将您的方法修改为
public void deleteUser(String userIdViaXhtml)
{
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("DELETE FROM AddressDetails WHERE USERID = :userIdViaXhtml");
query.setString("userIdViaXhtml",userIdViaXhtml);
query.executeUpdate();
session.createQuery("DELETE FROM UserDetails WHERE USERID = :userIdViaXhtml");
query.setString("userIdViaXhtml",userIdViaXhtml);
query.executeUpdate();
session.getTransaction().commit();
session.close();
}
提示:处理正常回滚的异常,不要直接在JSF托管bean中编写此代码,使用DAO模式。