liferay issue : display an image from a portlet to another - java

I 'm learning to write a portlet in liferay portal 6.1 and I use spring mvc portlet, I have an issue when I redirect page 1 to page 2 by using action phase in portlet.
And I will display some infomation by the id which have been passed when i redirect page 1 to page 2
So are there any ways to display an image from a portlet of page 1 to an other portlet in page 2 ?
Thanks in advance.

There is no way you can access the information passed from Portlet 1 in Portlet 2 using ActionRequest or RenderRequest, whichever possible.
First you need to get the original HttpServletRequest Object using PortalUtil class and then get the required parameters using ParamUtil, if possible.
The parameters which are passed through page redirection doesn't fall under any PortletRequest. Hence, they cannot be accessed directly using Action or Render request Object.

Related

How to change the url after forwarding a servlet to jsp page?

I have a search.jsp page that has some html content and a form. When the form is submitted, there is a servlet handle the form data and forward the results to the search.jsp page. However, the url in the browser after processing the form is changed to the servlet name:
http://localhost:8080/MyProject/SearchServlet
not the search.jsp page:
http://localhost:8080/MyProject/Search.jsp
How I can change the url to the search.jsp? In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?
You cannot do that by forwarding the request: you need to "tell" the browser to generate a new http request by using the response.sendRedirect() method.
Now the question is why do you want the url bar to display the name of the Jsp?
Hiding the real destination path is a desired feature when forwarding requests: users do not need to know the server side redirects (that's how they are also called) happening in your web app.
Think about it: to carry out its tasks a servlet potentially can forward the request a number of times before getting to the final destination: you don't want the url bar to change each and every single time.
Give a fancier name to your servlet like: "Search" rather than "SearchServlet" so that users will know they are on the search page of your web application and not in the "SearchServlet" page.
In addition to that, if you visit any professional website, you will hardly ever see the .jsp or .html or .php extension on the address bar. While that is not a requirement or specification and you are free to do so, I believe the first approach is best practice (it looks even better to me honestly). There is even a folder WEB-INF whose purpose is to hide your .jsp pages from direct access via url bar.
What I like doing is having a servlet as the landing-welcome page of the web app, that will be responsible to forward and redirect requests based on the user input and the inner working of the application.
Now back to your final request (pun intended)
"In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?"
What I would do is:
redirect the user to the "Search" servlet from the welcome/home servlet.
In the doGet method of the Search servlet I would forward the request to the search.jsp page (you could set attributes before forwarding if you need to).
In the search.jsp I would set the action attribute of the form to "Search" (the name of the servlet) and the method to POST.
In the doPost method of the Search servlet you would implement whatever logic you wish to implement and finally forward the request to the search.jsp
After hitting the search button (and even after the submit button is clicked) what the user will see on the address bar is simply
http://localhost:8080/MyProject/Search
Hope that makes sense.
Are you using the same search.jsp for searching and well as showing the result? It is possible to use the same jsp to perform both the functions but it's easier and desirable to make another jsp which will only show the results.
If you are not able to see the results on search.jsp then make sure that you are setting the proper response in the Servlet class before forwarding it to the jsp and also whether you are reading the response sent by the Servlet class properly in the jsp.
If you want, the page to not refresh at all, then go for AJAX.

Liferay alert user when portlet not present

Suppose portlet X is deployed to Liferay and has a friendly URL mapped. Suppose a user enters the Liferay Portal via a the mapped URL but the portlet is not present in the portal - it's deployed but not added to the page.
My problem is that when the user uses the mapped URL nothing happens - the portal gives no visual feedback, that the target portlet is not present.
How can I change that? I need some kind of an alert / notice to the user...
-- edit --
I need not use a second portlet to check the presence of yet another portlet.
Kindest regards,
AFAIK, there is no natual way to make that happen. A portlet need not always be installed on a page. So, the behaviour is quite normal.
One rather hacky solution I could think of:
Get hold of the ThemeDisplay object in a JSP using <liferay-theme:defineObjects /> which would expose the implicit object themeDisplay in the JSP scope.
Get hold of the type settings string using:
String typeSettings = themeDisplay.getLayout().getTypeSettings();
Type settings will have values like the below:
layout-template-id=foobar_2column
sitemap-include=1
column-1=foo_WAR_barportlet,abc_WAR_barportlet,56_INSTANCE_K4Vv,
column-2=baz_WAR_xyzportlet,
sitemap-changefreq=daily
So if you have a non-instanceable portlet with ID foo inside WAR
file bar, the portlet's unique ID on the layout will be
foo_WAR_barportlet.
Once you know the portlet ID that you're expecting to be present,
it's just a matter of string contains check.
<% if(!typeSettings.contains("foo_WAR_barportlet")) { %>
<h3 style="color: red">Alert! Portlet foo_WAR_barportlet not installed.</h3>
<% } %>
You can do the above steps even inside a theme, but you'll have to do it in Velocity instead of Java then. Hope that helps.
EDIT
You can add this line inside your portal_normal.vm
#if(!$layout.getTypeSettings().contains("foo_WAR_barportlet"))
<h3 style="color: red">Alert! Portlet foo_WAR_barportlet not installed.</h3>
#end
Yes you can achieve that using Inter-portlet communication, for notifying the user whether the portlet is added to the page or not. you need to create another portlet(lets call it ListenerPortlet) which by default sits on the page.
you can add the Listener portlet to the theme, so that it is by default added to every page.
Now, when you add your portlet to your page, your portlet should trigger a client-side javascript event and notify your Listener portlet that your portlet is added to your page.
From your portlet call,
Liferay.trigger(eventName, data)
and bind your Listener portlet to the event
Liferay.bind(eventName, function, [scope]) //make the scope as page
This way your Listener portlet will know if your portlet is added to the page or not. and you can display a message to the user if the portlet is not added.
For further reference check the IPC
and more specifically client-side Inter portlet communicaton
It would be better if we try this,
ThemeDisplay themeDisplay = request.getAttribute(WebKeys.THEME_DISPLAY);
Layout layout = LayoutLocalServiceUtil.getLayout(themeDisplay.getLayout().getPlid());
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
List allPortletIds = layoutTypePortlet.getPortletIds();
If the list is empty then the page doesnt contain any portlets.
Gettings the LayoutTypePortlet ensures that page the user has been redirected to is layout type portlet.

How to pass a parameter to previous page?

I am working on a JSP project. I have a page that calls another JSP.
Now the problem is, how to pass or use a variable in the called JSP page in its calling page?
You can save the variable in the HTTPSession or ServletContext object.
And in the calling JSP page, use or check session attribute for the variable.
session.setAttribute(objectId, Object); to set the variable.
session.getAttribute(objectId); to get the variable.
I have a page that calls another jsp
page
The problem lies here in this sentence.
Try to follow MVC. Use JSP just for rendering the View, and Servlet as a Controller.
Here, simple.souther.us, you find simple and awesome tutorials for newbies.
Your design should be
take one input param on page1.jsp
post it to some servlet , process it there, forward request to page1 jsp and pass param taken from page 1 as attribute
See Also
why-business-logic-should-be-moved-out-of-jsp ?

prevent duplicate submission of forms on jsp pages

Hi all
I've got some jsp pages and im using struts to handle my forms.
After submitting a form by user, the url shown in address bar becomes somthing.action, so when the user refreshes the page, the forms gets submitted again.
How can I handle this?
after submission of a form, Is there any possible way to show a ".jsp" url instead of a ".action" in the address bar?
Yes, use redirect-after-post. Either response.sendRedirect("foo.jsp"), or see here or here (depending on what exactly your code is).
If you are using Struts 2, it has a Token interceptor, to prevent duplicate form submissions -
http://struts.apache.org/2.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/TokenInterceptor.html
And an example: https://cwiki.apache.org/WW/token-interceptor.html
If you redirect user to some page (it can by same as form) after you make action, it will not send post data if page will by refreshed.

Creating render urls to link between pages in a community in Liferay

I've set up a Liferay community, along with a number of pages each defining there own set of portlets, themes and layouts. I want to be able to create links between these pages. For example given that I am at the top level page (lets call this 'home'), I want to link to another page (for example 'blog') from within one of my portlets.
Is there a way, either using the Liferay or Portlet APIs to create a render url based on a page parameter that I supply. As far as I can see, the liferay-portlet-ext taglib defines a renderUrl tag that I can use to create a URL that links back to the same page, however I can't see a way to specify the page name in this tag.
I'm using Liferay 5.2.3. Thanks in advance.
You can either link them with friendlyURLs (/web/othergroup/blog) or you can use
PortletURL portletURL = PortletURLFactoryUtil.create(
request, portletName, plid, ‎lifecycle);‎

Categories