JSF 2.0 Passing object between request scope beans using a link - java

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.

Related

Spring MVC recovering page state after saving

I have a Spring MVC application where I sometimes have to add a new object to a list, and at some point, save the page. At every point where I need to add something to a list, a controller action is called and when it is done adding it returns the page. The state of the page then is lost. (ie. scrollbar position). I would like to preserve the page state, also after saving the page (which does a redirect to the new page)
Right now I am putting some variables in the session, and reading them out EVERY time. I find this quite ugly.
Does anyone know like a solution to this? Or any third party dependency which can make my life easier on this? :)
Thanks in advance.
Ps. I hope my question is clear, and not too abstract. If so, I will try to clarify it more.
One way to avoid the complete page refresh is to use ajax. Submit the new List item via an ajax request and the browser will not perform the full refresh.
Exists another variant. You may perform AJAX call to Controller method and send only the data you need to save. After that through #ResponseBody annotation you can return refreshed data or any other result. So this solution force you to use AJAX call. You may use JQuery for this purpose.

How to lock and Unlock a URL in java

There is a hyperlink in my webpage, on click of which it navigates to a jsp page. once am in that jsp page i should be able to lock the jsp page where if others access the same jsp page it should show an alert or a warning saying its already in use. when i come out of that jsp page then if others access the URL it should navigate to that page. Any idea how to achieve that in java? Am using spring framework 3.1. it is a web aplication. Can i handle it in javascript?
you can create a static counter.
you can also set a key/value pair on the first request and your code decides to show alert based on the value of the key.
To make that work you need couple of things:
1. create a static lock, using concurrnet.util library objects
2. when a user get to the jsp, try to lock. if no success - alert, if sucess - just show the page
3. when suer holding the lock leave the page, unlock, making any subsequent request to compete for the lock again.
Better off, just for security define that there will be only one instance of the servlet created from the JSP. That is a configuration on the web server.

Can we call html ids from Wicket Java class

Is there a way to call ids that are created in html page without specifically giving wicket:id in the component. If so, how can I do that?
What I am trying to accomplish is to get access of the component to assign a default value from CMS. I could assign the default value using JS, however I need that value to be populated to Java so that I could use that in my class to update my business logic depending on the defaulted value.
As I understand it, Wicket is component oriented which means that single component (piece of HTML code) can't see all the HTML. And my guess is that at some late stage, you want to look at the whole page HTML and do something with a DOM node with a specific ID.
I'm not sure what the correct solution should be. One way is to put the value into the user's session, so it's available for each component and your business logic.

JSTL/JSP dynamic form page state

Working on a large Web app, I have houndresds of JSPs.
Each JSP includes (ONLY) a set of internal tags, for instance:
<AAA:INPUT value="bbb" state="<%=getPageState()"/>
This tag is rendered into an HTML input field, with a readonly/enabled state, based on the return value from getPageState().
This basically allows me to set the complete page as enabled/disabled from a single entry point.
I don't like this (mainly because it drives me away from writing the HTML I want and I need to maintain attributes for each HTML attribute I want), I know I can something similar on client side with JavaScript.
Are there other approachs to control the state of a complete JSP form in a single point on the web-server side?
The way I would go about this would be to convert the getPageState() method into a tag, and then use this tag within your custom tags. That way, you don't need to explicitly pass in the page-state on every invocation of your custom tag.
Of course, this means that you would have to modify all your custom tags to use this new tag, but I think that's better than having your code littered with explicit calls to get the page state. Not to mention, an opening for inconsistencies if a developer forgot to check the page state.
Also, now the decision as to how the element needs to be rendered rests in the custom tag itself (where it belongs).
The only problem I see is that now you have to make multiple (redundant) calls per element to get the page state, which is not that efficient. To get around this problem, you can have your custom tag (that gets the page state) set a page attribute, and have your custom tags inspect this page attribute to decide if the form element should be disabled or not (another way would be to create a variable with the scope AT_END).

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

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.

Categories