Get multiple selected values from list in arrraylist on select in Spring MVC - spring

I have a bean class named AnnouncementDetails having attributes
Now on submit i want to get the values of the selected users in selectedUsers Arraylist. Any idea how to do that? ..............................................

Change datatype to String array and then convert to arraylist by Arrays.asList(selectedUsers).
String [] selectedUsers

Related

Yii2 ActiveRecord how to get only populated attributes from model?

When using an ActiveRecord to retrieve models it's possible to select a few columns. For example:
Product::find()->select('product_id,name')->all();
There are more columns than product_id and name in the database, but this example returns an array filled with all models, having only the product_id and name attributes filled. This is expected of course because I've passed them into select().
Now when looping through the found models and calling $model->getAttributes() it returns an array of all model attributes and their values (null for the not-selected attributes), including all columns that were not selected in the query but are in the model's attributes() function.
Is there a way to call a function similar to getAttributes() but returning only the populated attributes that were selected in the query: product_id and name?
I know it's possible to pass in exclusions to getAttributes() but I'd rather have the populated attributes returned based on the values I've selected in the ActiveRecord query.
In your case you should be able to use fields() method:
$attributes = $model->getAttributes($model->fields());
Note that fields() does not guarantee this behavior if you change model after find - unsetting (unset($model->product_id)) or setting ($model->some_field = 'some value') attributes will affect result of fields().

Grails inList Constraint get values of inList map in error message

I am using inList constraint to validate list of values. And I am not using directly command/domain class to show error message
ex:
name inList: ["Joe", "Fred", "Bob"]
if name is not from the list error message is shown as
Property [{0}] of class [{1}] with value [{2}] is not contained within
the list [{3}]
Instead i want to show a proper message having values as Property of name of class MyClass with value XYZ is not contained within the list Joe, Fred, Bob.
Whereas MyClass , XYZ and List values must be coming from cmd object that I use to validate.
Please help on how to show this message.
I got the solution for it.
I can get the rejected value from cmd.errors.getRejectedValue() and I can get list of values from cmd.constraints.name.inList

assign list items which are attributes in a viewmodel to table attributes in asp.net mvc3

I am using Asp.net mvc3
In my viewmodel I have taken an IEnumerable attributelike:
public IEnumerable<EmployeeDetailsViewModel> EmployeeList{get;set;}
In my controller this EmployeeList is having a list of 2*2 matrix. i.e. I have passed an array to this list.
My array name is :employee
attributes:
First Name
Last Name
Task
after result it is displaying employee list in EmployeeList like EmployeeList[0]: values of First Name, Last Name,Task
now I want to store the list values in table in database.
My table also contains similar fields like my array attributes
I am using sql Server 2008 as backend
I want to do this in my controller.
how can I do this?
do I need to use foreach loop to assign list values to attributes of table or need to do something else?
please suggest me something...
I myself resolved the problem:
can use foreach loop here like:
foreach (ModelName item in viewModel.EmployeeList)
{
TableName a = new TableName();
a.Project1up = item.FirstName;
a.Employee1up = item.LastName;
a.Allocation = item.Task;
}

Converting Object to Class object

in my Spring MVC project i m using Hibernate, by using Criteria API i am applying Group BY and Order BY clause. Query get executed on DB successfully and it brings result also but its an array of Object--
Here is code of Criteria API
Criteria criteria = session.createCriteria(DashboardSubindicatorSubmission.class, "DashboardSubindicatorSubmission")
.setProjection(Projections.projectionList()
.add(Projections.sum("InputValue").as("InputValue"))
.add(Projections.groupProperty("fkAccademicYearId"))
.add(Projections.groupProperty("fkAssessmentPlanID"))
.add(Projections.groupProperty("fkSubindicatorID"))
.add(Projections.groupProperty("InputTitle")))
.addOrder(Order.asc("fkAccademicYearId"))
.addOrder(Order.asc("fkAssessmentPlanID"))
.addOrder(Order.asc("InputTitle"));
List<DashboardSubindicatorSubmission> dashboardSubindicatorSubmissionList = (List<DashboardSubindicatorSubmission>)criteria.list();
session.flush();
transaction.commit();
return dashboardSubindicatorSubmissionList;
I am casting criteria.list() to List<DashboardSubindicatorSubmission> but when i try to do dashboardSubindicatorSubmissionList.get(i) on controller it gives me exception java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to mkcl.accreditation.model.DashboardSubindicatorSubmission.
i come to know that, though i m casting it to List<DashboardSubindicatorSubmission> still its an list of object[] thats why i cant do dashboardSubindicatorSubmissionList.get(i) because it returns me object of DashboardSubindicatorSubmission. (Correct me if i am wrong)
So how can i convert my result into list of DashboardSubindicatorSubmission class?
Does setResultTransformer() helps me in this case?
You have two options. When you use projections, Hibernate doesn't know how to respect each field because it uses the name of each field to build objects and he doesn't know the names yet.
Thus, your first option is to name the fields grouped so that they match the names of object properties. This is necessary even if the string you use in projection is already the name of the object field. Something like:
.add(Projections.groupProperty("fkAccademicYearId"), "fkAccademicYearId") // same value
.add(Projections.groupProperty("fkAssessmentPlanID"), "other") // other value
The second option is to do what you yourself suggested, create your own implementation of ResultTransformer. I reckon this a interesting option if you want to extract other object of this query, as when you make a report.

Grails parent child form validation

I have an Invoice
class Invoice{
static hasMany = [lineItems: InvoiceItem]
double total
}
class InvoiceItem{
String description
double price
double qty
}
My issue is with form validation. If a user enters a string or invalid number format in either price or qty I get a
Failed to convert property value of type java.lang.String
to required type double for property price
BUT the error is in the Invoice object NOT in the LineItems object therefore I cannot highlight in RED the form appropriately. (And field value remains at zero when displayed so message is somewhat meaningless to user)
I was thinking of using COMMAND object with String parameters and validating their numeric value but I can't figure how to bind the InvoiceItem List.
What is the appropriate Grails way?
I could do all validation on the client side in javascript but that is not my question
You can do it with command objects. You need to read:
http://blog.peterdelahunty.com/2009/01/cool-way-to-dynamically-add-entries-to.html
command object data binding
Grails command object data binding
http://grails.1312388.n4.nabble.com/validating-nested-command-objects-td1346994.html

Resources