页面中的转发参数

时间:2013-06-07 15:04:47

标签: java jsf param

我有一个panelAdmin页面,当我点击adminList时,我转发参数

            <h:commandLink value="Admini" action="#{userMB.adminList()}">
                <f:param name="userId" value="#{loginMB.user.id}" />
            </h:commandLink>

UserMB方法

public String adminList() {
    return "adminList";
}

和faces-config:

<navigation-rule>
    <from-view-id>/adminPanel.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>adminList</from-outcome>
        <to-view-id>/adminList.xhtml</to-view-id>
    </navigation-case>

我的adminList页面:

<?xml version='1.0' encoding='UTF-8' ?> 
<!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:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <h:head>
        <title>Lista adminow</title>
    </h:head>

    <h:body>
        <h3>Zalogwano jako: #{loginMB.user.firstName} #{loginMB.user.firstName}</h3>
        <h3>Lista administratorów</h3>
        <f:view>
            <h:form>
                <p:dataTable value="#{userMB.allAdmins}" var="admin" widgetVar="customerTable">
                    <p:column headerText="Imie">
                        #{admin.firstName}
                    </p:column>
                    <p:column headerText="Nazwisko">
                        #{admin.lastName}
                    </p:column>
                    <p:column headerText="Pesel">
                        #{admin.personalId}
                    </p:column>
                    <p:column headerText="Edytuj">  
                        <h:commandLink value="Edytuj" action="#{userMB.editStart()}">
                            <f:setPropertyActionListener target="#{userMB.user}" value="#{admin}" />
                        </h:commandLink>
                    </p:column>
                    <p:column headerText="Usun">
                        <h:commandLink value="Usun" action="#{userMB.deleteUser()}">
                            <f:setPropertyActionListener target="#{userMB.user}" value="#{admin}" />
                        </h:commandLink>
                    </p:column>
                </p:dataTable>
                <h:commandButton value="add" action="#{userMB.createStart()}"/>
                <h:commandButton value="back" action="/adminPanel.xhtml"/>
            </h:form>
        </f:view>
    </h:body>

</html>

当我点击添加按钮或后退按钮时出现错误:HTTP状态500

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException
root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.
在glassfish服务器日志中

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at pl.ePrzychodnia.mb.UserMB.getAllAdmins(UserMB.java:51)
    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.getValue(BeanELResolver.java:363)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:138)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:183)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:731)
    at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:729)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:575)
    at org.primefaces.component.api.UIData.setRowModel(UIData.java:405)
    at org.primefaces.component.api.UIData.setRowIndex(UIData.java:397)
    at org.primefaces.component.api.UIData.processPhase(UIData.java:254)
    at org.primefaces.component.api.UIData.processDecodes(UIData.java:223)
    at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176)
    at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:933)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    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.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)

UserMB

@ManagedBean
@RequestScoped
public class UserMB {

    @EJB
    private UserFacadeLocal userFacade;
    private User user;

    public List<User> getAllAdmins() {
        return userFacade.findAllAdmins();
    }

UserFacade

@Stateless
public class UserFacade implements UserFacadeLocal {

    @PersistenceContext
    private EntityManager em;
    @EJB
    private UserDao userDao;

    @Override
    public List<User> findAllAdmins() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        String paramId = requestParameterMap.get("userId").toString();
        Integer userId = Integer.parseInt(paramId);
        Query q = em.createQuery("SELECT u FROM User u where u.role = 'admin' and u.id <> :id");
        q.setParameter("id", userId);
        List<User> users = q.getResultList();
        return users;
    }

1 个答案:

答案 0 :(得分:0)

从堆栈跟踪看起来UserFacadeLocal没有注入UserMB。在getAllAdmins方法中放置一个断点并验证是否是这种情况。