如何使用网页更新portlet视图?

时间:2015-03-11 13:01:45

标签: java liferay liferay-6 portlet

在我的应用程序中,我需要在我的portlet中动态访问远程应用程序(在其他tomcat实例上运行的应用程序)。所以我在视图和编辑模式下创建了portlet,这样我就可以看到"偏好" portlet设置选项中的选项。我的首选项页面有三个带提交按钮的输入字段。现在基于我需要呈现我的portlet内容的输入字段。点击提交按钮,我可以获得configuraionActionImpl课程中的网址。现在我可以将portlet视图设置为该URL网页内容吗?让我们在偏好中说出如果用户输入" http://localhost:8080/Myapp/events"然后单击首选项配置页面中的提交按钮,然后将portlet视图更改为Myapp应用程序的事件页面,如果用户想要修改来自" http://localhost:8080/Myapp/events"到" 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

请建议我存档相同的指南。

4 个答案:

答案 0 :(得分:2)

在你的行动中:

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

但是,如果您使用偏好设置,则可以直接从url中的偏好设置检索yourNewJsp.jsp,而不会从操作中传递该参数actionResponse.setRenderParameter("url", urlVal);。你也可以只使用一个jsp,如果&#34; urlVal&#34;如果首选项不为null或为空,则显示<iframe>

答案 1 :(得分:2)

您在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>
...

答案 2 :(得分:2)

您可以使用以下代码

修改代码

在你的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首选项中的值,您可以根据需要呈现动态页面。

注意:不要忘记导入所需的课程。

答案 3 :(得分:1)

我认为问题在于使用按钮项目。我使用按钮项目的几次让我头疼,并且需要使用onclick属性来跟随它。

尝试使用 aui:输入类型=&#34;提交&#34; 项而不是aui:按钮。