Liferay:jsf portlet 和从带有视图参数的 commandButton 导航

Posted

技术标签:

【中文标题】Liferay:jsf portlet 和从带有视图参数的 commandButton 导航【英文标题】:Liferay: jsf portlet and navigation from commandButton with view params 【发布时间】:2017-03-09 00:06:08 【问题描述】:

我正在使用带有 primefaces 6.0 的 Lifreay 6.2。

我有这两个简单的观点:

view.xhtml

<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<f:metadata>
    <f:viewParam name="fromPlace"
        value="#modelBean.fromPlace" />
</f:metadata>

<h:body>
    <h:form>
        <p:inputText id="foo"
            value="#modelBean.fromPlace" />
        <p:commandButton value="submit"
            action="search?faces-redirect=true&amp;includeViewParams=true" />
    </h:form>
</h:body>
</f:view>

搜索.xml

<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<f:metadata>
    <f:viewParam name="fromPlace"
        value="#modelBean.fromPlace" />
</f:metadata>

<h:body>
    #param.fromPlace

    #modelBean.fromPlace
</h:body>
</f:view>

这样做的目标很简单。在 inputText 元素上键入一个值,单击提交按钮,然后重定向到第二页并显示结果。我有一个命令按钮的原因是我希望将来执行一些支持 bean 操作并动态返回重定向 viewId,但现在无关紧要。

我的问题是这个.. 如果我将 ajax=false 放在命令按钮组件中,一切都会按预期工作。如果我将 ajax=true 放在命令按钮组件中,当我的输入包含空格时,我会收到一个奇怪的错误 ONLY。如果它不包含空格,它将按预期再次工作。手动将参数放在 URL 字符串中也不起作用,不管各种编码尝试等。此外,使用常规 jsf 组件(如here)也会发生同样的事情。在 Eclipse 控制台中会出现以下几行:

14:42:20,856 ERROR [BridgeContextImpl:234] Illegal character in query at index 79: /test-portlet/WEB-INF/views/search.xhtml?fromPlace=123 321&_bridgeAjaxRedirect=true
java.net.URISyntaxException: Illegal character in query at index 79: /test-portlet/WEB-INF/views/search.xhtml?fromPlace=123 321&_bridgeAjaxRedirect=true

这种行为正常吗?

编辑:我使用liferayfaces.org/ 提供的原型创建了一个新的 jsf portlet 项目,然后将其作为现有的 maven 项目导入,但出现了类似的错误。

错误

15:54:11,198 ERROR [ExceptionHandlerAjaxImpl:75] javax.portlet.faces.BridgeException: java.net.URISyntaxException: Illegal character in query at index 110: /com.mycompany.my.primefaces.portlet-portlet/WEB-INF/views/search.xhtml?_jsfBridgeRedirect=true&fromPlace=1234 1234
javax.faces.FacesException: javax.portlet.faces.BridgeException: java.net.URISyntaxException: Illegal character in query at index 110: /com.mycompany.my.primefaces.portlet-portlet/WEB-INF/views/search.xhtml?_jsfBridgeRedirect=true&fromPlace=1234 1234

pom.xml

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>com.mycompany.my.primefaces.portlet</artifactId>
    <packaging>war</packaging>
    <name>com.mycompany.my.primefaces.portlet</name>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <faces.api.version>2.2</faces.api.version>
        <liferay.faces.bridge.ext.version>3.0.0</liferay.faces.bridge.ext.version>
        <liferay.faces.bridge.version>4.0.0</liferay.faces.bridge.version>
        <mojarra.version>2.2.13</mojarra.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>$faces.api.version</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>$mojarra.version</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.faces</groupId>
            <artifactId>com.liferay.faces.bridge.ext</artifactId>
            <version>$liferay.faces.bridge.ext.version</version>
        </dependency>
        <dependency>
            <groupId>com.liferay.faces</groupId>
            <artifactId>com.liferay.faces.bridge.impl</artifactId>
            <version>$liferay.faces.bridge.version</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.0</version>
        </dependency>
    </dependencies>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <!-- Seting the JSF 2 PROJECT_STAGE to Development will cause the JSF implementation will do the following at runtime: -->
    <!-- 1. Log more verbose messages. -->
    <!-- 2. Render tips and/or warnings in the view markup. -->
    <!-- 3. Cause the default ExceptionHandler to display a developer-friendly error page. -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <!-- JSF 2.2 now allows for composite components and resources to be hidden under WEB-INF -->
    <context-param>
        <param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
        <param-value>/WEB-INF/resources</param-value>
    </context-param>
    <!-- Although the FacesServlet will not be invoked by any portlet requests, it is required to initialize JSF. -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <security-constraint>
        <display-name>Prevent direct access to Facelet XHTML</display-name>
        <web-resource-collection>
            <web-resource-name>Facelet XHTML</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <auth-constraint/>
    </security-constraint>
</web-app>

liferay-portlet.xml

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">

<liferay-portlet-app>
    <portlet>
        <portlet-name>com.mycompany.my.primefaces.portlet</portlet-name>
        <icon>/resources/images/icon.png</icon>
        <requires-namespaced-parameters>false</requires-namespaced-parameters>
        <ajaxable>false</ajaxable>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>guest</role-name>
        <role-link>Guest</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>power-user</role-name>
        <role-link>Power User</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>user</role-name>
        <role-link>User</role-link>
    </role-mapper>
</liferay-portlet-app>

【问题讨论】:

我对@9​​87654323@ 感到非常尴尬。事实证明我没有仔细阅读你的问题。对于那个很抱歉!明天我会尝试发布一个经过更彻底研究的答案,但我会评论我的最佳猜测它会说什么。 【参考方案1】:

编辑:FACES-2958 已修复为 Bridge Impl 4.1.1 和 Bridge Ext 5.0.2

您遇到了一个错误:FACES-2958。

如您所见,您可以使用不带 ajax 的 commandButton 来解决此问题。

p:commandButton:

<p:commandButton value="submit"
    action="search?faces-redirect=true&amp;includeViewParams=true"
    ajax="false" />

h:commandButton:

<h:commandButton value="submit"
    action="search?faces-redirect=true&amp;includeViewParams=true" />

【讨论】:

感谢您花时间调查这个小错误,我会接受这个作为答案。如果这最终得到解决,请发表评论或更新您的答案。

以上是关于Liferay:jsf portlet 和从带有视图参数的 commandButton 导航的主要内容,如果未能解决你的问题,请参考以下文章

带有 freemarker 和 spring 的 Liferay portlet

带有可选参数的 Liferay 友好 URL

Liferay 7 portlet中所有能在@Component中修改的属性

liferay 7 中 liferay-portlet.xml 的等效文件是啥?

如何从 Liferay 7 取消部署 portlet?

使用 liferay 和 icefaces 共享 Portlet 数据