what is a good way of passing parameters to SimpleJdbcTemplate - jdbctemplate

being pretty new to Spring and Jdbc, I am looking at a code from a Spring book and it is like this:
public voidaddSpitter(Spitterspitter){
jdbcTemplate.update(SQL_INSERT_SPITTER,
spitter.getUsername(),
spitter.getPassword(),
spitter.getFullName(),
spitter.getEmail(),
spitter.isUpdateByEmail());
spitter.setId(queryForIdentity());
}
Ok, so first param should be my SQL statement, but for the second param well does it get ugly in the code if there are like 15 columns in my table and I want to write 15 lines of those .get() methods? is there any nices/cleaner way of passing these?

Get the values into an Object List (in a helper method ?) and convert it into an array while passing to update().
For eg:
List<Object> insertValues = new ArrayList<Object>();
....
insertValues.add(spitter.getUsername());
....
jdbcTemplate.update(SQL_INSERT_SPITTER, insertValues.toArray());

Related

Passing collection name dynamically

Can any one suggest for a simple but tricky query. I have a function like
public object GetPropertyValue(object obj_, string property_)
{
return HttpContext.Current.Request.Headers[property_];
}
But I want to use Obj_ instead of hard coding the headers in return value. Because it can be Headers/ cookies/...or anything
Something like HttpContext.Current.Request.obj_[property_];
Please anyone???
You can use
HTTPContext.Current.Request[YOUR_PROPERTY_NAME] ;
it has an indexer which reads from the QueryString, Form, Cookies, or ServerVariables collections.
http://msdn.microsoft.com/en-gb/library/system.web.httprequest.item.aspx

EditorForModel and DisplayForModel don't work on Expando objects

Or at least, they don't do what I was expecting. My Action method looks like this:
Function Test() As ActionResult
Dim model As Object = New ExpandoObject()
model.FieldA = 123
model.FieldB = "This is a string"
model.FieldC = DateTime.Now
Return View(model)
End Function
and the view looks like this:
#ModelType Object
#Html.DisplayForModel
I was expecting it to produce the same thing you'd get if the model were a real object with those fields, but instead I get this:
[FieldA, 123][FieldB, This is a string][FieldC, 3/29/2012 12:10:24 PM]
EditorForModel does the exact same thing. Is this the correct behavior, and what's the best way to implement the behavior that I'm looking for?
Yes, this is the correct behavior. Default metadata provider (DataAnnotationsModelMetadataProvider) uses reflection to get model information and that is the reason you get this result.
I would suggest to create a custom metadata provider that will work correctly with dynamics. Try to inherit from this class - http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelmetadataprovider.aspx.

getting XMLNode attribute's values

I am working on asp.net application. I have a function like this:
Public Function ExtractText(node As XmlNode) As String
End Function
I need to pass following XML as input to above function:
<myrequirements Id="7743" Type="tcg_Concept20_sc_323256419566173_context" StartNode="2724" EndNode="2869">
</myrequirements>
Then I need to get StartNode and EndNode attributes values.
Do I need to use XPath. Please suggest.
Regards,
Asif
You could try using node.Attributes("StartNode").Value and node.Attributes("EndNode").Value which will give you the values of those 2 attributes assuming the XmlNode instance passed as argument contains the XML node shown in your question.

Grails data binding

A Grails controller received is called with the following request parameters:
defaultPrice[0].amount 22
defaultPrice[0].currency 1
defaultPrice[0].id
defaultPrice[1].amount 33
defaultPrice[1].currency 3
defaultPrice[1].id
I've defined the following command class:
class PriceCommand {
BigDecimal amount
Integer currency
Integer id
}
I attempt to bind the request parameters to a `List' in the action
def save = {List<PriceCommand> defaultPrice ->
}
But within the action, defaultPrice is null.
It requires an command with existing list of data, with specified name, that will be filled with data from request.
Try
import org.apache.commons.collections.ListUtils
import org.apache.commons.collections.Factory
class PriceListCommand {
List<PriceCommand> defaultPrice = ListUtils.lazyList([], {new PriceCommand()} as Factory)
}
and use this command inside controller. It should works
I'm not sure if this is what your looking but it may help...
1.) I think indexed params only work if you have a parent-child or one-to-many relationship. For example you might need to introduce a PriceCommandParent which contains a list of PriceCommand. I may be wrong on this and I welcome any corrections.
2.) I've found that indexed params aren't as magically as some of the other areas of Grails/Groovy so sometimes i'd rather deal with the mapping myself. Below is how i've handled it in the past....
def things = []
params.each{name, value->
if (name.matches('^(thing\\[\\d+\\])$')){ //<-- look for 'thing[x]'
things.add(new Thing(params[name]);
}
}
Let me know if any of this is of help

How to pass JSON to controller action?

I have some textboxes and a table of data created client side that I want pass to a controller as JSON.
Can you please give me the syntax for this?
I want to use Jquery to enumerate the table.
Assume I have 2 textboxes called name and age.
Assume a table with 2 columns. one column with class
called phonetype and one column class called phonenumber.
So how do construct the JSON from this?
Thanks in advance.
Malcolm
Sending Json to a ActionResult with an object as the parameter and using the JsonValueProviderFactory do the deserialization.
Sending JSON to an ASP.NET MVC Action Method Argument
Looping through table rows:
var tableRows = $('.TableDemo tr');
tableRows.each(function () {
var tdText = $(this).find('#id_of_td_with_data').text();
});

Resources