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

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.

Related

Prevent ignoring of some properties of Model by toJson overriding

When I save the records, I need the uid properties for the initial rows; because of I can assign a value each of them through the uid.
However, toJson overriding forces to ignore some properties like dirty, uid, etc...
How can I prevent this? Or How can I find a way to solve this?
You should not persist the uid on the backend or override it.
The uid is an internal mechanism used by Kendo UI to track its widgets and widget components to allow user interaction and component functionality. Each uid is generated dynamically every time the widget is rendered, so, attempting to do what your thinking of doing won't work.
The proper solution is you should assign your own unique id property to your records and persist it on the server/client. Then, when a change is made client-side, you post the record data along with its unique id, find the associated record by id in the backend database, and update the associated record data in the database.

Modeling a form with an Auto-Suggest Lookup in Backbone.js

I have report UI with a small form at top where the user looks up a person by name using an Auto-Suggest textbox, and I set a hidden ID field when they select one. They then enter a start and end date, and hit submit to load a report below. The report data is fetched using the Person's ID, and the date range as a Backbone route. I can also show the person's name in the report header since I have it from the Auto-Suggest lookup.
The problem is, if someone bookmarks a report (a nice feature to have), I'd like to repopulate the form (which shows the person's name) and the report header.
So, currently I have one route ('id/startdate/to/enddate') that sometimes is triggered by an already populated form model, and sometimes is triggered by a bookmark/refresh and needs to repopulate the form model from route data and server-side data.
How would you model this? I was going to have a model bound to the form:
{ id: 234, name: 'Bill', startDate: '1/1/2011', endDate: '1/1/2012' }
But I am struggling with this idea of sometimes needing to fetch the name and populate the form, and sometimes already having a populated form (and name). Feels like there should be a better design for my Backbone views/models/routes.
You can populate your Model from the router by triggering a message with the name, startDate etc parameters that the Model will listen to.
The same thing can be done in the View that sets the data on the Model. So, regardless of where you get the information from (View, Router), your Model would correctly hold the state.
Then your Form View could listen on changes to its Model, re-rendering itself with pre-filled information on Model change.
Hope it helps.

ASP.NET MVC 3.0 - Maintain model state

Am new to ASP.NET MVC 3.0. Request expert's view on the below mentioned scenario.
I have a customer details page, where only Name is editable. There are 10 other customer properties that are non editable and displayed using SPAN. When user submits the page, I need to update only the Name.
If am using EF, I will have to load customer again, overwrite name and then save. Otherwise I will have to maintain customer model somewhere.
Anyone tried caching model (or viewmodel) using session id? Is it a good practice?
You are almost thinking in right direction.
If am using EF, I will have to load customer again, overwrite name and then save. Otherwise I will have to maintain customer model somewhere.
In Update Method **Load Customer again and update name Only as required and then save
**For 2 reasons
The first and most important rule is 'don't trust user data'. and
Concurrency and to avoid saving old data. See this example
Instead of using Session, I will suggest to use Hidden Field for record LastUpdateDateTime and Customer ID which will be posted back in the model to retrieve record and verify LastUpdatedtime with database record
Tipically, you should use a view model different than the database model. Having said that,in your current case the situation is very simple, submit only the name to the controller and then set the Name property of the object you get from EF with the submitted name.
Caching the view model or the model isn't your concern. The database model caching is handled by EF, your problem is mainly a lack of clear application layering. IN fact I strongly suggest to learn a bit more about the MVC pattern, basic application architecture (2-3 layering) and when and how to use a OR\M (which EF is).
use hidden inputs for other properties in your form. In that way you can get all properties binded to your EF entity and you dont need to get entity again from db.
#Html.DisplayFor(model=>model.x)
#Html.HiddenFor(model=>model.x)
or you can serialize the entity(if you use POCO entities) and set to hidden input. When you post back you should deserialize the entity.
My choice is always first one. :)

Passing ModelAttribute across multiple requests in Spring MVC

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.

can we store viewstate in masterpage?

Example
I have a gridview having multiple customers.
When a user clicks on the customer link then the CustomerId is stored in the Session "CustomerId".
if i open multiple customer details on multiple tabs then the Session "CustomerId" is overwritten.
so it does not make sense to store customerid in the Session.
I just want to store customerids for different tabs
Is there a way by which i can store the customerid in the viewstate of the masterpage?
(Assuming there is a single master page and multiple content pages.)
I usually try to use the URL for page-specific state info. Using session or some other shared storage can easily get messy. If the amount of state data is a bit much for being passed around in the URL, one way could be to rework the way you store the info in the session. by creating a class for holding the needed data, store several such objects in the session and identifying them by some generated request id. This request id can then be passed in the url to allow the page to pick up the correct state info from the session object.
No. The master page does not "live" between requests to different pages, you will have the same effect as storing the value in the page's viewstate.
I would normally use the querystring for this kind of thing, primarily so that the pages are bookmarkable (if you use sessionstate or form values instead, it's impossible to bookmark the page). As you say in your comment to Fredrik's answer, this does mean that users can manipulate the URL to view different customers, but this isn't a problem provided you're checking that the user is allowed to access the specified customer.
If you really don't want to use the querystring, I'd go with hidden form fields to pass the ID around. You should still be checking whether the user is allowed to see the customer though, rather than just blindly assuming that all's fine.

Resources