How to send data to ActionBean via JavaScript? - java

I'm playing with Stripes and I wonder if it is possible to send Javascript generated data to ActionBean. To be more specific, when I click with my mouse on certain element on page, I want to send ID of that element back to ActionBean after clicking on stripes:link. Providing I already have that ID saved in a JS variable id, how do I do that?

Are you using parameterised link?
<stripes:link id="mylink" href/beanclass="..." event="...">
<stripes:param name="id" value="some_default_value"/>
Click on me!
</stripes:link>
Which would most probably generate: http://mysite.com/...?id=some_default_value, which you would later use javascript to change some_default_value to the id you want?
Note: Suggestion unverified. I've no dev tool installed on this old lappie.
Edit: On second thought, why not just write some javascript to append "?id=" + id; to the link's url address?

create a hidden input element in your html
<input type="hidden" name="?" id="?" />
use javascript to set the value of it
document.getElementById("?").value = ??;
and the value will be posted with your form submission.

Related

Not saving texts when selecting other option from dropdown list in HTML form

I am using the following code to develop a dropdown list in a html form.
<html:select property="complaintCategory" onchange="showfield(this.options[this.selectedIndex].value)" >
<html:option value="option1">option1</html:option>
<html:option value="option2">option2</html:option>
<html:option value="option3">option3</html:option>
<html:option value="Other">Other</html:option>
</html:select>
<div id="div1"></div>
I want to create a text box so that user can write something when they select Other option. The function showField(name) is creating the new text box.
function showfield(name)
{
if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="complaintCategory" />';
else document.getElementById('div1').innerHTML='';
}
The problem I am facing is that when I select "Other" option from the dropdown list and then write something on it, it is not saving the texts, it is saved as value Other only. I want to pass the texts written in the text box as the complaintCategory. Would really appreciate someone's help on this, i am stuck.
Thanks in advance...
(I'm working on the assumption that <html:select property="complaintCategory" will output <select name="complaintCategory". It is usually helpful to narrow down the problem to server side code or client side code and provide only one of them.)
When you submit the form (after selecting other) you will have 2 elements with the same name.
Whatever server side code you are using to read the submitted form data, it appears to be taking the first value.
Either:
Modify your server side code to get the second value (I don't know the API you are using to parse the form data so I can't tell you the specifics for that)
Call the generated input by a different name and an if statement.
e.g.
client side:
if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="otherComplaintCategory" />';
server side (psuedo code):
IF complaintCategory is "Other":
THEN complaintCategory = otherComplaintCategory

Inter Portlet Communication page submittion

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

Java print a template

The problem is pretty simple but I cant find a straight-forward solution online.
I have a document template, which I want to be able to populate with information (different information, more than once) and then print. My initial thought was to create a html template, and then send a parsed version to the print.
I suppose there is more than one question here:
What is the best approach to solve this problem?
If the HTML parsing is the best solution can someone point me in the correct direction as I dont know where to start.
Thanks.
-- EDIT: a better example to clear up any confusion.
On a java GUI program, the user is able to pull up a customers information. I wish them to be able to print an invoice at the click of a button. My current problem is deciding how to populate the invoice with the customers information and send it to the printer.
I would go for a web HTML form. Let the user populate the input fields and pass them in the POST body when a form submit button is pressed. Then you analyze the input and create the final document version (on the server) and only then return the document to the client with an appropriate MIME type in the response header (that's how you enable the printing).
For example (simple form with submit button)
<form name="input" action="html_form_action.asp" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
EDIT:
For building document template take a look on playframework.

Java seeking referer

I am using Struts and Java. The problem is that I have a page with some results. The user can click an item and edit it. I want after editing the user to be able to return back to the results. Back isn't broken but if he submits the form for update he would have to click back 2 times I think and may have problem.
I have tried header("Referer") but this doesn't work in Internet Explorer.
I am trying to implement a solution. Any ideas? My idea is to save url and move around an ID of that url. And when I want to return back get the url from ID. Storing it in the session is not a solution because the user may have opened multiple windows.
Passing a URL as a request parameter may create security issues. Powerlord is right that the USER can alter the referrer header. This will allow the user to visit a page, something they can do anyway. More seriously, following a URL that is in a request parameter allows an attacker to send the user to a page of the attacker's choice, with the appearance that this page is recommended by your application. So the answer from BalusC can enable Cross-Site Request Forgery.
The best way is to pass it around as a request parameter. On the edit link or button, just pass the current URL along as request parameter. Here's an example with a link:
edit
Or if it's a button to submit a form, then rather pass it as hidden input value in the same form:
<input type="hidden" name="from" value="${pageContext.request.requestURI}">
In the page with the edit form, transfer it to the subsequent request as hidden input value of the form:
<input type="hidden" name="from" value="${param.from}">
In the action method, just redirect to that URL after finishing the action. Since I don't do Struts, I can't give a detailed Struts example, but here is how you would do it with "plain vanilla" Servlet, you must be able to port it to a Struts approach:
response.sendRedirect(request.getParameter("from"));

sending request to a page

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.

Categories