Passing ModelAttribute across multiple requests in Spring MVC - session

I have a multiple criteria search form with a command attribute. On the first submit, results are obtained based on the options in command object. On the results page, there is a link to export all the results to excel.. I have implemented it using the XMLViewResolver. I need to pass the SearchForm model attribute to the controller that handles this export requests. Also, from search results page, user can click on a person's profile and can come back to search results again. I want to keep this Model Attributes in session across all these requests. How would I achieve that in Spring MVC? #SessionAttributes is probably not an option because, as I understand, once the request goes to different controller, this object is flushed.

You can store whatever object you would like (SearchForm model) in the session associated with the HttpRequest via request.getSession();
This will allow you to access your model from the session within all controllers.
You could also store the criteria as hidden form fields on the form responsible for the user action. For instance, when the user clicks export to excel, the button would be contained within a form which contains hidden form fields whose values are set from the previous SearchForm bean.

Related

Keep filter data in laravel

I have a page with table of some data (let's say posts). I have a form there which leads to the same page but with get parameters. So when I choose December in dropdown list form will lead to posts/index?month=december. Controller index method makes eloquent query using query string parameters to filter rows. After that I click edit post(or add new post) and change something through edit form. Then update or store method redirects back to index as usually. What is the best way to come back to index?month=december page? Sessions? How should I do it? Set some values from query to session in Controller index method when I come there first time and get them also in Controller method from session after editing/creating?
Try to append all request parameters in the redirect:
return redirect()->route('route_name',['month' => request()->get('month')]);
You can read more about this in this section in the docs.

Populate Wicket table with Ajax

Is it possible to have 1 input text field that accepts characters from the user, and as the user types, queries to a webservice are launched, and to update the contents of another component dynamicaly with the results of the webservice (which would be a table below the input text)?
If you prefer to use Wicket form controls and Data Table component then you need to make the call to the REST service in your impl of IDataProvider.
Otherwise, as I explained to you earlier today in your other SO post you can do this directly in JS without Wicket. It is a matter of taste and skills.

How to get object instead of string from dropdown list in Spring MVC

On my JSP page i have following dropdown list:
<form:select path="companies" name="company">
<form:options items="${companies}" itemValue="name" itemLabel="name"/>
</form:select>
This list displays name attribute of Company object. when I choose value and submit form, my controller receive this attribute.
I looking for way to how to receive this object in controller instead of single attribute.
So, first of all I recommend having the company id, if there is one, instead of company name as the value that will we submitted from the drop down. You can still display the name as the description but send the id as the value. An id would usually be the primary key in your company table, if you have one. Names are not very unique.
Then when you receive this id as a parameter in your spring controller method you will get a handle to the same list that was used to render the drop down and get the object. The thing about spring is that object models only persist as long as the request by default, so the list you used to render the drop down would have disappeared by the time the user sends the id back. The first request was used to render the page, and the second request is the sending of the parameter. So, you will want to persist this list in memory longer than the request. You can make that list persist longer by keeping it in the session. Spring has an annotation for making model attributes persist at session level. Just keep in mind that the object will then live in the session as long as the user is logged in unless you remove it.
I kind of wonder why you want the object. Often times the id is all you need because you set that in the db as the users company of choice and you are all set. However, I can't tell what you are trying to do.

Spring MVC update field based on calculation

I have page with spring form fields.
They're bind using command.
Couple of fields in form need to be update based on calculation from other fields.
Those fields can be modified by user.
How can I do that?
I think about trigger java calculation in model using onchanged in web page. But I don't know how access methods from model and how read fields from page.
I notice, that field are update by setter when page is submit. This is too late if I want calculate 'live' when depending fields are changing.
If you need these calculations to be done on server (java model)... I suggest to use input's onChange event as you said, send values with AJAX to server and calculate there, returning the result to client again for update UI in real-time.
With this approach, you will get the 'live thing' you are wondering for.

How to Bind Data coming from DB to spring input tag

i have the following scenario.
I have a list of auctions coming from the DB. I am displaying each auction properties on the JSP page as a record.
I am using spring 3.0.5, Tomcat 6.0.29 and eclipselink as JPA provider.
In my controller i am seeting this list as model attribute and passing it to the JSp page.
I want to use spring tags (form:input) to display the data.
Using Form backing bean we can display default values on a form. But in my case is there any way to bind data to form:input tag in the JSP?
Thanks
There is no reason to display your data in form:input tags other than that you want to allow your user to edit the data. If that is not the case, then using form:input just to display the data is pointless.
If you want to allow your user to edit the data, how are you going to get back the changes he has done? Obviously you are going to need your Command object. So there is no reason not to use formBackingObject() in your FormController to set values in your Command object, which will be consequently displayed on your JSP. When user will make any changes and submit the form, you will get all those modified values in your Command object which you can process in onSubmit() method of your FormController.

Resources