Showing posts with label popup. Show all posts
Showing posts with label popup. Show all posts

WSRP - Open a JSP/Servlet in dialog box from portlet

In the Open a JSP/Servlet in dialog box from portlet entry i talked about how you can open a JSP in dialog box from portlet. I wanted to see if that code will work in WSRP and how it will work so i tried consuming the sample portlet as WSRP portlet and tried clicking on the dialog box and it worked also it was able to pass query parameters.

This is the code in index.jsp that i use for creating URL pointing to popup.jsp

<input type="button" onclick="openPopup('<%=renderResponse.encodeURL(
renderRequest.getContextPath() + "/popup.jsp?userName=poup.jsp") %>')" value="Popup.jsp" />


When i look the HTML generated by this code in the local version of portlet this is what i see

<input type="button" value="Popup.jsp" onclick="openPopup('
/wpcert/PA_ServletPopup/popup.jsp?userName=poup.jsp')">


In my local the portal the ServletPopup is installed at /wpcert/PA_ServletPopup context so that's what get appended to the URL and the url goes unchanged in renderResponse.encodeURL()

THis markup is generated when i see same portlet in the WSRP world

<input type="button" value="Popup.jsp" onclick="openPopup(
'/wps/WsrpProxyPortlet/ResourceProxy/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48dT48Yj5odHRwOi8vbG9jYWxob3N0OjEwMDQwPC9iPjxwP
jxuPndzcnAtc2VjdXJlVVJMPC9uPjx2PmZhbHNlPC92PjwvcD48cD48bj53c3JwLXVybFR5cGU8L24-PHY-cmVzb3VyY2U8L3Y-
PC9wPjxwPjxuPnA8L24-PHY-MTJfVlZJTE1LRzEwODVGOTBJUzQ0SlJRNTMwMDc8L3Y-PC9wPjxwPjxuPmc8L24-
PHY-MV9WVklMTUtHMTAwTjA4MElBMEc3NTdDMDAwMDwvdj48L3A-PC91Pg!!/4UXoYMjvvzfhLvelKL9fiEIl5fs!/wpcert
/PA_ServletPopup/popup.jsp?userName=poup.jsp')">


As you can see the renderResponse.encodeURL() method generates a big string starting with /wps/WsrpProxyPortlet/ResourceProxy in the URL, so when you request a JSP the request will first go the consumer portal, there the ResourceProxy portlet will tunnel the request to producer and return the results back

Open a JSP/Servlet in dialog box from portlet

In the last few days i talked about what are different options for opening a dialog box from portlet and but all those approaches have there disadvantages. So the easiest option would be to open a dialog box pointing to a Servlet or JSP that resides in same porltet application. THis approach wont work if you need any portlet context but should work otherwise.

I built this sample portlet to demonstrate what i mean. My Sample portlet has a popup.jsp and when ever you click on open popup.jsp button it opens popup.jsp in dialog box, i am passing userName parameter to the JSP. You can download the sample code for portlet from here

THe doView() method of sample portlet forwards control to index.jsp which looks like this

<%@page language="java" contentType="text/html; %>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/portal.tld" prefix="portal" %>

<portlet:defineObjects />

<script type="text/javascript">
<!--
function openPopup(popupUrl){
if (window.showModalDialog) {
window.showModalDialog(popupUrl,"name","dialogWidth:455px;dialogHeight:450px");
} else {
window.open(popupUrl,'name',
'height=500,width=500,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
}
}
//-->
</script>


<input type="button"
onclick="openPopup('<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/popup.jsp?userName=poup.jsp") %>')"
value="Popup.jsp" />
<br/>


I am creating URL to a JSP which resides in same portlet application like this renderResponse.encodeURL(renderRequest.getContextPath() + "/popup.jsp?userName=poup.jsp"). I am passing userName as parameter to the popup.jsp, i used a JSP but it can be a servlet


THis is how my poup.jsp looks like


<html>
<body>
Hello from popup.jsp
<h1><%= request.getParameter("userName") %></h1>

</body>
</html>


The popup.jsp is pretty simple, all that it does is read value of userName request parameter and print it in HTML