There is something strange happening.
Param interceptor is the one in charge to take parameters strings values from the request and put them in the java property of my action.
Some defaults type converters exist (primary types, Date, Map, Array)
And I tried to put a string parameter in a BigDecimal. Strangely... it works but how ? I am pretty sure that the BigDecimal converters does not exist and has to be implemented by the framework user
If i am correct it is using the default BigDecimal parsing.have a look at DefaultTypeConverter
Related
I was reading DWARFv4 spec and came across attribute DW_AT_endianity.
Correct me if my understanding is wrong.
As per spec, Two of the tag values TAG_variable and TAG_base_type both can have that attribute.
But as far as, i understand when you create a TAG_variable you have to pass the "Type", which can be of TAG_base_type(others are also possible).
So my question is if we are setting DW_AT_endianity on TAG_variable, why TAG_base_type also supports that attribute.
In other words every variable is of some type and can have that attribute, so why need attribute support for both type and variable?
The DW_AT_endianity attribute was the response to a proposed C compiler extension allowing the "creation of data in both big and little endian formats". It appears that the attribute was intended for base types (like this example) or structs/unions (like this one). Using the same attribute for both a simple variable and its underlying base type would, as you say, be redundant — note that the attribute is optional (see DWARF 4, sections 4.1.12 and 5.1).
We're creating some custom fields by adding new definition fields to category and product definition items in uCommerce.
When we retrieve an instance of the ctegory or prouduct from the uCommerce.Entitiesv2 we're having trouble getting the culture specific value for these fields when multilingual is selected?
There is a collection on the Product object called ProductDefinitionField but not sure whether .Value returns the culture specific version of whether we need to call another method (extention method maybe)
Has anyone got a code snipper for this?
When accessing or retrieving Multilingual properties on a uCommerce you can use the GetProperty method on a product.
It has two overloads, one taking name (string) and another one taking name (string) and culturecode (string).
If you want to retrieve the full collection of multilinqual properties you can use GetProperties which also have two overloads. One without parameters and the other with a string culturecode.
Depending on the version of uCommerce you're using some of them might be missing/not a part of the API.
Best regards Martin
Is there any recommended/standard way to implement such MVC-code that a validation of form fields that are stored as nullable integers (int?) would work?
One of the root problems I'm having with this now, is that if I input something that is not integer like "abc123" and try to submit the form, the MVC tech stack will automatically convert this erroneous integer value to empty string before any validators are processed. Because empty is totally acceptable value in my case, validators will pass, form saving looks like proceeding correctly to the end and everything looks good until the user opens the updated form again and sees only empty field instead of the integer he or she inputted.
At first, I tried multiple dataAnnotation-validators to solve this problem, but none of them worked and the reason seems to be the fact that the erronoeus integer will be converted to empty string before the value is passed to validators.
[Integer]
[RegularExpression("[0-9]{1,}")]
public int? MyTroublesomeIntField { get; set; }
After this I tried to solve the problem by creating custom model binder by implementing IModelBinder. After experimenting with custom model binders, it seems that the erroneous value gets converted to empty string even before the custom model binder starts to run. Here's couple of code snippets from my modelBinder:
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
//f.e. this returns empty string
value.AttemptedValue
//f.e. this also returns empty strings, even though name "raw" leads one to think that this is the value no one has touched.
value.RawValue
//even this returned empty string even though one could think that by accessing the field from HttpRequest would give access to the truly original value inputted by the user, but no, mvc stack gets to tinker this one as well.
string valueBefore = HttpContext.Current.Request.Form[bindingContext.ModelName];
So it seems to me, that there is some part of MVC tech stack that makes this conversion (erroneous integer input to empty string value) even before the custom model binders start to work. I'm just wondering, where is this happening, and is there a chance to customize it to get access to the original value?
Other alternative I have been thinking by now would be to create a separate string field for each of the free input int? form fields I have in the system. For string fields even those simple dataAnnotations like RegularExpressions would work straight away and the main problem would be solved. Finally I would just need to add binding data from all these "dummy"-string values to real int?-values somewhere in the controller/business logic. But this just seems like a massive work as I'm having loads of these ints in the systm. It feels strange that there would be this much extra work to do in order to get int?s working, when at the same time, exactly similar apporach works pefectly with double? fields which is currently not working with int?s - if I input erroneous double containing f.e. letters, client side validation will catch the error.
Any suggestions what would be the best way to proceed here?
My tech stack is pretty much like this: Razor, MVC5, Entity framework,
In MVC if you want to create an editor for a property or a display for you property you do something like that:
#Html.EditorFor(m=> m.MyModelsProperty);
#Html.DisplayFor(m=> m.MyModlesProperty);
Why do we have to pass a delegate why can't we just pass the model's property directlly? e.g.:
#html.EditorFor(Model.MyModlesProperty);
The reason for this is because of Metadata. You know, all the attributes you could put on your model, like [Required], [DisplayName], [DisplayFormat], ... All those attributes are extracted from the lambda expression. If you just passed a value then the helper wouldn't have been able to extract any metadata from it. It's just a dummy value.
The lambda expression allows to analyze the property on your model and read the metadata from it. Then the helper gets intelligent and based on the properties you have specified will act differently.
So by using a lambda expression the helper is able to do a lot more things than just displaying some value. It is able to format this value, it is able to validate this value, ...
I'd like to add, that besides the Metadata and making the Html helper strongly typed to the Model type, there's another reason:
Expressions allow you to know the name of the property without you hard coding strings into your project. If you check the HTML that's produced by MVC, you'll see that your input fields are named "ModelType_PropertyName", which then allows the Model Binder to create complex types that are passed to your Controller Actions like such:
public ActionResult Foo(MyModel model) { ... }
Another reason would be Linq to SQL. Expression Trees are the magic necessary to convert your Lambdas to SQL queries. So if you were to do something like:
Html.DisplayFor(p => p.Addresses.Where(j => j.Country == "USA"))
and your DbContext is still open, it would execute the query.
UPDATE
Stroked out a mistake. You learn something new every day.
The first example provides a strongly-typed parameter. It forces you to choose a property from the model. Where the second is more loosely-typed, you could put anything in it, even something that isn't valid property of the model.
Edit:
Surprisingly, I couldn't find a good example/definition of strong vs loose typing, so I'll just give a short example regarding this.
If the signature was #html.EditorFor(string propertyName); then I could make a typo when typing in the name and it would not be caught until run-time. Even worse, if the properties on the model changed, it would NOT throw a compiler error and would again not be detected until run-time. Which may waste a lot of time debugging the issue.
On the other hand with a lambada, if the model's properties changed you would get a compiler error and you would have to fix it if you wanted to compile your program. Compile-time checking is always preferred over run-time checking. This removes the chance of human error or oversight.
Say there is a value in valuestack of struts 2; when we code the jsp, we don't know what the exact variable name of this value, but we only know that the variable name of this value is saved in another variable name, say "XXX".
The question is how can get the value by using "XXX", I try this, but it is not working.
<s:property value="${XXX}"/>
The action marshals data for the view, as such it should do the processing to get the required data. From the sounds of it, it sounds like the action could gather the appropriate data into a map.
However there are strange cases and you might have one. But before addressing that if you only have the name of the variable where can it be assumed the real variable is? Is it in the value stack (and if so what is stopping you from accessing it directly)? If it is not on the value stack you'll need to enable static method assess and create an appropriate static method, since you are only provided with the name of the variable and assuming it is a property of a java bean you'll then need to use reflection or apache beanutils.
In general it is better to get what you need in the action for your views.
Also to set a value in your jsp's you are aware of the struts2 set tag (this is probably not what you want but there was a small chance it was so I included it)? See: http://struts.apache.org/2.2.3.1/docs/set.html