Stripes & jQuery - how do i sent over variable in an ajax request? - java

So I have a table in a jsp page with several rows and checkboxes for each row. I created a js function that creates an array of the value on the checkboxes. I want to send this array over in an ajax call so I toJson-ed it but I dont understand how actionbean variables get set with these parameters. Can anyone help? THANKS!

Good question. Typically you create instance variables on your action beans, expose w/ getter/setters, and they are populated automagically via form post params or get params.
If you had a small handful of checkboxes, you could make a boolean for each one on your ActionBean, then your ajax call could be to a URL like "Preferences.action?box1=true&box2=false&box3=false".
If you had a ton, you could create a List on the ActionBean. I've only dealt w/ this the non-ajax way, but you'd set the name attribute on the checkbox to something like this: name="preferences[0]". I think you could do a jquery ajax call this way too, but you might have to url encode the name of the param.
I think you could also look into the jquery form plugin to simply POST the json over.

Related

Wicket PageParameters Interfere with Page's Model Object

I have a simple page with a form that includes a TextField. I have set the model object (Serializable POJO) with a single variable called containerId. No matter if I use a LoadableDetachableModel or the standard Model.of(), if I have a PageParameter also named containerId then the form model behavior breaks causing the value to always remain as what the PageParameter value is set. If I change the page parameter name to container and pass that value into my model on initialization the problem goes away. Has anyone else experienced this? If you can provide any details as to why this is happening?
It sounds like the conflict is in the URL that is produced when you submit the form. I would speculate that your page is mounted in such a way that the PageParameters are encoded as query parameters in the URL, and that the form is submitted via a GET which therefore encodes its values as query parameters in the URL. Thus two query parameters with the same name get added, and one of them is lost.
So, there are at least two options you can try:
Change the form to POST (add method="POST" to the HTML)
Mount the page in a way that encodes the page parameters differently, for example with something like the following:
mountPage("/page/${containerId}", YourPageClass.class);

Using request across 2 different struts-action flows

I have a struts action flow(struts-1.x framework), which, when executes, the action class ActionFlowActionUnit1.java sets a String variable varName to request using the code
request.setAttribute("varNameFromRequest", varName);
and the flow finally leads to the loading of a jsp Page1.jsp.
Now, Page1.jsp contains a button, which, when clicked, initiates a new struts action flow, which has the action class ActionFlowActionUnit2.java. In this class, I want to use the varName which I had set in request using request.getAttribute().
How can I do it WITHOUT USING SESSION?
Technically, I'm not sure if achieving this using requestis possible, because, triggering a new struts-action will lose all other information in the request that was previously set (if I'm correct).
I couldn't get anything from Google.
As you say, it is not feasible technically as you want it (every http request from the browser creates a new HttpServletRequest object)
You have 2 options:
Using the Session, which you want to avoid as far as I understand
Bring back and forth some parameter into every successive request with the value you would like to keep.
The second option would mean to store some parameter inside your Page1.jsp <form> with the variable you need your second action to receive, and then rinse and repeat. This is a pure html form solution.
If you are implementing a complex flow, this looks a fair case to have a look at Spring Webflow. There you can manage flow-level variables, which are stored at a "different" scope than request or session, and looks exactly what you want.
http://projects.spring.io/spring-webflow/

JSF 2.0 Passing object between request scope beans using a link

for some time I'm trying to pass an object between two pages, that are using different managed beans. I'm using jsf 2.1 and primefaces 3.1.
My case:
I have a p:dataTable and one column is a h:link where the user can click to edit the current object
after clicking a new page will be open, where the content of the selected object will be displayed for further actions
I've tried sending some parameters from page 1 as GET request and process them from page 2. It works, but this is not the solution I want. I need the whole object that was selected. I was thinking to pass it in the request map, or to declare the bean responsible for page 1 as #SessionScoped and to inject this bean from the second one.
What I don't succeed is to find out which object was selected before changing the page. I've tried using ajax, or an actionListener for the link tags, but nothing works. Before performing the actionListener or the ajax event, the page is changed...
How can I first set the selected object/row and only after that change the page?
Thank you for your help.
Instead of using h:link, you would want to use p:commandLink, set the selected object in actionListener, and change the page in oncomplete phase, with a javascript line like location = 'page2.xhtml';.
You can find the related tag documentation below. It also exists in version 3.1.
http://www.primefaces.org/docs/vdl/3.4/primefaces-p/commandLink.html
PS: Either page 1 or page 2 still needs to be session-scoped, otherwise it won't work.

jqGrid using a servlet to store the data, how to access the data from a different java class

I'm using jqGrid for a table input and setting up the url as a servlet which will deal with the GET and POST requests and save the rows to a Java object.
I'm using webwork web framework and I was wondering how I can get access to the object that the servlet is saving the data to.
One way I have thought of is to just call the GET method from the Java action class which the servlet will return a JSON string with the object data.
Is there a better design for doing this?
This is probably not too clear so ask questions so I can help get across my point.
Thanks
Since you are using WebWork, why are you writing a Servlet? The jqGrid can post data to an action directly. Let the action do the work that the servlet is doing.

How to create a servlet to return a database query in a certain div

I need to create a very simple application:
I need to have a form that submits data to a servlet
The servlet then queries a database to retrieve a list of reports based on the criteria given by the form
The returned list of documents has to displayed in a div on a .jsp page
I am not sure about the last one (number 3). I know how to call the doPost or some other method using jQuery (but not when triggered by a button), only using a timer, but I am sure it's a similar thing. But I want to keep it as simple as possible and avoid jQuery if possible.
If I call the servlet and return the data without using jQuery, how can I specify the location (i.e. which div) I want it to be returned in.
Where you display the contents of your Ajax request is up to the client-side Javascript.
The servlet will just create the data (either as HTML, or as JSON or XML, depending on what you need on the client).
If you do not want to use jQuery (or prototype.js, or something similar), you have to code the logic for firing off the Ajax request, and for parsing the result (including how to display it) yourself.
I'd say, stick with jQuery. That will keep it "as simple as possible".
If you are not sure how to start a new request when a button is clicked, check out this tutorial.

Categories