JSF h:commandlink 更新版本后不工作

JSF h:commandlink not working after updating version

我正在努力将一个应用程序从 websphere 迁移到 tomcat,这在很大程度上是完全成功的。之前的应用程序是 Java EE 5 上的 运行,我将其更新为 Java 7。在此过程中,我将 JSTL 更新为 1.2,将 myfaces 更新为 2.0,以及一些其他内容。这是我当前的 jar 库:

Library of Jars

除了 h:commandlink 之外,关于应用程序的一切都正常。我相信这与更新 jars 和 java 版本有关。我可以访问所有其他页面,只是导航按钮不起作用。这是他们的jsp:

<%-- tpl:metadata --%>
<%-- jsf:pagecode language="java" location="/src/pagecode/menu/Main.java" --%><%-- /jsf:pagecode --%>
<%-- /tpl:metadata --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<link rel="stylesheet" type="text/css" media="screen"
href="../theme/menu.css">
<link rel="stylesheet" type="text/css" href="../theme/stylesheet.css"
title="Style">
<hx:viewFragment id="viewFragmentMain"><hx:scriptCollector id="scriptCollector1">

    <% String Page = (session.getAttribute("mainPage") == null) ? "1" : (String)session.getAttribute("mainPage"); %>

    <h:form id="formMenu" styleClass="form">
        <ul id="tabmenu">
            <li<%= (Page.compareTo("1") == 0) ? " class=active" : ""%>><h:commandLink id="link1" action="#{pc_Main.doLink1Action}"><h:outputText id="textMenu1" styleClass="outputText" value="Page 1"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("2") == 0) ? " class=active" : ""%>><h:commandLink id="link2" action="#{pc_Main.doLink2Action}"><h:outputText id="textMenu2" styleClass="outputText" value="Page 2"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("3") == 0) ? " class=active" : ""%>><h:commandLink id="link3" action="#{pc_Main.doLink3Action}"><h:outputText id="textMenu3" styleClass="outputText" value="Page 3"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("5") == 0) ? " class=active" : ""%>><h:commandLink id="link5" action="#{pc_Main.doLink5Action}"><h:outputText id="textMenu5" styleClass="outputText" value="Page 5"></h:outputText></h:commandLink></li>
            <% if (request.isUserInRole("giftAdmin") | request.isUserInRole("giftAdminBackup") | request.isUserInRole("merchAdmin") | request.isUserInRole("merchAdminBackup")){ %>
            <li <%= (Page.compareTo("4") == 0) ? " class=active" : ""%>><h:commandLink
                id="link4" action="#{pc_Main.doLink4Action}">
                <h:outputText id="textMenu4" styleClass="outputText"
                    value="Page 4"></h:outputText>
            </h:commandLink></li>
            <li><a href="<%= request.getContextPath() %>/forms/Help.pdf" target="_blank">Help</a></li>
            <% } %>
        </ul>
    </h:form>
</hx:scriptCollector>

单击 link 时,假设调用此 class 中找到的相应方法。然而,它从未真正进入这个 class。假设在索引中进行调用,然后根据导航规则它 returns 正确的视图,但它永远不会进入正确的方法。

import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlOutputText;
import javax.servlet.http.HttpSession;

import com.accounting.credit.ControlBean;
import com.accounting.credit.CreditAction;
import com.accounting.credit.CreditRequest;

import jxl.common.Logger;
import pagecode.PageCodeBase;

public class Main extends PageCodeBase {

    private final static Logger LOG = Logger.getLogger(Main.class);

    protected HtmlCommandLink link2;
    protected HtmlOutputText textMenu1;
    protected HtmlForm formMenu;
    protected HtmlCommandLink link3;
    protected HtmlCommandLink link4;
    protected HtmlCommandLink link1;
    protected HtmlOutputText textMenu2;
    protected HtmlOutputText textMenu3;
    protected HtmlOutputText textMenu4;
    protected CreditAction action;
    protected CreditRequest req;
    protected ControlBean control;
    protected HtmlOutputText textMenu5;
    protected HtmlCommandLink link5;

    protected HtmlCommandLink getLink2() {
        if (link2 == null) {
            link2 = (HtmlCommandLink) findComponentInRoot("link2");
        }
        return link2;
    }

    protected HtmlOutputText getTextMenu1() {
        if (textMenu1 == null) {
            textMenu1 = (HtmlOutputText) findComponentInRoot("textMenu1");
        }
        return textMenu1;
    }

    protected HtmlForm getFormMenu() {
        if (formMenu == null) {
            formMenu = (HtmlForm) findComponentInRoot("formMenu");
        }
        return formMenu;
    }

    protected HtmlCommandLink getLink3() {
        if (link3 == null) {
            link3 = (HtmlCommandLink) findComponentInRoot("link3");
        }
        return link3;
    }

    protected HtmlCommandLink getLink4() {
        if (link4 == null) {
            link4 = (HtmlCommandLink) findComponentInRoot("link4");
        }
        return link4;
    }

    protected HtmlCommandLink getLink1() {
        if (link1 == null) {
            link1 = (HtmlCommandLink) findComponentInRoot("link1");
        }
        return link1;
    }

    public String doLink1Action() {
        // Type Java code that runs when the component is clicked
        LOG.info("In doLink1Action"); 
        HttpSession session = (HttpSession)getFacesContext().getExternalContext().getSession(true);
        session.setAttribute("edit", "False");
        getReq().clear();
        getAction().clear();
        return "page1";
    }

    public String doLink2Action() {
        // Type Java code that runs when the component is clicked
        LOG.info("In doLink2Action");
        HttpSession session = (HttpSession)getFacesContext().getExternalContext().getSession(true);
        session.setAttribute("edit", "False");
        getReq().clear();
        getAction().clear();
        return "page2";
    }

    public String doLink3Action() {
        // Type Java code that runs when the component is clicked
        LOG.info("In doLink3Action");
        HttpSession session = (HttpSession)getFacesContext().getExternalContext().getSession(true);
        session.setAttribute("edit", "False");
        getReq().clear();
        getAction().clear();
        return "pag3";
    }

    public String doLink4Action() {
        // Type Java code that runs when the component is clicked
        LOG.info("In doLink4Action");
        HttpSession session = (HttpSession)getFacesContext().getExternalContext().getSession(true);
        session.setAttribute("edit", "False");
        getReq().clear();
        getAction().clear();
        return "page4";
    }

    protected HtmlOutputText getTextMenu2() {
        if (textMenu2 == null) {
            textMenu2 = (HtmlOutputText) findComponentInRoot("textMenu2");
        }
        return textMenu2;
    }

    protected HtmlOutputText getTextMenu3() {
        if (textMenu3 == null) {
            textMenu3 = (HtmlOutputText) findComponentInRoot("textMenu3");
        }
        return textMenu3;
    }

    protected HtmlOutputText getTextMenu4() {
        if (textMenu4 == null) {
            textMenu4 = (HtmlOutputText) findComponentInRoot("textMenu4");
        }
        return textMenu4;
    }

    /** 
     * @managed-bean true
     */
    protected CreditAction getAction() {
        if (action == null) {
            action = (CreditAction) getFacesContext().getApplication()
                .createValueBinding("#{action}")
                .getValue(getFacesContext());
        }
        return action;
    }

    /** 
     * @managed-bean true
     */
    protected void setAction(CreditAction action) {
        this.action = action;
    }

    /** 
     * @managed-bean true
     */
    protected CreditRequest getReq() {
        if (req == null) {
            req = (CreditRequest) getFacesContext().getApplication()
                .createValueBinding("#{req}").getValue(getFacesContext());
        }
        return req;
    }

    /** 
     * @managed-bean true
     */
    protected void setReq(CreditRequest req) {
        this.req = req;
    }

    /** 
     * @managed-bean true
     */
    protected ControlBean getControl() {
        if (control == null) {
            control = (ControlBean) getFacesContext().getApplication()
                .createValueBinding("#{control}").getValue(
                        getFacesContext());
        }
        return control;
    }

    /** 
     * @managed-bean true
     */
    protected void setControl(ControlBean control) {
        this.control = control;
    }

    protected HtmlOutputText getTextMenu5() {
        if (textMenu5 == null) {
            textMenu5 = (HtmlOutputText) findComponentInRoot("textMenu5");
        }
        return textMenu5;
    }

    protected HtmlCommandLink getLink5() {
        if (link5 == null) {
            link5 = (HtmlCommandLink) findComponentInRoot("link5");
        }
        return link5;
    }

    public String doLink5Action() {
        // Type Java code that runs when the component is clicked
        return "page5";
    }

}

onclick 与之前运行的不同,这让我相信它与版本有关,但我没有找到任何与 onclick 为何会发生变化相关的信息。

上一个 onclick 是:

<a id="viewFragmentMain:formMenu:link2" href="#" onclick="document.forms['viewFragmentMain:formMenu']['__LINK_TARGET__'].disabled=false;document.forms['viewFragmentMain:formMenu']['__LINK_TARGET__'].value='viewFragmentMain:formMenu:link2'; document.forms['viewFragmentMain:formMenu'].submit();document.forms['viewFragmentMain:formMenu']['__LINK_TARGET__'].disabled=true; return false;"><span id="viewFragmentMain:formMenu:textMenu2" class="outputText">My Requests</span></a>

新的 onclick 是:

<a href="#" onclick="return myfaces.oam.submitForm('viewFragmentMain:formMenu','viewFragmentMain:formMenu:link2');" id="viewFragmentMain:formMenu:link2"><span id="viewFragmentMain:formMenu:textMenu2" class="outputText">My Requests</span></a>

简单地说,我想我想问的是如何使新的 link 看起来像更新版本的旧版本?

什么版本的 WebSphere? 通常你不能在你的应用程序中捆绑 JSF 实现 jar,除非你明确地想要覆盖 WAS 中捆绑的那个。如果是这样,请查看文档 here

我发现问题与使用仍然 "in use" 的旧 Websphere jar 有关。我有上面的标签库,IBM 面孔使用前缀 hx。这仅适用于 Websphere 服务器。因此,在删除它们的过程中,我不得不重做已实现的标签,其中大多数都有简单的等价物。另一个出现的问题,在它的后端,我们为 IBM Faces 实施,所以我目前正在重写以合并实际使用的标签。我还必须更改仍设置为包含 IMB 上下文参数的 web.xml,因此我删除了它们。

正如您在这里看到的,我删除了允许链接工作的 hx 标签库,并且没有任何问题。我还必须从 web.xml 文件中删除所有提及的 IBM 实例和来源。

<%-- tpl:metadata --%>
<%-- jsf:pagecode language="java" location="/src/pagecode/menu/Main.java" --%><%-- /jsf:pagecode --%>
<%-- /tpl:metadata --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<link rel="stylesheet" type="text/css" media="screen" href="theme/menu.css">
<link rel="stylesheet" type="text/css" href="theme/stylesheet.css" title="Style">

    <% String Page = (session.getAttribute("mainPage") == null) ? "1" : (String)session.getAttribute("mainPage"); %>

    <h:form id="formMenu" styleClass="form">
        <ul id="tabmenu">
            <li<%= (Page.compareTo("1") == 0) ? " class=active" : ""%>><h:commandLink id="link1" action="#{pc_Main.doLink1Action}"><h:outputText id="textMenu1" styleClass="outputText" value="Credit/Gift Card Request"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("2") == 0) ? " class=active" : ""%>><h:commandLink id="link2" action="#{pc_Main.doLink2Action}"><h:outputText id="textMenu2" styleClass="outputText" value="My Requests"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("3") == 0) ? " class=active" : ""%>><h:commandLink id="link3" action="#{pc_Main.doLink3Action}"><h:outputText id="textMenu3" styleClass="outputText" value="My ToDo's"></h:outputText></h:commandLink></li>
            <li<%= (Page.compareTo("5") == 0) ? " class=active" : ""%>><h:commandLink id="link5" action="#{pc_Main.doLink5Action}"><h:outputText id="textMenu5" styleClass="outputText" value="Search"></h:outputText></h:commandLink></li>
            <% if (request.isUserInRole("giftAdmin") | request.isUserInRole("giftAdminBackup") | request.isUserInRole("merchAdmin") | request.isUserInRole("merchAdminBackup")){ %>
            <li <%= (Page.compareTo("4") == 0) ? " class=active" : ""%>><h:commandLink
                id="link4" action="#{pc_Main.doLink4Action}">
                <h:outputText id="textMenu4" styleClass="outputText"
                    value="Accounting"></h:outputText>
            </h:commandLink></li>
            <li><a href="<%= request.getContextPath() %>/forms/Help.pdf" target="_blank">Help</a></li>
            <% } %>
        </ul>
    </h:form>