【问题标题】:Richfaces 4.5.17 Menuitem action method not workingRichfaces 4.5.17 Menuitem 操作方法不起作用
【发布时间】:2017-05-31 21:42:21
【问题描述】:

开发者!!

我正在将一个 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>

faces-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>

请,任何至少指出我这个问题的原因的帮助将不胜感激。

提前谢谢你!

威廉

【问题讨论】:

  • 如果您对@action 有疑问,您应该展示它(并说明它应该做什么),HTML 部分在很大程度上是无关紧要的。
  • 你好,马基尔!谢谢回复 !动作做什么并不重要,因为它没有被触发。 xhtml 可能是相关的,因为 RF4 存在一些问题,例如,未放置标签 (这导致 RF4 不加载 javascript)。这就是我发布 xhtml 文件的原因。另外,我刚刚发布了 web.xml 和 faces-config。谢谢!
  • 啊,看起来你有嵌套的表单,去掉它们。如果这没有帮助,请尝试手动创建菜单,看看是否可行。

标签: jsf-2 richfaces


【解决方案1】:

解决了!

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>

就是这样!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多