Setting textbox value in struts - java

I have a form in Struts 1.2 where I have text boxes (text box created using struts html tag) . I have retrieved the values for this text box from data base and then put in session attribute (session.setAttribute("UserInfo",userinfoobj)) now I wants to get values from session attribute and set this value as value of text box

Best way would be to use a form-bean to hold the values of your input fields and then bind this form bean to your JSP page using struts configuration. Populate this form bean and then set it in session or request scope. Struts will handle the values (for capturing and rendering) behind the scene automatically if you use appropriate struts tags.
Pls google how to use ActionForm with struts 1.x
EDIT: here is simple example on how to do this http://www.raistudies.com/struts-1/login-form-with-struts-1/

Use OGNL stack.
Use this: value="parameters.<your request variable name>".

Related

How to maintain the contents in the text field after redircting to same page jsp.

Hi I have jsp page That is having some text fields,I will fill that text fields and I will submit that form to server side.If any error comes in the server side I will redirect to the same page with error message Now I want that text field to remain as it is but in my case it is clearing.How to make the text field as same.
The two options available to you are:
Don't reload the page. Instead submit the data via AJAX or validate it by javascript. That way you don't ever have to touch the data in the form. Make sure your javascript highlights the fields that have errors in some way.
The endpoint that you're POSTing your data to needs to be able to recognise that the data is invalid and include that data when returning the user to the same page. I'm not familiar with jsp, but generally you'd do that by including variables in your template for your form that could contain the data, and passing empty strings on the first load of the page. If the page is then shown to the user again after a failed form validation, pass back the POST data that you received in your form request.
There are two option, you can dispatch the request/response to the same page instead of redirect, but you need to add an attribute and recover it in the JSP, or you can add the attribute in the session , recover the value to de text field and remove it using (if you are using JSTL)

How to set a value in JSP session using javascript?

Is it possible to set/assign value in session using java script?
Basically, I have few links on my JSP page and on click of those links, I need to set a variable in session.
session is server side concept, while javascript plays on browser
You can either use ajax on click of those link make a request to server and pass the info. and set it to session # server
Or
If those links on whichuser is going to click is your server's then pass some param with it. and track it at server
The easiest approach is to use a hidden input element bound to a bean property in session scope, and put a JavaScript onclick handler in a commandlink that sets the hidden element's value before submitting the page.

Various Questions about JSF

i'm doing a JSF2 project.
1 . i have a form with some checkbox. Look the following code
<ui:repeat>
<h:selectManyCheckbox value="#{wagent.selectedPra}">...
</h:selectManyCheckbox>
</ui:repeat>
So i use selectManyCheckbox inside ui:repeat and i want that the value of the different selectManyCheckbox point to the same value.
But when the form is submit i didn't have all the selected boxs.
How i can do that ?
2 . I have a form with some inputs. On my action i want to merge some pdf files, stay on the same page and that a pop-up appear to offer to download the merged files.
3 . Does all the managed beans with request scope are created for each request or just if i used them in the xhtml page?
4 . I have a commandlink to logout. On my action i use session.invalidate() and return "login". So i go back to the login, but when validating the login, my session managed bean doesn't seem to be created. Error is something like yourSessionBean is null. What's wrong ?
1: So i use selectManyCheckbox inside ui:repeat and i want that the value of the different selectManyCheckbox point to the same value. But when the form is submit i didn't have all the selected boxs. How i can do that ?
Let them point to a different value instead. With the given example the bean value will be overridden everytime until end of the loop. As an example, use a List<List<Pra>> in a bean (or whatever Pra means in your question):
<ui:repeat value="#{wagent.allSelectedPra}" var="selectedPra">
<h:selectManyCheckbox value="#{selectedPra}">
...
</h:selectManyCheckbox>
</ui:repeat>
2: I have a form with some inputs. On my action i want to merge some pdf files, stay on the same page and that a pop-up appear to offer to download the merged files.
At least two things needs to be done:
facesContext.getExternalContext().addResponseHeader("Content-Disposition", "attachment;filename=name.pdf"); // Force "Save As" dialogue.
facesContext.responseComplete(); // Prevent JSF from taking response in hands.
3: Does all the managed beans with request scope are created for each request or just if i used them in the xhtml page?
They are created for every HTTP request. The scope which you described only applies on view scope (if I understand you right).
4: I have a commandlink to logout. On my action i use session.invalidate() and return "login". So i go back to the login, but when validating the login, my session managed bean doesn't seem to be created. Error is something like yourSessionBean is null. What's wrong ?
You are probably accessing the session scoped managed bean the wrong way. You need to either inject it as #ManagedProperty or to grab it by Application#evaluateExpressionGet().

JSF, How to set a property in a different page/backing bean and then navigate to that page?

I am using JSF 2.0 and attempting to pass values between different pages in my App.
The setup is as follows:
I have a page called userSelect that has a backing bean userSelectBacking. On this page I display a list of users that can be selected and submit using an h:commandbutton, when the page is submit the navigation goes to a userEdit page.
I have a page called userEdit, that has a backing bean userEditBacking which displays the information for a user
and allows that user to be edited.
I would like to pass the user selected from the userSelect page into the userEdit page.
I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page, however when I navigate to the userEdit page, it loses the information I set.
is there a way that I can pass the values between the two pages/backing beans?
thanks
I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page
It should work.
however when I navigate to the userEdit page, it loses the information I set.
This will happen if the data loading logic is wrong, or you fire a redirect afterwards while the bean is request scoped.
To fix the data loading logic, just ensure that in case of a request scoped bean the same datamodel is preserved in the subsequent request. Usually you use the bean's constructor or lazy loading in the getter for this. If that is not an option, then you need to put the bean in a bit broader scope, e.g. #ViewScope or #SessionScope.
To fix the redirect issue, either just don't fire a redirect (i.e. remove <redirect/> from navigation case, or don't call ExternalContext#redirect()), or put bean in a broader scope.

Implementing dynamic pagesize in pagination struts2 + java

I am trying to implement pagination through display tag in Struts2.
Now my requirement is I have a combo box which has some page size value like 5, 10, 15 ..
So, How can I update that value in page size of display tag in Struts2 ?
You would create an exposed property on your action, named for example selectedPageSize. This property could be set to a default size (in your example 5). You would keep a hidden form field storing the currently selected value. This would then be used in your view with the display tag similar to:
<c:set name="selectedPageSize" value="selectedPageSize" scope="request"/>
<display:table pagesize="${selectedPageSize}" ... >
I'm not sure about struts2 in particular, but with JSF in general, if you bind whatever component you use to paginate into your backing bean, you can set properties such as that through an action.

Categories