状态更新无效

时间:2013-03-03 15:57:25

标签: java jsf java-ee jsf-2

当我点击Speak Out(p:commandButton)提交状态时,我收到以下异常。该异常是由行

的commentList()函数引起的
Query query = em.createQuery(
    "SELECT c FROM Comment c WHERE c.statusId=" + statusId,
    Comment.class);

1)Home.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
</h:head>

    <div style="width: 100%; background-color: #EEEEEE;">
        <h:panelGrid columns="1">
            <h:form id="speakout">
                <h:outputText value="Speak Out" />
                <br />
                <h:outputText value="Share whats in your mind.!"
                    style="color:#aaaaaa;font-size:x-small;" />
                <p:inputTextarea name="content" id="sharetext" cols="60" rows="2"
                    onclick="this.value='';" value="#{statusBean.status.statusmsg}"
                    style="text-size:small;" />
                <br />
                <p:commandButton type="submit" value="Speak Out"
                    action="#{statusBean.save}" ajax="false"
                    styleClass="buttonstyle" />
                <br />
            </h:form>
        </div>
        <!--  -->
        <div class="items">
            <h:form>
                <ui:repeat var="p" value="#{statusBean.statusList}">
                    <ui:fragment rendered="#{p.statusmsg!=null}">
                        <div class="status">
                            <!--  for text status msg check-->
                            <h:commandLink action="#{friendBean.gotoFriendProfile(p.email)}"
                                styleClass="link">
                                <img src="../images/profilePicture/thumb/#{p.picture}"
                                    style="height: 39px; width: 39px;" />&nbsp;
                                <h:outputText value="#{p.statusBy}:" />
                            </h:commandLink>
                            <h:outputText value="#{p.statusmsg}" styleClass="textstyle1" />
                            <h:outputText value="#{p.timeMillis}"
                                style="font-size:xx-small;float:right;color:#bbbbbb;font-style: italic;">
                                <f:converter converterId="timeConverter" />
                            </h:outputText>
                            <h:commandLink action="#{statusBean.deleteStatus(p.statusId)}"
                                value="Delete"></h:commandLink>
                            <p:growl />
                            <br />
                            <ui:repeat var="q" value="#{statusBean.commentList(p.statusId)}" >
                                <div class="barcomment">
                                    <br />
                                        <h:commandLink action="#{friendBean.gotoFriendProfile(q.email)}"
                                            styleClass="link">
                                            <img src="../images/profilePicture/thumb/#{q.picture}"
                                                style="height: 29px; width: 29px;" />&nbsp;
                                            <h:outputText value="#{q.commentBy}:" />
                                        </h:commandLink>
                                        <h:outputText value=" #{q.comment}" styleClass="textstyle1" />
                                        <h:outputText value="#{q.timeMillis}"
                                            style="font-size:xx-small;float:right;font-style: italic;">
                                            <f:converter converterId="timeConverter" />
                                        </h:outputText>
                                        <h:commandLink action="#{statusBean.deleteComment(q.commentId)}"
                                            value="Delete"></h:commandLink>
                                    </div>
                                </ui:repeat>
                                <br />
                                <div class="comment">
                                    <p:inputText value="#{statusBean.comment.comment}"
                                        styleClass="box" />
                                    <p:commandLink value="Views"
                                        action="#{statusBean.update(p.statusId)}"
                                        ajax="false" styleClass="link" />
                                </div>
                                <br />
                            </div>
                        </ui:fragment>
                    </ui:repeat>
                </h:form>
            </div>
        </h:body>
</html>

2)状态Bean

 package com.bean;



    public class StatusBean {
        Date d;
        Comment comment;
        Status status;
        private EntityManager em;
        private UploadedFile uploadedFile;

        public Comment getComment() {
            return comment;
        }

        public void setComment(Comment comment) {
            this.comment = comment;
        }

        public Status getStatus() {
            return status;
        }

        public void setStatus(Status status) {
            this.status = status;
        }

        public StatusBean() {
            d = new Date();
            comment = new Comment();
            status = new Status();
            EntityManagerFactory emf = Persistence
                    .createEntityManagerFactory("FreeBird");
            em = emf.createEntityManager();
        }



        public List<Status> getStatusList() {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext()
                    .getSession(true);
            User user = (User) session.getAttribute("userdet");
            Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
                    + user.getEmail() + "' ORDER BY s.timeMillis desc",
                    Status.class);
            List<Status> results = query.getResultList();
            Query query1 = em.createQuery("SELECT f FROM Friend f WHERE f.email='"
                    + user.getEmail() + "'", Friend.class);
            List<Friend> results1 = query1.getResultList();
            Iterator<Friend> it = results1.listIterator();
            while (it.hasNext()) {
                String email = it.next().getFriendEmail();
                Query query2 = em.createQuery(
                        "SELECT s FROM Status s WHERE s.email='" + email
                                + "' ORDER BY s.timeMillis desc", Status.class);
                List<Status> results2 = query2.getResultList();
                results.addAll(results2);

            }
            Collections.sort(results);
            int index = 0;
            int end = index+5 > results.size() ? results.size() : index+5;
            List<Status> subList = results.subList(0, end);

            session.setAttribute("statusindex", end);
            return subList;

        }
    public String save() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext()
            .getSession(false);
    User user = (User) session.getAttribute("userdet");
    status.setEmail(user.getEmail());
    status.setStatusBy(user.getFirstName());
    d = new Date();
    status.setTimeMillis(d.getTime());
    status.setPicture(user.getImage());
    System.out.println("status save called");
    em.getTransaction().begin();
    em.persist(status);
    em.getTransaction().commit();
    FacesContext context2 = FacesContext.getCurrentInstance();
    context2.getExternalContext().getSessionMap().remove("statusBean");
    return "success";
}


        public String update(String statusId) {

            System.out.println("Update Called...");
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext()
                    .getSession(true);
            User user = (User) session.getAttribute("userdet");
            comment.setStatusId(Integer.parseInt(statusId));
            comment.setCommentBy(user.getFirstName());
            comment.setPicture(user.getImage());
            comment.setEmail(user.getEmail());
            d = new Date();
            comment.setTimeMillis(d.getTime());
            em.getTransaction().begin();
            em.persist(comment);
            em.getTransaction().commit();
            getComment().setComment("");
            FacesContext context2 = FacesContext.getCurrentInstance();
            context2.getExternalContext().getSessionMap().remove("statusBean");
            return "success";

        }

        public List<Comment> commentList(String statusId) {
            FacesContext context = FacesContext.getCurrentInstance();
            Query query = em.createQuery(
                    "SELECT c FROM Comment c WHERE c.statusId=" + statusId,
                    Comment.class);
            List<Comment> results = query.getResultList();
            return results;
        }

        public String deleteStatus(String statusId) {

            Status status = em.find(Status.class, Integer.parseInt(statusId));
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext()
                    .getSession(true);
            User user = (User) session.getAttribute("userdet");
            if (user.getEmail().equals(status.getEmail())) {
                em.getTransaction().begin();
                em.remove(status);
                em.getTransaction().commit();

            } else {
                context.addMessage(null, new FacesMessage(
                        "You don't have permission to delete"));
            }

            return "success";

        }

        public String deleteComment(String commentId) {

            Comment comment = em.find(Comment.class, Integer.parseInt(commentId));
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext()
                    .getSession(true);
            User user = (User) session.getAttribute("userdet");
            Status status = em.find(Status.class, comment.getStatusId());
            if (user.getEmail().equals(status.getEmail())
                    || comment.getEmail().equals(user.getEmail()))

            {
                em.getTransaction().begin();
                em.remove(comment);
                em.getTransaction().commit();
            } else {
                context.addMessage(null, new FacesMessage(
                        "You don't have permission to delete"));
            }
            return "success";

        }

    }

3)异常

Mar 03, 2013 9:10:26 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/FreeBird_v.6] threw exception [An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [SELECT c FROM Comment c WHERE c.statusId=], line 0, column -1: unexpected end of query.
Internal Exception: NoViableAltException(-1@[792:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );])] with root cause
NoViableAltException(-1@[792:1: comparisonExpressionRightOperand returns [Object node] : (n= arithmeticExpression | n= nonArithmeticScalarExpression | n= anyOrAllExpression );])
    at org.eclipse.persistence.internal.libraries.antlr.runtime.DFA.noViableAlt(DFA.java:159)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.DFA.predict(DFA.java:144)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.comparisonExpressionRightOperand(JPQLParser.java:4326)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.comparisonExpression(JPQLParser.java:4215)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.simpleConditionalExpressionRemainder(JPQLParser.java:3389)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.simpleConditionalExpression(JPQLParser.java:3326)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalPrimary(JPQLParser.java:3275)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalFactor(JPQLParser.java:3194)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalTerm(JPQLParser.java:3103)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.conditionalExpression(JPQLParser.java:3029)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.whereClause(JPQLParser.java:2986)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.selectStatement(JPQLParser.java:380)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.document(JPQLParser.java:281)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.parse(JPQLParser.java:134)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.buildParseTree(JPQLParser.java:95)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:215)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:190)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:126)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1475)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1497)
    at com.bean.StatusBean.commentList(StatusBean.java:227)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:484)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at com.sun.faces.facelets.component.UIRepeat.getValue(UIRepeat.java:268)
    at com.sun.faces.facelets.component.UIRepeat.getDataModel(UIRepeat.java:244)
    at com.sun.faces.facelets.component.UIRepeat.setIndex(UIRepeat.java:434)
    at com.sun.faces.facelets.component.UIRepeat.doVisitChildren(UIRepeat.java:642)
    at com.sun.faces.facelets.component.UIRepeat.visitTree(UIRepeat.java:600)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
    at com.sun.faces.facelets.component.UIRepeat.visitTree(UIRepeat.java:606)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
    at javax.faces.component.UIForm.visitTree(UIForm.java:331)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
    at com.sun.faces.lifecycle.RestoreViewPhase.deliverPostRestoreStateEvent(RestoreViewPhase.java:258)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:245)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:107)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    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:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:2)

您传递给p.statusId的{​​{1}}似乎是空的。您可以尝试将commentList(String statusId)置于System.out.println("statusId: "+statusId);方法的开头。

您也可以在{I}之前放置commentList(String statusId)

<h:outputText value="p.statusId: #{p.statusId}"/>

我也不确定将<ui:repeat var="q" value="#{statusBean.commentList(p.statusId)}" > 置于statusBean.commentList(p.statusId) ui:repeat属性中是否合适。您最终可能会比您想要的更频繁地从DB中检索状态列表。最好将它预加载到某个属性,然后在需要的地方传递它。

<强>更新

同时替换:

value

Query query = em.createQuery(
    "SELECT c FROM Comment c WHERE c.statusId=" + statusId, Comment.class);

这样您的查询对SQL注入和语法错误更安全。

更新2

TypedQuery<Comment> query = em.createQuery(
    "SELECT c FROM Comment c WHERE c.statusId = :statusId", Comment.class);
query.setParameter("statusId", statusId);

在您的StatusBean中

private class StatusWrapper {

  private Status status;
  private List<Comment> comments;

}

在您的页面中使用statuses属性。希望它很清楚(可能有一些拼写错误。)