Compare two dates in JSTL - jstl

I am comparing current date with a given date(from database in yyyy-MM-dd format) in jstl, but it is comparing only dd part not complete date dd-MM-yyyy. For example if current date is 29-06-2016 and I compare with 30-06-2018, it is comparing only 29 with 30. My code is:
<c:set var="now" value="<%=new java.util.Date()%>" />
<fmt:formatDate var="currDate" pattern="dd-MM-yyyy" value="${now}" />
<fmt:parseDate value="${mat_list.dt_expiry_date}" var="parsedExpDate" pattern="yyyy-MM-dd" />
<fmt:formatDate var="expiryDate" pattern="dd-MM-yyyy" value="${parsedExpDate}" />
<c:if test="${currDate > expiryDate}"><b><span style="background-color: red; color: white">${expiryDate}</span></b></c:if>
How to compare full date with current date.

Try as below. Use either scriptlet or JSTL.
<jsp:useBean id="now" class="java.util.Date" />
<fmt:setLocale value="en_US" />
<fmt:parseDate value="${mat_list.dt_expiry_date}" var="parsedExpDate" pattern="yyyy-MM-dd" />
<c:if test="${now.time gt parsedExpDate.time}">
<b><span style="background-color: red; color: white">${parsedExpDate}</span></b>
</c:if>

Related

Extraction Rule configuration in Visual Studio 2013 web performance tests

I've been following this guide to create my web performance tests in VS 2013 and I found interesting thing, which I am not sure how to understand.
I have a web application. On certain request this application returns me a page, where I have a span element which has style attribute, which is equal to "color:Blue;". In my performance test I go to that page and I have "Extract Attribute Value" extraction rule to get a value of style attribute of that span tag. When I configure my rule to get attribute style1 for that tag, the rule fails (which I expect to happen), but when I create a rule with match attribute value "c#l#r-BBBB", it doesn't fail (although, I expect it to fail).
Does anybody know why?
Here is the page source:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="Blue.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="QG+BA5tJt9bUUKK/SNJvCYaITvz71sZMdjWwNGygbhGjjs6Vy/29qy+kskbo3g4Vaz2Zfpi8hlr2F4g366EChHwtM2N676WWg0LBR3+9hc0=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="D66C0198" />
</div>
<div>
<span id="Label1" style="color:Blue;">Blue</span>
</div>
</form>
</body>
</html>
These is the extraction rule in .webtest, that I expect to fail:
<ExtractionRule Classname="Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractAttributeValue, Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" VariableName="ExtractionTest" DisplayName="Extract Attribute Value" Description="Extract the value of an attribute from a specified HTML tag.">
<RuleParameters>
<RuleParameter Name="TagName" Value="span" />
<RuleParameter Name="AttributeName" Value="style" />
<RuleParameter Name="MatchAttributeName" Value="" />
<RuleParameter Name="MatchAttributeValue" Value="c#l#r-BBBB" />
<RuleParameter Name="HtmlDecode" Value="True" />
<RuleParameter Name="Required" Value="True" />
<RuleParameter Name="Index" Value="0" />
</RuleParameters>
</ExtractionRule>
I believe you need to specify vales for both the MatchAttributeName and the MatchAttributeValue properties. The documentation for the ExtractAttributeValue rule is not clear about what combinations of its properties are supported. However, I interpret the rule as saying: look for a TagName tag TagName where the attribute MatchAttributeName has the value MatchAttributeValue and from that tag return the value in AttributeName. Using that interpretation has always worked for me.
The documentation for AttributeName says that it "is used to identify the attribute whose value you want to extract", it does not say it is used to match any values. This documentation goes on to say that MatchAttributeName and (my emphasis) MatchAttributeValue are used in some cases.

tagx files: c:set tag, access to a property of an object

I'm using Spring Roo, and I have changed the list.tagx file to add a title at the top. This title is the value of a property of the first item. The problem is that I want to specify this property in a generic way, as an attribute of the list.tagx. Something like that:
<jsp:directive.attribute name="titleValue" type="java.lang.String"
required="false" rtexprvalue="true"
description="The value to be shown in the title" />
...
<c:when test="${not empty items}">
<c:if test="${not empty titleValue}">
<c:set var="variable" value="${items[0].titleValue}" />
</c:if>
...
There is a problem, because it tries to get the value of a property called 'titleValue' into the object items[0]. For instance, if I set the value of 'titleValue' as 'firstName', I want to get the value of items[0].firstName
Is it possible to do that?
Thanks in advance...
it is possible by using the spring:eval tag from the namespace xmlns:spring="http://www.springframework.org/tags". Try this:
<jsp:directive.attribute name="titleValue" type="java.lang.String"
required="false" rtexprvalue="true"
description="The value to be shown in the title" />
...
<c:when test="${not empty items}">
<c:if test="${not empty titleValue}">
<c:set var="variable">
<spring:eval expression="items[0].${titleValue}" />
</c:set>
</c:if>
...
EDIT: fixed typo, sorry. It wasn't ${items[0]}.${..} but items[0].${..}

JSTL split Radio button into two lists

I am trying to split my Radio Button list into "n" number of columns based on number of Rows. This is what I was trying, here programs is my list which currently has 17 Radio Buttons. I am trying to divide (17/2) rows and 2 columns currently.
<c:set var="numRows" value="${fn:length(programs)/2-0.5}" />
<c:set var="totalRows" value="${fn:length(programs)}" />
<c:forEach begin="0" end="${numRows }" varStatus="i">
<c:out value="i=${i.index} " />
<c:set var="rowStart" value="${i.index * numColumns}" />
<fmt:formatNumber var="numColumns" value="${fn:length(programs) / numRows}"
maxFractionDigits="0" />
<c:forEach begin="0" end="${numColumns - 1}" varStatus="j" >
<c:set var="index" value="${rowStart + j.index}"/>
<c:out value="j=${j.index} " />
<c:choose>
<c:when test="${index lt fn:length(programs)}">
<c:forEach var="prgip" items="${programs}">
<c:forEach begin="0" end="${totalRows }" varStatus="k" var="prgip" items="${programs}">
<input type="radio" value="${prgip.program_id}" name="program"> ${prgip.program_name}
<c:out value="k=${k.count} " />
</c:forEach>
</c:forEach>
</c:when>
<c:otherwise> </c:otherwise>
</c:choose>
</c:forEach>
</c:forEach>
Thanks in Advance
-Mim Jones.

Spring Roo File Upload

Hy everyone!
Im trying to implement file upload with Spring roo. The files path will be persisted in the database, and the file will be saved on the file system.
According to informations found on the spring dveloper board, i modified the input.tagx and create.tagx files.(info: https://jira.springsource.org/browse/ROO-442)
input.tagx:
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:output omit-xml-declaration="yes" />
<jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)" />
<jsp:directive.attribute name="field" type="java.lang.String" required="true" rtexprvalue="true" description="The field exposed from the form backing object" />
<jsp:directive.attribute name="label" type="java.lang.String" required="false" rtexprvalue="true" description="The label used for this field, will default to a message bundle if not supplied" />
<jsp:directive.attribute name="labelCode" type="java.lang.String" required="false" rtexprvalue="true" description="Key for label message bundle if label is not supplied" />
<jsp:directive.attribute name="required" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicates if this field is required (default false)" />
<jsp:directive.attribute name="disabled" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Specify if this field should be enabled" />
<jsp:directive.attribute name="validationRegex" type="java.lang.String" required="false" rtexprvalue="true" description="Specify regular expression to be used for the validation of the input contents" />
<jsp:directive.attribute name="validationMessageCode" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the message (message property code) to be displayed if the regular expression validation fails" />
<jsp:directive.attribute name="validationMessage" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the message to be displayed if the regular expression validation fails" />
<c:if test="${empty render or render}"
<c:when test="${disableFormBinding}">
<input id="_${field}_id" name="${field}" type="${type}"/>
</c:when>
<c:otherwise>
<!-- currently (spring 3.0.3), form:input doesn't support type attribute -->
<!-- <form:input id="_${field}_id" path="${field}" disabled="${disabled}"/> -->
<input id="_${field}_id" name="${field}" type="${type}"/>
<br/>
<form:errors cssClass="errors" id="_${field}_error_id" path="${field}"/>
</c:otherwise>
<c:if test="${empty disabled}">
<c:set value="false" var="disabled" />
</c:if>
<c:if test="${empty label}">
<c:if test="${empty labelCode}">
<c:set var="labelCode" value="${fn:substringAfter(id,'_')}" />
</c:if>
<spring:message code="label_${fn:toLowerCase(labelCode)}" var="label" htmlEscape="false" />
</c:if>
<c:if test="${empty validationMessage}">
<c:choose>
<c:when test="${empty validationMessageCode}">
<spring:message arguments="${fn:escapeXml(label)}" code="field_invalid" var="field_invalid" htmlEscape="false" />
</c:when>
<c:otherwise>
<spring:message arguments="${fn:escapeXml(label)}" code="${validationMessageCode}" var="field_invalid" htmlEscape="false" />
</c:otherwise>
</c:choose>
</c:if>
<c:if test="${empty required}">
<c:set value="false" var="required" />
</c:if>
<c:set var="sec_field">
<spring:escapeBody javaScriptEscape="true" >${field}</spring:escapeBody>
</c:set>
<div id="_${fn:escapeXml(id)}_id">
<label for="_${sec_field}_id">
<c:out value="${fn:escapeXml(label)}" />
:
</label>
<c:choose>
<c:when test="${disableFormBinding}">
<input id="_${sec_field}_id" name="${sec_field}" type="${fn:escapeXml(type)}" />
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${type eq 'password'}">
<form:password id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" />
</c:when>
<c:otherwise>
<form:input id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" />
</c:otherwise>
</c:choose>
<br />
<form:errors cssClass="errors" id="_${sec_field}_error_id" path="${sec_field}" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${required}">
<spring:message code="field_required" var="field_required" htmlEscape="false" />
<spring:message argumentSeparator="," arguments="${label},(${field_required})" code="field_simple_validation" var="field_validation" htmlEscape="false" />
</c:when>
<c:otherwise>
<spring:message argumentSeparator="," arguments="${label}, " code="field_simple_validation" var="field_validation" htmlEscape="false" />
</c:otherwise>
</c:choose>
<c:set var="sec_field_validation">
<spring:escapeBody javaScriptEscape="true">${field_validation}</spring:escapeBody>
</c:set>
<c:set var="sec_field_invalid">
<spring:escapeBody javaScriptEscape="true" htmlEscape="true">${field_invalid}</spring:escapeBody>
</c:set>
<c:set var="sec_field_required">
<spring:escapeBody javaScriptEscape="true">${field_required}</spring:escapeBody>
</c:set>
<c:set var="sec_validation_regex" value="" />
<c:if test="${!empty validationRegex}">
<c:set var="sec_validation_regex" value="regExp : '${validationRegex}', " />
</c:if>
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({elementId : '_${sec_field}_id', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: '${sec_field_validation}', invalidMessage: '${sec_field_invalid}', required : ${required}, ${sec_validation_regex} missingMessage : '${sec_field_required}' }}));
</script>
</div>
<br />
</c:if>
</jsp:root>
create.tagx:
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:output omit-xml-declaration="yes"/>
<jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)"/>
<jsp:directive.attribute name="modelAttribute" type="java.lang.String" required="true" rtexprvalue="true" description="The name of the model attribute for form binding"/>
<jsp:directive.attribute name="path" type="java.lang.String" required="true" rtexprvalue="true" description="Specify the relative URL path (wit leading /)" />
<jsp:directive.attribute name="compositePkField" type="java.lang.String" required="false" rtexprvalue="true" description="The field name of the composite primary key (only used if a composite PK is present in the form backing object)" />
<jsp:directive.attribute name="multipart" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if this is a multipart form (default: false)" />
<jsp:directive.attribute name="label" type="java.lang.String" required="false" rtexprvalue="true" description="The label used for this object, will default to a message bundle if not supplied"/>
<jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" />
<jsp:directive.attribute name="openPane" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Control if the title pane is opened or closed by default (default: true)"/>
<jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)"/>
<jsp:directive.attribute name="enctype" type="java.lang.String" required="false" description="Used to set the enctype, e.g. multipart/form-data for file upload support. "/>
<c:if test="${empty render or render}">
<c:if test="${empty label}">
<spring:message code="label_${fn:toLowerCase(fn:substringAfter(id,'_'))}" var="label" htmlEscape="false" />
</c:if>
<!--<c:set var="enctype" value="application/x-www-form-urlencoded"/> -->
<form:form action="${form_url}" method="POST" modelAttribute="${modelAttribute}" enctype="${enctype}">
</form:form>
<c:if test="${multipart}">
<c:set var="enctype" value="multipart/form-data"/>
</c:if>
<spring:message arguments="${label}" code="entity_create" var="title_msg" htmlEscape="false" />
<util:panel id="${id}" title="${title_msg}" openPane="${openPane}">
<spring:url value="${path}" var="form_url"/>
<c:set var="jsCall" value=""/>
<c:if test="${not empty compositePkField}">
<c:set var="jsCall" value="encodePk()" />
</c:if>
<form:form action="${form_url}" method="POST" modelAttribute="${modelAttribute}" enctype="${enctype}" onsubmit="${jsCall}">
<form:errors cssClass="errors" delimiter="<p/>"/>
<c:if test="${not empty compositePkField}">
<form:hidden id="_${fn:escapeXml(compositePkField)}_id" path="${fn:escapeXml(compositePkField)}" />
<script type="text/javascript">
<![CDATA[
dojo.require("dojox.encoding.base64");
function encodePk() {
var obj = new Object();
dojo.query("input[name^=\"${compositePkField}.\"]").forEach(function(node, index, nodelist){
obj[node.name.substring('${compositePkField}'.length + 1)] = node.value;
});
var json = dojo.toJson(obj);
var tokArr = [];
for (var i = 0; i < json.length; i++) {
tokArr.push(json.charCodeAt(i));
}
var encoded = dojox.encoding.base64.encode(tokArr);
dojo.byId('_${fn:escapeXml(compositePkField)}_id').value = encoded;
}
]]>
</script>
</c:if>
<jsp:doBody />
<div class="submit" id="${fn:escapeXml(id)}_submit">
<spring:message code="button_save" var="save_button" htmlEscape="false" />
<script type="text/javascript">Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));</script>
<input id="proceed" type="submit" value="${fn:escapeXml(save_button)}"/>
</div>
</form:form>
</util:panel>
</c:if>
</jsp:root>
My problem is i get the fallowing error:
javax.servlet.jsp.JspTagException: Illegal use of <when>-style tag without <choose> as its direct parent
at org.apache.taglibs.standard.tag.common.core.WhenTagSupport.doStartTag(WhenTagSupport.java:95)
at org.apache.jsp.tag.web.form.fields.input_tagx._jspx_meth_c_005fwhen_005f0(input_tagx.java:519)
at org.apache.jsp.tag.web.form.fields.input_tagx.doTag(input_tagx.java:325)
at org.apache.jsp.WEB_002dINF.views.pphotoes.create_jspx._jspx_meth_field_005finput_005f0(create_jspx.java:172)
at org.apache.jsp.WEB_002dINF.views.pphotoes.create_jspx.access$2(create_jspx.java:157)
What am i doing wrong? (My controller and entity files are good i think, if needed i can post them)
Thx for any help.
cheers.
As the error message clearly states, you need to have a <c:choose> as a direct parent tag before the <c:when> tag - possibly in the following first few lines of your input.tagx file. Additionally, the <c:if> tag is not closed properly.
<c:if test="${empty render or render}"
<c:when test="${disableFormBinding}">
Cheers.

How to show List as table in spring mvc form

public class Parent {
private Integer Id;
private String name;
private String age;
private List<Childrens> childrens;
// getters and setters
}
My JSP classs:
<spring:bind path="parent.*">
<c:if test="${not empty status.errorMessages}">
<div class="error">
<c:forEach var="error" items="${status.errorMessages}">
<img src="<c:url value="/images/iconWarning.gif"/>"
alt="<fmt:message key="icon.warning"/>" class="icon" />
<c:out value="${error}" escapeXml="false" />
<br />
</c:forEach>
</div>
</c:if>
<form:form commandName="parent" method="post"
action="parent .html" onsubmit="return onFormSubmit(this)"
id="parentForm">
<appfuse:label styleClass="desc" key="name" />
<form:errors path="name" cssClass="fieldError" />
<form:input path="name" id="name" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
<appfuse:label styleClass="desc" key="age" />
<form:errors path="age" cssClass="fieldError" />
<form:input path="age" id="cage" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
I want show list of Childrens as a table. How can I do it? A list inside form page.
Probably your best bet is to use the built-in jstl tag lib (which you already) to declare the bean and get the list of children and iterate over them with "forEach".
Using spring form tag doesn't prevent you from doing this as far as I know.

Resources