Richfaces 4.5.17 Menuitem操作方法不起作用

时间:2017-05-31 21:42:21

标签: jsf-2 richfaces

开发者!!

我正在将Web应用程序从JEE5迁移到JEE7。我的问题涉及表示层。

所有系统页面都从RichFaces 3.3.3迁移到4.5.17。我一直关注官方迁移指南([https://developer.jboss.org/wiki/RichFacesMigrationGuide33x-4xMigration][1]),Richfaces论坛(https://developer.jboss.org/en/richfaces),当然还有StackOverflow。但没有人能为我解决我的问题:

应用程序菜单是在managedbean上构建的(不是xhtml上的标签)。它在RF 3.3.3上运行良好,使用类HtmlToolBar,HtmlDropDownMenu,HtmlMenuGroup和HtmlMenuItem。但是,在迁移到RF4对应的类之后,不再触发MenuItem操作方法用户点击MenuItem后,页面会刷新并请求当前页面本身,而不是所需的页面。

迁移的代码如下:

ManagedBean

package br.gov.serpro.sfitweb.ui.managedbean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.richfaces.component.Mode;
import org.richfaces.component.UIDropDownMenu;
import org.richfaces.component.UIMenuGroup;
import org.richfaces.component.UIMenuItem;
import org.richfaces.component.UIToolbar;

import br.gov.serpro.sfitweb.bean.entity.Usuario;
import br.gov.serpro.sfitweb.bean.enums.Permissao;
import br.gov.serpro.sfitweb.context.ContextFinder;
import br.gov.serpro.sfitweb.context.SFITSecurityContext;

@Named
@SessionScoped
public class MenuMBTrecho implements Serializable {
    private static final long serialVersionUID = 1L;

    private UIToolbar toolbar;

    public MenuMBTrecho() {
    }

    @PostConstruct
    public void init() {
        montarToolbar();
    }

    public void montarToolbar() {
        FacesContext ctx = FacesContext.getCurrentInstance();

        this.toolbar = (UIToolbar) ctx.getApplication().createComponent(ctx, UIToolbar.COMPONENT_TYPE,
                "org.richfaces.ToolbarGroupRenderer");

        // log.debug("User" + secCtx.getUserPrincipal().getCpf());

        UIDropDownMenu menuPlanejamento = new UIDropDownMenu();
        menuPlanejamento.setLabel("Planejamento");
        boolean mostraMenu = false;

        ///////// Submenu PPA /////////
        boolean mostraSubMenuPPA = false;
        UIMenuGroup subMenuPPA = new UIMenuGroup();
        subMenuPPA.setLabel("PPA");

        UIMenuItem menuItem = new UIMenuItem();
        menuItem.setMode(Mode.ajax);
        menuItem.setLabel("Cadastrar período de PPA");
        ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
        ELContext ELCtx = FacesContext.getCurrentInstance().getELContext();
        menuItem.setActionExpression(
                factory.createMethodExpression(ELCtx, "#{ppaMB.listarPpas}", String.class, new Class[] {}));

        subMenuPPA.getChildren().add(menuItem);
    }

    public UIToolbar getToolbar() {
        return toolbar;
    }

    public void setToolbar(UIToolbar toolbar) {
        this.toolbar = toolbar;
    }

}

名为menu.xhtml

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:richext="http://java.sun.com/jsf/composite/richext"
        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:rich="http://richfaces.org/rich"
        xmlns:p="http://primefaces.org/ui">

        <div id="menu">
            <h:form id="menuForm">          
                <rich:toolbar binding="#{menuMB.toolbar}"/>
            </h:form>
        </div>

    </ui:composition>

common.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:rich="http://richfaces.org/rich"      
    xmlns:a4j="http://richfaces.org/a4j">

<f:view contentType="text/html">

    <h:head>
        <ui:include src="/public/pages/scriptTags.xhtml" />
    </h:head>

    <link rel="stylesheet"
        href="#{configMB.context}/public/resources/styles/richfaces.css" />

    <h:body onload="ajustaAltura();"
        style="width:100%; padding:0px; margin: 0px; border-spacing: 0px;">

        <h:form id="principalMenu">

        <rich:messages globalOnly="false" errorClass="errorFatalMessage"
            fatalClass="errorFatalMessage" infoClass="infoMessage"
            warnClass="warnMessage" showSummary="true" showDetail="true">
        </rich:messages>

        <ui:insert name="top">
            <ui:include src="/public/pages/top.xhtml" />
        </ui:insert>
        <ui:insert name="header">
            <ui:include src="/public/pages/header.xhtml" />
        </ui:insert>
        <ui:insert name="menu">
            <ui:include src="/private/pages/menu.xhtml" />
        </ui:insert>

        <div id="corpo" align="center">
            <rich:panel style="width: 97%;border:0">
                <ui:insert name="body" />
            </rich:panel>
        </div>
        <ui:insert name="footer">
            <ui:include src="/public/pages/footer.xhtml" />
        </ui:insert>
        </h:form>
    </h:body>

</f:view>
</html>

面-config.xml中

    <?xml version="1.0"?>
    <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

        <application>
            <resource-bundle>
                <base-name>br.gov.serpro.sfitweb.utils.MsgReaderUTF8</base-name>
                <var>msg</var>
            </resource-bundle>
        </application>

        <lifecycle>
            <phase-listener>br.gov.serpro.sfitweb.ui.MessagePhaseListener</phase-listener>
        </lifecycle>

        <application>
            <locale-config>
                <default-locale>en</default-locale>
            </locale-config>
            <resource-bundle>
                <base-name>com.example.faces.i18n.Text</base-name>
                <var>text</var>
            </resource-bundle>
        </application>

    </faces-config>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <display-name>sfitweb</display-name>
    <distributable />

    <context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>

    <!-- parametros necessario para processar os JS internos do Richfaces https://developer.jboss.org/thread/203506 -->
    <context-param>
        <param-name>org.richfaces.resourceOptimization.enabled</param-name>
        <param-value>true</param-value>
    </context-param>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.richfaces.webapp.ResourceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/org.richfaces.resources/*</url-pattern>
    </servlet-mapping>


    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/tomahawk.taglib.xml;
            /WEB-INF/taglib/sfit.taglib.xml;</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <!-- parametros do Richfaces 4 -->

    <context-param>
        <param-name>org.richfaces.enableControlSkinning</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.richfaces.skin</param-name>
        <param-value>wine</param-value>
    </context-param>
    <context-param>
        <param-name>weblets.CONFIG_FILES</param-name>
        <param-value>weblets-config.xml</param-value>
    </context-param>

    <!-- https://developer.jboss.org/wiki/RichFacesMigrationGuide33x-4xMigration-Upgrading 
        filtro do richfaces nao eh mais necessario no JSF 2 -->
    <filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
        <init-param>
            <param-name>uploadMaxFileSize</param-name>
            <param-value>20m</param-value>
        </init-param>
    </filter>


    <filter-mapping>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
<!--    <listener> -->
<!--        <listener-class>br.gov.serpro.sfitweb.context.SFITSecurityServletRequestListener</listener-class> -->
<!--    </listener> -->
    <listener>
        <listener-class>net.java.dev.weblets.WebletsContextListener</listener-class>
    </listener>
    <!-- WebService para consulta de permissoes -->
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Weblets Servlet</servlet-name>
        <servlet-class>net.java.dev.weblets.WebletsServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ImageUploadServlet</servlet-name>
        <servlet-class>br.gov.serpro.sfitweb.utils.ImageUploadServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ImageDownloadServlet</servlet-name>
        <servlet-class>br.gov.serpro.sfitweb.utils.ImageDownloadServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>PossuiPermissao</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Weblets Servlet</servlet-name>
        <url-pattern>/weblets/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ImageUploadServlet</servlet-name>
        <url-pattern>/imageUpload</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ImageDownloadServlet</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>PossuiPermissao</servlet-name>
        <url-pattern>/PossuiPermissao</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <!-- Pages Configuration -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/public/pages/error/HTTP404.jsf</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/public/pages/error/HTTP500.jsf</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/public/pages/login.jsf</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/public/pages/error/global.jsp</location>
    </error-page>
</web-app>

请不要错过任何帮助我至少指出这个问题的原因。

提前谢谢!

威廉

1 个答案:

答案 0 :(得分:0)

解决了!

1。)修改了ManagedBean上的init():

@PostConstruct
    public void init(){
        menuPlanejamento = (UIDropDownMenu)FacesContext.getCurrentInstance().getApplication().createComponent(UIDropDownMenu.COMPONENT_TYPE);
}

2.。)在montarToolbar()方法上添加了menuItem.setMode(Mode.ajax);

3。)修改了xhtml页面:

<div id="menu">
        <h:form id="menuForm">
            <rich:toolbar id="menuToolbar">
                <rich:dropDownMenu id="menuPlanejamento" binding="#{menuMB.menuPlanejamento}"/> 
            </rich:toolbar>
        </h:form>
    </div>

那就是它!!