Struts 2 - Date instance become String - forms

When I submit the form, input error is occur. JourneyDate is instance of 'Date'. But ,here it become String which is not accepted by the setter and getter.
<s:hidden name="JourneyDate" value="%{JourneyDate}"></s:hidden>
I want JourneyPlan as Date Type, but it become String.

Try intercepting the value before passing it to the getter/setter. For example send JourneyDateString from your form, create a Date from the String, and then pass that to your getter/setter. Something like:
public void setJourneyDateString(String journeyDateString)
{
//journeyDateString could be "2013-03-28" for example
Date journeyDate = new SimpleDateFormat("yyyy-MM-dd").parse(journeyDateString);
setJourneyDate(journeyDate);
}

The object that you've set in the value attribute will keep it's type as Date. Then you need to define corresponding setter in the action to set the value of the Date. It will convert to string if you place the value in the body of the tag.

Related

Why does casting this string in this way work but not the other way?

I am reading data from Firestore. I was getting an error that said type 'Timestamp' is not a subtype of type 'String' in type cast, so I changed the createDate line below to use .toString() instead of doing as String. This solved the problem, but why did this work?
Notification.fromJson(Map<dynamic, dynamic>? json): //Transform JSON into Notification
createDate = (json?['createDate']).toString(), //This works
modifiedDate = json?['modifiedDate'] as String; //This gives the error: 'type 'Timestamp' is not a subtype of type 'String' in type cast'
The format of both of these fields is October 5, 2022 at 10:49 PM UTC-5.
Like it is said in error message, you are getting this error, because the value's type, which is coming from json is Timestamp, not String. It is better to make createDate and modifiedDate fields in Timestamp type instead of String, because it provides it's own methods, that make your work easier. Converting to String (and probably, you are parsing this String in somewhere) is redundant.
In Flutter every object class have toString() method, which is return a value with String type
(json?['createDate']).toString();
So if you write that code above, it will return a value with String type and then assign that value to createDate variable. It means you not assign json?['createDate'].
modifiedDate = json?['modifiedDate'] as String;
That line above will error because you assign that json to modifiedDate which is have different type. Cast (as) only work if the object/variable have same hierarchy, like
Child child = Child();
Parent parent = child as Parent();
json?['createDate'] is a Timestamp type object that just contains numbers describing a point in time. As you can see from the API documentation, it has a method called toString that knows how to convert that to formatted date string. Since a Timestamp it is not a subclass of String, Dart does not allow it to be cast to one. Perhaps you want to cast it to Timestamp instead, since that's what it is?

One way data binding not working when using date pipe

I'm using a date object to keep track of the current date in an application.
In my view I have a one way binding like this:
<h3>{{ currentDate | date }}</h3>
And in the component, I have functions to change this date, like this:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
}
But when these functions are triggered, the currentDate value doesn't update on the view.
I made sure the date object is being updated, just not on the view.
Whenever I remove the date pipe, it works.
Anyone has any idea how to fix this?
Thanks!
The value is not updating in the view because the pipes in angular are so called pure (or, stateless) by default. That means that the input will not be re-evaluated if the input object changes, but only if it's replaced.
From the documentation (see section Pure and Impure pipes):
Angular executes a pure pipe only when it detects a pure change to the
input value. A pure change is either a change to a primitive input
value (String, Number, Boolean, Symbol) or a changed object reference
(Date, Array, Function, Object).
Try the following code instead:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
this.currentDate = new Date(this.currentDate);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
this.currentDate = new Date(this.currentDate);
}

How to edit date value with date type format in bootstrap

I have these codes but then the value display in the edit box is "mm/dd/yyyy"
#Html.TextBoxFor(m => m.StartDate, new { #Value = Model.StartDate.ToString("MM/dd/yyyy"), #placeholder= Model.StartDate.ToString("MM/dd/yyyy"), #class = "form-control", #type="date" })
How can I achieve something like this where the displayed date is the value from the database and not "mm/dd/yyyy"
First, don't set the value attribute directly. Razor will pretty much ignore this anyways. The value for a bound field comes from ModelState, which is itself composed of values from Request, ViewBag/ViewData, and Model, in that order. So, for example, if you want StartDate to default to "today", then you would simply populate your model with that in the action before you return the view:
model.StartDate = DateTime.Today;
Or, better, you can change the property on your model class to default to today automatically:
private DateTime? startDate;
public DateTime StartDate
{
get { return startDate ?? DateTime.Today; }
set { startDate = value; }
}
Just bear in mind that if your action happens to take a param like startDate or you set something like ViewBag.StartDate, those values will always take precedence.
Second, you're utilizing an HTML5 date input type. In browsers that support the HTML5 input types, the supplied value for a datetime, date or time, must be in ISO format. For a date, that means YYYY-MM-DD. If the value is not supplied like that, then the browser considers it garbage and discards it, leaving the control as if no value was supplied.
Also, FWIW, you don't need to prefix every member of your anonymous object with #. It doesn't technically hurt anything, but it's code smell. The # prefix exists to escape language keywords. With class, for example, you can't use it directly since it's a keyword, so you have to use #class instead. However, things like placeholder and type are not keywords, and therefore don't need an # prefix.

How to fetch value from custom multifield component?

I have created a multifield custom widget having two fields with names ./urlLink and ./urlText.
Now i m trying to fetch the values from widget into the component's jsp with following code
String property = properties.get("./urlLink",String[].class);
for(String value: property ) {
out.print(value);
}
out.print(property);
But i am not able to get its value instead i m getting error.
If you're getting a property and it contains a string value, you need to use the method getString() - that way when you have the property, you can set the string to the value by doing something like this:
Property property = properties.get("./urlLink",String.class);
String value = property.getString();
Just a side note, if your return is supposed to be a string array, your type that you're putting the values in should be a string array.
String[] value
Check out the documentation on day.com for Properties and getting the values inside them.
Looks like a typo: you don't prefix a property name with .\ when accessing it.
My guess is you got a NullPointerException, right? That's because there's no ./urlLink property in the value map (properties). You should check against that anyway (so that it's not thrown on a fresh page with no content).
If that doesn't help -- double check that you have the properties in the content (call your page with .xml or .infinite.json extensions, and then double check if you can read them as plain strings (you should be able to -- CRX does some magic, smart type conversions).
It's good to register custom xtype as :
// registering the custom widget with the name dualfield
CQ.Ext.reg("dualfield", CQ.Ext.form.DualField);
Then u can easily fetch the value as :
String[] data = properties.get("multi",String[].class);
Here multi is the name of widget having multifield as xtype

How to send list of string values as "LIST" object in Dozer (custom-converter-param)?

Currently the list is passed in the custom-converter-param as "aa,bb,cc,dd" etc., but the parameter is passed as string and we need to again split the string with comma and finally save it as a list for processing.
Is there is a way to pass the list of string object as "List" as a parameter?
Thanks,
Kathir
In every custom converter the value of the property custom-converter-param is passed to the field parameter of type String in org.dozer.DozerConverter<A, B> class, and its only getter method contract is like this:
public String getParameter()
So No, is not possible to retrieve the value as a List. The simplest/best thing to do here is to create a method getParameterAsList in your own converter to split the String value.