jQuery plugin vs Dojo plugin using ModelDriven in Struts 2 - java

I am using Struts2 with ModelDriven for form submitting.
When I use datetimepicker tag of Dojo and select date from that tag the form is submitted correctly with the form values populated in the ModelDriven object.
But when datepicker tag of jQuery is taken in the form and date is selected the object in ModelDriven have null values.
If date is not selected and form is submitted then it work fine.
Is there any setting required that I missed to resolve the issue ?
I had included following jar for JQuery.
JQuery:
<sx:datepicker name="startDate" id="startDate" label="" cssStyle="width:275px; "
minDate="0" displayFormat="dd/mm/y" changeMonth="true" changeYear="true"
readonly="true" value="%{startDate}"/>

remove readonly="true" and everything will work fine.
readonly attribute default to false tells that whether the input is read-only.

I'm pretty sure that
name="startDate"
should be changed to
name="yourmodel.startDate"
Show more code for a better help...

Struts expects a value to be sent to the server as a locale-independent value, conforming to RFC3 339 (yyyy-MM-dd'T'HH:mm:ss) this is what the default dojo tag (now deprecated) provides: http://struts.apache.org/2.0.12/docs/datetimepicker.html
You can just send: 2013-01-03 the rest is optional.
The jQuery Datepicker I uses mm/dd/yy. To change this format to what struts2 expects either change the defaultFormat used for presentation or set altFormat which changes what was input into the format presented during submission (So if you like "mm/dd/yy" you can just set altFormat to "yyyy-mm-dd" and get the desired effect).
http://jqueryui.com/datepicker/#date-formats
http://api.jqueryui.com/datepicker/
As you can see jQuery easily provides for this, how to do it with the tag... I don't know. Someone else is welcome to take this information and extend it in another answer, with a tag specific technique if possible.

Problem was Dojo takes the date value on JSP as Date in java whereas JQuery takes it as string.
And so struts was finding for setDate(String) and not setDate(Date) for JQuery.

Related

JSP - Date formatting and Google cache

I want to display a formatted date on my JSP page, so I use:
<fmt:formatDate pattern="MMM d" value="${myEvent.date}"/>
It works perfectly. It is displayed on the page as
Nov 28
However, a strange thing happens when it is cached by Google - the date on the cached page is displayed like this:
2016-11-28 20:00:00.0
Can anyone explain this? Shouldn't the formatting happen on the server? Doesn't my application server (Tomcat) send only the HTML to the page? How does Google know that this was in fact a Java date in the first place?
Just to clarify, to get to the cached version of the page, I perform a search on Google that displays my page in the results and I click on the down arrow and get to the cached version of my page.
Apparently, when a JSP is requested without the "Accept-Language" HTTP header, the JSTL formatDate tag does not format the date and the result of toString() of the Date object is returned instead.
You can verify this by using cUrl to get the page source (instead of viewing the page source in Chrome).
Bottom line: If you you are using the JSTL formatDate tag and you want your dates to be formatted in Google's cache, you should explicitly set the locale. For example:
<fmt:setLocale value="en_US" />
I hope this helps someone.

How to pass dynamic parameter value in a link using Struts taglib?

I'm trying to use Struts framework in my project. I want to use the html:button to send parameter in the link but I don't understand how to make it.
In other words, I want to translate this line:
<input type="button" onClick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"/>
to Struts taglib, something like this:
<html:button property="" onclick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"></html:button>
But it didn't work.
The html:button tag is used only inside the form tag. See the docs
This tag is only valid when nested inside a form tag body.
Also set the property attribute.

struts2 jquery from date and to date

I am working a webproject using struts2 and I used struts2 jquery plugin for datepicker.
Now I need to validate ToDate depending on FromDate.
ToDate shouldnot be greater than FromDate
validation should be like this, datepicker should disable the behind dates accodring to fromdate.Then user not able to select lower than the from date
Please let me know how can I implemt this.
Please post the code will help
Thanks in advance
That's your choice how you want to impliment the validation.There are 2 ways which as of now seems feasible to me
Client side validation using JavaScript.
Server Side validation in your action class.
For the first part your both data-picker must have id and name so once user filled ToDate and FromDate and before submitting the form you can call a java-script function where you can fetch the values of respective date-picker fields using java-script document.getEmelemntById() and can run your comparison logic.
Other option is to let Struts2 fill the ToDate and FromDate values in your action class and you can use validate method inside your Action to perform date validation.choice is all yours.
You can even play around with validation framework even.
Update
After playing around with J query plugin source code here is the workaround we have
<s:form id="form" theme="xhtml">
<sj:datepicker id="date12" name="date12" label="With Close Event" onCompleteTopics="onDpClose"/>
<sj:datepicker id="date13" name="date13" label="With Min and Max Date" minDate="0" maxDate="+2m"/>
</s:form>
<script type="text/javascript">
$.subscribe('onDpClose', function(event,data) {
$('#date13').datepicker( "option" , 'minDate',event.originalEvent.dateText );
});
</script>
In similar way can work for other way around.Hope this will work for you.
There is an example similar to what you ask for on datepicker demos site.
I've modified and tried to simplify that example and created a fiddle for it which you can find here: http://jsfiddle.net/melih/vRULq/

Having JSF spit out an HTML Search field. Doable?

I'm not a Java developer, but work with a team that is using JSF 1.2
We'd like to start using HTML 5 tags and attributes. It does not appear that JSF 1.2 supports those by default.
Is there anyway to have a JSF text tag:
<x:inputText>
spit out an html 5 search tag:
<input type="search" placeholder="blahblah" />
Right now, I'm having to let it output a regular text field and then I place inline JS after it to trigger a function that converts it client side:
<input type="text">
<script> funciton here that changes type to 'search' and adds placeholder attribute</script>
It works, but is a bit hacky. Is there a legitimate way to get server-side JSF to output proper HTML 5 tags?
Create a custom component. This allows you fine grained control over rendered HTML.
Or upgrade to JSF 2.0, then you can create a composite component which is a lot easier.

Does form with enctype="multipart/form-data" cause problems accessing a hidden field

I have created a hidden form element
<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage">
<label>
</label>
<input name="imgUploadObjId" id="imgUploadObjId" value="52" type="hidden">
//rest of the form here
</form>
And I am trying to get the value with this line in a servlet (as I have done before):
int objId = Integer.parseInt(request.getParameter("imgUploadObjId"));
But I get this (line 33 is the line above):
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
web.objects.UploadImage.doPost(UploadImage.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Is there something different about a form with enctype="multipart/form-data"? Or can you see some other error.
The servlet parses the parameters by default using application/x-www-form-urlencoded encoding. The multipart/form-data encoding however isn't supported in servlets until Servlet 3.0. The getParameter() calls will all return null.
In Servlet 3.0, you should have used HttpServletRequest#getParts() instead to get all parts of a multipart/form-data request, including normal form fields. Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parse multipart/form-data requests. See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet?
Note that if you aren't using any <input type="file"> field at all, then you can just leave the encoding away from the <form>. It will then default to application/x-www-form-urlencoded.
As a workaround, you can also add the required hidden parameters as GET parameters in the form's action attribute:
<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage?imgUploadObjId=52">
//rest of the form here
</form>
this will allow the request.getParameter("imgUploadObjId") call to work.
Indeed there is something different.
request.getParameter will only work for hardcoded URL parameters specified in action attribute of <form> element. In your case it does not contain any.
All other parameters will be incoded into the form itself, which you have to process by parsing HTTP request's input stream directly.
Fortunately, you are not the first and there are some good open-source libraries that take care of this.
I've been using Apache FileUpload. You create a parser and pass a request object to it and then iterate through different items. One of them will be your hidden field.
The multi-part encoding shouldn't affect hidden text fields. It is likely something else. Can you post more of the HTML/Servlet code?
Not sure if this helps, but I have used multipart forms in jsp pages which are submitted to a struts servlet and these pages have hidden fields which are received in my Struts Action classes (wrapped in Struts ActionForm), so I don't think there is any hard stop here.
Have you tried receiving this value as String and seeing what actually comes there?
You'd check the servlet code itself. Are you getting the request? Can you debug the app in order to see which variables are present in the environment when you try to get the value and parse it.
I only had the id atttribute set for the field and it didn't show up in the List items list. When I added the name attribute, it showed up.

Categories

Resources