I am using java liferay portal, in which there are multiple portlets. I want to create a portlet with a form that when it is submitted the data is retrieved and the specific result is shown in some other page portlet. But unfortunately these things are not going in the way.
I was thinking of using sessions but 2 problems arrised.
javascript value assignment to java variable.
if the values are passed to the page on which the specific portlet is placed, that portlet doesn't get the values.
Then I heard the concept of Inter Portlet Communication(IPC), and took some help from "liferay in action" but there the code works if both the portlets are placed on the same page, and my requirement is that one portlet is placed on first page and when the form is submitted it is redirected to the second page, to the second portlet for getting the parameters. I tried more example but its not working in my way.
I have found another way of, a relatively easiest, just tried that wiki from liferay
As i understood, you have some JavaScript parameters which you want to pass to the next page. You can, though, do it with APPLICATION_SCOPE of PortletSession and you can solve the problem of converting JS params to Java by placing the values in a input. If these input vars aren't supposed to be written by the user and you take them from somewhere else, you can make the input hidden:
In your jsp:
<form>
<input type="hidden" id="myinput1" name="in1" value="">
<input type="hidden" id="myinput1" name="in2" value="">
</form>
<script>
var a = "avalue";
var b = "bvalue";
document.getElementById("myinput1").value=a;
document.getElementById("myinput2").value=b;
</script>
Then submit the form when you need to. Next, you'll be able to do like this in the ProcessAction method of the portlet:
String a= request.getParameter("in1");
String b= request.getParameter("in2");
PortletSession session = request.getPortletSession();
session.setAttribute("a", a , PortletSession.APPLICATION_SCOPE);
session.setAttribute("b", b , PortletSession.APPLICATION_SCOPE);
In the other portlet, you can find it by calling
session.getAttribute("a",PortletSession.APPLICATION_SCOPE);
This, of course, if you can't simply place them in the next page's url.
As far as I know IPC does indeed only work between portlets on the same page. Also the portlet specification doesn't provide a generalized mechanism for switching pages so you can only use portal vendor specific ways to achieve that. But using public render parameters and a correctly constructed Liferay URL to another page you should be able to achieve the result you want: http://www.liferay.com/web/guest/community/forums/-/message_boards/message/1207858
Related
I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.
I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.
Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)
Things I did so far : clear the browser cache.Code works fine but not the expected result.
Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.
Thanks in advance..
You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.
Your approach of clearing cache is correct. Coupled with that, you can use this approach.
<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>
One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.
When the user press the browser's back button, user can come to the
previous page where he submitted the details. And when the user submit
this, data is saved once more to the database.
According to how you described it, that is based on a doGet request. Which means every time you visit that URL, it will send the request with whatever parameters were added.
As someone already mentioned, if you switch the form to a post method and switch the Servlet to a doPost, you won't have this issue anymore.
Alternatively you can circumvent this with a javascript solution. Here are some options:
You can check if the user clicked the back button, disable form if true.
Another way is by storing a cookie which you check on page load, if it exists you can disable the form.
You can use this code also
$(document).ready(function() {
function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
You must use a Post-Redirect-Get pattern: https://en.m.wikipedia.org/wiki/Post/Redirect/Get.
Actually, every use of standard HTML forms with method="post" should be implemented with that pattern. It doesn't have any use for AJAX-posted forms, which actually could be another solution but will require more work and probably some architectural changes.
I had this same problem while building a django web app, and my solution was to not allow caching of the html that contains the form. In your request handler, do not allow the browser to cache the page. This will force the browser to get the page fresh from the document.
Which, in this case, you can just verify in your request handler if the requested form has already been submitted.
My code for reference:
from django.views.decorators.cache import never_cache
#never_cache
def GetForm(request, pk):
# Logic #
if (IsFormCompleted(pk)):
# Handle request #
Here is a solution.
give a random id in a hidden field on the form. Then on the server side, if the user resubmit, check if the random id already on the database. If so, redirect user.
I have a JSP file in which there are two textareas and a submit button all in a form ;
<form action="" method="post">
<textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
<input type="submit" name="Send Command"/> <br/>
<textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>
this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.
I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.
what I can't find is how can I do all the things that I said above without going to a new page while using form action.
I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:
<%
DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
%>
<p>Login Successful</p>
<% } else { %>
<p>Login Failed</p>
<% } %>
also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.
p.s: I'm using tomcat and Intellij for developing this web application
and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value
Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.
If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.
You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.
You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.
Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.
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.
I'm trying to fill-out a form automatically and press a button on that form and wait for a response. How do I go about doing this?
To be more particular, I have a a --HUGE-- collection DNA strains which I need to compare to each-other. Luckily, there's a website that does exactly what I need.
Basically, I type-in 2 different sequences of DNA and click the "Align Sequences" button and get a result (the calculation of the score is not relevant).
Is there a way to make a Java program that will automatically insert the input, "click" the button and read the response from this website?
Thanks!
You can use the apache http client to send a request to a web site.
Look at the source to the page in question, and you'll find the part. This contains all the fields that need to be sent to the server. In particular, you'll see that it needs to be sent as a Post, rather than the more common Get. The link above shows you how to do a post with the http client code.
You'll need to provide a nameValuePair for every field in the form, such as these ones:
<input type="hidden" name="rm" value="lalign_x"/>
<input type="checkbox" name="show_ident" value="1" />
<textarea name="query" rows="6" cols="60">
It will probably take some trial and error for you to get all the fields set up correctly. I'd recommend doing this with small data sets. Once it all seems to be working, then try it with your bigger data.
In Python you can use mechanize library (http://wwwsearch.sourceforge.net/mechanize/). It's quite simple and you doesn't need to know Python very well to use it.
Simple example (filling login form):
br = Browser()
br.open(login_link)
br.select_form(name="login")
br["email"] = "email#server.com"
br["pass"] = "password"
br.submit()
You could probably do this using Selenium.
I am trying to modify a portlet to load data for a table over AJAX because the WS calls take a ridiculous amount of time to complete. The table is basically an overview with one entry per table row and a link in each row to more detailed information on the entry.
Here is how I am currently creating the URLs for each row in the table:
<portlet:renderURL portletMode="VIEW" windowState="maximized" var="showURL">
<portlet:param name="id" value="${entry.ID}"/>
</portlet:renderURL>
I have created an AJAX servlet to receive the AJAX calls and return JSON which will be added to the table dynamically using jQuery callbacks. The servlet works fine and the rows are added to the table with no real problems. The problem I am having is currently with the links that should be in the table.
My question is how do I mimic the above JSP code in the servlet to generate the correct portlet URLs?!?! I'm a bit new to portlets and their URLs seem to be a serialized mess of gibberish to me.
Take a look at this JSP page for an example of how this is done -- one good method, anyway. Look at the definition for editPortletUrl near the top of the page, then look at how it's used later.
There's a PORTLETID token embedded in the URL, which is later replaced with a real value by JS.
This portlet uses Fluid Infusion, which I recommend highly for powerful, accessible, higher-order widgets based on jQuery.
Brian said
Thanks for the idea. I tried doing what you had but I don't see how the JS will be able to sub the real ID for the placeholder. Using renderURL I get this in the JS:
var baseShowUrl = "/wps/myportal/portalname/!ut/p/c5/hY7NCoJAFEafpSe4X_Pf0hJ0Sied6M9NCEVIWS2ioqdvwo2b6H7LczhcqijsUj-aY31vrpf6TFuq9C52yJ32DAZCworYz_V0DIDRqq1fTdu8D_tOFUra0oqgZoKD-VLabBpzQPXVDVWqV9UjE6qFzGcmD1XQ-vtHz0jSQgVjmXnr5xxm2HH8uAgdnyRRKnQGJJpJMCcXLI9WSLj6wwW59Noe6NYafjr59BkNBh8sH-CA/dl3/d3/L0lDU0NsQ1FvS1VRIS9JSFNBQ0l3a1FBd3FibTZtLzRDMWI5WUF4RW1TUVVnZyEvN19ETjBNTjdSMjA4MDQ1MEk0RFJPN0pCMDAwMi92aWV3L0NPTlRSQUNUSUQ!/";
UPDATE from Drew Wills
Could you show me how your or tag?