如何用网页更新 portlet 视图?

How to update the portlet view with a web page?

在我的应用程序中,我需要在我的 portlet 中动态访问远程应用程序(在其他 tomcat 实例上 运行 的应用程序)。所以我在查看和编辑模式下创建了 portlet,这样我就可以在 portlet 设置选项中看到 "preferences" 选项。我的首选项页面有三个带有提交按钮的输入字段。现在基于我需要呈现我的 portlet 内容的输入字段。单击提交按钮后,我可以在 configuraionActionImpl class 中获得 URL。现在我可以将 portlet 视图设置为 URL 网页内容吗?假设在首选项中,如果用户输入“http://localhost:8080/Myapp/events" and on click of submit button in preference configation page then the portlet view should be changed to events page of Myapp application, If user wants to modify the URL from "http://localhost:8080/Myapp/events" to "http://localhost:8080/Myapp/tasks”并单击配置页面首选项页面中的提交按钮,则 portlet 视图应呈现任务页面。

portlet.xml

 <portlet-name>FRunner</portlet-name>
            <display-name>FRunner</display-name>
            <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>
            <init-param>
                    <name>view-template</name>
                    <value>/view.jsp</value>
            </init-param>
            <init-param>
                    <name>config-template</name>
                    <value>/preferences.jsp</value>
            </init-param>
            <expiration-cache>0</expiration-cache>
            <supports>
                    <mime-type>text/html</mime-type>
                    <portlet-mode>view</portlet-mode>
                <portlet-mode>edit</portlet-mode>
            </supports>

liferay-portlet.xml

<liferay-portlet-app>
        <portlet>
                <portlet-name>FRunner</portlet-name>
                <icon>/icon.png</icon>
                <configuration-action-class>com.demo.formrunner.ConfigurationActionImpl</configuration-action-class>
                <header-portlet-css>/css/main.css</header-portlet-css>
                <footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
                <css-class-wrapper>FRunner-portlet</css-class-wrapper>
        </portlet>
</liferay-portlet-app>

preferences.jsp

<portletefineObjects />
<liferay-portlet:actionURL portletConfiguration="true" var="configurationURL" />
<aui:form action="<%= configurationURL %>" method="post">
  <aui:fieldset label="Form Runner Portlet Settings">
    <aui:layout>
       <aui:column>
          <aui:input type="text" name="url" label="URL:" inlineLabel="true"/>
       </aui:column>
       <aui:button-row>
          <aui:button type="submit" value="Submit"/>
         <aui:button type="button" value="Cancel" last="true"/>
      </aui:button-row>
   </aui:layout>
 </aui:fieldset>
</aui:form>

ConfigurationActionImpl.java

package com.demo.formrunner;
public class ConfigurationActionImpl extends DefaultConfigurationAction {

    @Override
    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        super.processAction(portletConfig, actionRequest, actionResponse);
        PortletPreferences prefs = actionRequest.getPreferences();
        String urlVal= prefs.getValue("url", "");
        System.out.println("URL Value is" + urlVal); // able to get the url value here. Now How to update the portlet view with this URL
    }
} 

1) 我需要如何根据输入 URL 呈现我的 portlet 视图? URL 应该类似于 http 请求 (http://localhost:8080/Demo/myPage)

请给我建议存档指南。

我认为问题出在按钮项的使用上。有几次我使用按钮项目让我有些头疼,并且需要点击它才能跟随它属性。

尝试使用 aui:input type="submit" 项目代替 aui:button.

在您的操作中:

package com.demo.formrunner;
public class ConfigurationActionImpl extends DefaultConfigurationAction {

@Override
public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    super.processAction(portletConfig, actionRequest, actionResponse);
    PortletPreferences prefs = actionRequest.getPreferences();
    String urlVal= prefs.getValue("url", "");
    System.out.println("URL Value is" + urlVal); // able to get the url value here. Now How to update the portlet view with this URL

    actionResponse.setRenderParameter("url", urlVal);
    actionResponse.setRenderParameter("jspPage","/html/yourNewJsp.jsp"); 
}

}

yourNewJsp.jsp

...
<iframe src="<%=request.getParameter("url") %>"></iframe>
...

但是,如果您使用首选项,则可以直接从 yourNewJsp.jsp 中的首选项中检索 url,而无需从操作中传递该参数 actionResponse.setRenderParameter("url", urlVal);。您也可以仅使用一个 jsp,其中如果首选项中的 "urlVal" 不为空或为空,则显示 <iframe>.

在您的 ConfigurationAction 中:

package com.demo.formrunner;
public class ConfigurationActionImpl extends DefaultConfigurationAction {

@Override
public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {


    PortletPreferences prefs = actionRequest.getPreferences();
    String urlVal= prefs.getValue("url", "");
    System.out.println("URL Value is" + urlVal); // able to get the url value here. Now How to update the portlet view with this URL


        try {
            prefs.store();
        }
        catch (ValidatorException ve) {
            SessionErrors.add(
                    actionRequest, ValidatorException.class.getName(), ve);
            return;
        }

        SessionMessages.add(
                actionRequest,
                PortalUtil.getPortletId(actionRequest) +
                SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
                portletResource);

        SessionMessages.add(
                actionRequest,
                PortalUtil.getPortletId(actionRequest) +
                SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);

}

在你的view.jsp

<%
    PortletPreferences prefs = liferayPortletRequest.getPreferences();
    String url = prefs.getValue("url", "");
%>

...
<iframe src="<%=url %>"></iframe>
...

您可以使用以下代码修改您的代码,

在你的configuration.jsp

<aui:form action="<%= configurationURL %>" method="post" name="fm">
  <aui:fieldset label="Form Runner Portlet Settings">
    <aui:layout>
       <aui:column>
          <aui:input autoFocus="<%= (windowState.equals(WindowState.MAXIMIZED) || windowState.equals(LiferayWindowState.POP_UP)) %>" cssClass="lfr-input-text-container" label="source-url" name="preferences--src--" prefix="<%= relative ? StringPool.TRIPLE_PERIOD : StringPool.BLANK %>" type="text" value="<%= src %>" />

       </aui:column>
       <aui:button-row>
         <aui:button type="submit" />
      </aui:button-row>
   </aui:layout>
 </aui:fieldset>
</aui:form> 

在view.jsp

String src = portletPreferences.getValue("src", StringPool.BLANK);

<%
//String iframeSrc = StringPool.BLANK;
String iframeSrc = src;
if (relative) {
        iframeSrc = themeDisplay.getPathContext();
}

//iframeSrc += (String)request.getAttribute(WebKeys.IFRAME_SRC);

if (Validator.isNotNull(iframeVariables)) {
        if (iframeSrc.contains(StringPool.QUESTION)) {
                iframeSrc = iframeSrc.concat(StringPool.AMPERSAND).concat(StringUtil.merge(iframeVariables, StringPool.AMPERSAND));
        }
        else {
                iframeSrc = iframeSrc.concat(StringPool.QUESTION).concat(StringUtil.merge(iframeVariables, StringPool.AMPERSAND));
        }
}

String baseSrc = iframeSrc;

int lastSlashPos = iframeSrc.substring(7).lastIndexOf(StringPool.SLASH);

if (lastSlashPos != -1) {
        baseSrc = iframeSrc.substring(0, lastSlashPos + 8);
}

String iframeHeight = heightNormal;

if (windowState.equals(WindowState.MAXIMIZED)) {
        iframeHeight = heightMaximized;
}
%>

<c:choose>
        <c:when test="<%= auth && Validator.isNull(userName) && !themeDisplay.isSignedIn() %>">
                <%-- <div class="alert alert-info">
                        <a href="<%= themeDisplay.getURLSignIn() %>" target="_top"><liferay-ui:message key="please-sign-in-to-access-this-application" /></a>
                </div> --%>
        </c:when>
        <c:otherwise>
                <div>
                        <iframe alt="<%= HtmlUtil.escapeAttribute(alt) %>" border="<%= HtmlUtil.escapeAttribute(border) %>" bordercolor="<%= HtmlUtil.escapeAttribute(bordercolor) %>" frameborder="<%= HtmlUtil.escapeAttribute(frameborder) %>" height="<%= HtmlUtil.escapeAttribute(iframeHeight) %>" hspace="<%= HtmlUtil.escapeAttribute(hspace) %>" id="<portlet:namespace />iframe" longdesc="<%= HtmlUtil.escapeAttribute(longdesc) %>" name="<portlet:namespace />iframe" onload="<portlet:namespace />monitorIframe();" scrolling="<%= HtmlUtil.escapeAttribute(scrolling) %>" src="<%= HtmlUtil.escapeHREF(iframeSrc) %>" title="<%= HtmlUtil.escapeAttribute(title) %>" vspace="<%= HtmlUtil.escapeAttribute(vspace) %>" width="<%= HtmlUtil.escapeAttribute(width) %>">
                                <%= LanguageUtil.format(pageContext, "your-browser-does-not-support-inline-frames-or-is-currently-configured-not-to-display-inline-frames.-content-can-be-viewed-at-actual-source-page-x", HtmlUtil.escape(iframeSrc)) %>
                        </iframe>
                </div>
        </c:otherwise>
</c:choose>

<aui:script>
        function <portlet:namespace />monitorIframe() {
                var url = null;

                try {
                        var iframe = document.getElementById('<portlet:namespace />iframe');

                        url = iframe.contentWindow.document.location.href;
                }
                catch (e) {
                        return true;
                }

                var baseSrc = '<%= HtmlUtil.escapeJS(baseSrc) %>';
                var iframeSrc = '<%= HtmlUtil.escapeJS(iframeSrc) %>';        

                if ((url == iframeSrc) || (url == (iframeSrc + '/'))) {
                }
                else if (Liferay.Util.startsWith(url, baseSrc)) {
                        url = url.substring(baseSrc.length);

                        <portlet:namespace />updateHash(url);
                }
                else {
                        <portlet:namespace />updateHash(url);
                }

                return true;
        }

        Liferay.provide(
                        window,
                        '<portlet:namespace />init',
                        function() {
                                var A = AUI();

                                var hash = document.location.hash.replace('#', '');

                                // LPS-33951

                                if (!A.UA.gecko) {
                                        hash = A.QueryString.unescape(hash);
                                }

                                var hashObj = A.QueryString.parse(hash);

                                hash = hashObj['<portlet:namespace />'];

                                if (hash) {
                                        var src = '';

                                        if (!(/^https?\:\/\//.test(hash))) {
                                                src = '<%= HtmlUtil.escapeJS(baseSrc) %>';
                                        }

                                        src += hash;

                                        var iframe = A.one('#<portlet:namespace />iframe');

                                        if (iframe) {
                                                iframe.attr('src', src);
                                        }
                                }
                        },
                        ['aui-base', 'querystring']
                );

        Liferay.provide(
                        window,
                        '<portlet:namespace />updateHash',
                        function(url) {
                                var A = AUI();

                                var hash = document.location.hash.replace('#', '');

                                var hashObj = A.QueryString.parse(hash);

                                hashObj['<portlet:namespace />'] = url;

                                var maximize = A.one('#p_p_id<portlet:namespace /> .portlet-maximize-icon a');

                                hash = A.QueryString.stringify(hashObj);

                                if (maximize) {
                                        var href = maximize.attr('href');

                                        href = href.split('#')[0];

                                        maximize.attr('href', href + '#' + hash);
                                }

                                var restore = A.one('#p_p_id<portlet:namespace /> a.portlet-icon-back');

                                if (restore) {
                                        var href = restore.attr('href');

                                        href = href.split('#')[0];

                                        restore.attr('href', href + '#' + hash);
                                }

                                // LPS-33951

                                location.hash = A.QueryString.escape(hash);
                        },
                        ['aui-base', 'querystring']
                );

        <portlet:namespace />init();

</aui:script>

<aui:script use="aui-autosize-iframe">
var iframe = A.one('#<portlet:namespace />iframe');

if (iframe) {
        iframe.plug(
                A.Plugin.AutosizeIframe,
                {
                        monitorHeight: <%= resizeAutomatically %>
                }
        );

        iframe.on(
                'load',
                function() {
                        var height = A.Plugin.AutosizeIframe.getContentHeight(iframe);

                        if (height == null) {
                                height = '<%= HtmlUtil.escapeJS(heightNormal) %>';

                                if (themeDisplay.isStateMaximized()) {
                                        height = '<%= HtmlUtil.escapeJS(heightMaximized) %>';
                                }

                                iframe.setStyle('height', height);

                                iframe.autosizeiframe.set('monitorHeight', false);
                        }
                }
        );
}
</aui:script>

它会将值保存在 portlet 首选项中,您可以根据需要呈现动态页面。

注意:不要忘记导入必需的 类。