Struts2 client side validation without using action class - validation

Need to do client side validation in struts2 only using validation.xml but my beans are in bean class. validation.xml only supports action class based validation.
i need client side validation without using javascript or any validate method.

Struts Validator Framework provides an easy-to-use mechanism for performing client-side validation.
For each validation routine defined in the validation-rules.xml file Struts provides an optional JavaScript code that can run on the client-side to perform the same validation that takes place on the server side.
LoginForm extends DynaValidatorForm
<form-bean name="LoginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="userName" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
</form-bean>
following validations are defined in the validation.xml file.
<form name="LoginForm">
<field property="userName" depends="required">
<arg key="LoginForm.userName"/>
</field>
<field property="password" depends="required,minlength">
<arg0 key="LoginForm.password"/>
<arg1 key="${var:minlength}" name="minlength" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
</field>
</form>
To enable client-side validation you have to place the Struts HTML Tag Library's javascript tag in each jsp page for which you need to preform client-side validation.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JS Validation</title>
</head>
<body>
<html:form action="/Login" onsubmit="validateLoginForm(this);">>
<html:javascript formName="LoginForm" />
User Name : <html:text name="LoginForm" property="userName" />
Password : <html:password name="LoginForm" property="password" />
<html:submit value="Login" />
</html:form>
</body>
</html>

Related

Submit in Spring form using DWR library

In one of J2EE project, we are using DWR libary. Being familiar with simple ajax calls and little DWR, I am not able to figure out which service call is used on our backend on clicking the submit button in one of our spring forms.
KRIDeclaration.jsp file:
Spring Form tag :
<form:form commandName="KRIDeclarationCommand" name="krideclaration" method="post" enctype="multipart/form-data">
........
Submit button: <td height="20" colspan="4" align="right" class="dvtCellLabel">
<div align="center"><span style="padding-top: 5px;"> <input
class="btnstyle" type="button" name="btnSendForApproval"
value="Submit" onClick="declareIncident();" />
KRIDecleration.js
declareIncident()
{
Basic front end Validation
Backend Validation using DWR generated js file with a callback
}
callbackmethod(Result)
{
if (success)
document.krideclaration.submit();
else
//code for displaying errors.
}
dwr-context.xml:
dwr:annotation-scan base-package="com.abc.riskdashboard.common" scanDataTransferObject="true" scanRemoteProxy="true" />
<!-- DWR will map util.js and engine.js files to the dwrController.
You can then include this files as external Javascript references from your JSP -->
<dwr:url-mapping />
<!-- Defines the dwrController. During production, set the debug property to false -->
<dwr:controller id="dwrController" debug="true" />
<!-- This is required if you want to configure any beans not managed by
Spring. Leaving it enabled doesn't do any negative effects.
-->
<dwr:configuration>
<dwr:convert type="bean" class="com.abc.riskdashboard.common.dto.RiskAreaMasterDTO" />
<dwr:convert type="bean" class="com.abc.riskdashboard.common.dto.RiskIndicatorDTO" />
<dwr:convert type="bean" class="com.abc.riskdashboard.common.dto.RiskDeclarationDTO" />
<dwr:convert type="bean" class="com.abc.riskdashboard.common.dto.UserMasterDTO" />
<dwr:convert type="bean" class="com.abc.riskdashboard.common.dto.RoleMasterDTO" />
Need to find out what document.krideclaration.submit() in KRIDecleration.js calls when success. There is not submit function or action attribute in spring form

Allowing only digits in aui:form, with Liferay portlet-model-hints.xml

My portlet-model-hints.xml below stipulates that quantity is required, that works fine.
Now I also want to stipulate that quantity must be made of digits:
<model-hints>
<model name="com.example.model.MyEntity">
[...]
<field name="order" type="long">
<validator name="required" />
<validator name="digits" /> <----- Does not work
</field>
[...]
</model>
</model-hints>
PROBLEM: Adding <validator name="digits" /> makes the text field disappear.
Is there a problem in my syntax? Should I do the validation in the JSP instead? By the way here is the JSP form to add/edit my entity:
<aui:form action="<%= editMyEntityURL %>" method="POST" name="fm">
<aui:fieldset>
[...]
<aui:input name="quantity" />
[...]
</aui:fieldset>
[....]
</aui:form>
[Workaround, I am still looking for a better solution]
Not elegant at all, but moving digits validation to the JSP works:
<aui:form action="<%= editMyEntityURL %>" method="POST" name="fm">
<aui:fieldset>
[...]
<aui:input name="quantity">
<aui:validator name="digits"/>
</aui:input>
[...]
</aui:fieldset>
[....]
</aui:form>
It must be done in all JSP forms that use the entity.

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.

How to pass values from JSP to tiles attribute?

I am converting an existing Tiles 1 webapp to Tiles 2 architecture. I am having trouble passing values from JSP page to tiles attributes.
Here is my tiles definition file (tiles-definition.xml)
<tiles-definitions>
<definition name="cda.layout" template="/jsp/layouts/layout.jsp">
<put-attribute name="pageTitle" value="StoryTitle" type="string"/>
<put-attribute name="pageHeader" value="StoryHeader" type="string"/>
<put-attribute name="resources" value="" type="string"/>
</definition>
</tiles-definitions>
The layout.jsp looks like:
<html>
<head>
<title><tiles:insertAttribute name="pageTitle" flush="true"/></title>
</head>
<body>
...
...
<div class="content">
<h1><tiles:insertAttribute name="pageHeader" flush="true"/></h1>
</div>
...
...
</body>
</html>
I have a story page which uses the layout and need to pass values to template attributes.
<%
// create a business object and populate
String mytitle= story.getTitle();
String myheader = story.getHeader();
%>
<tiles:insertTemplate template="../layouts/layout.jsp" flush="false" >
<tiles:putAttribute name="pageTitle" value="${mytitle}"/>
<tiles:putAttribute name="pageHeader"value="${myheader}"/>
</tiles:insertTemplate>
In the story.jsp, I can System.out.print() the values for mytitle, myheader and they are showing correct. But, these values are NOT passed on to the tile attributes.
Any idea how to fix this?
${mytitle} is a JSP EL expression which means: find an attribute in page scope, or request scope, or session scope, or application scope, named "mytitle".
By defining a scriptlet variable, you haven't defined an attribute in any of these scopes. It would work if you had
pageContext.setAttribute("mytitle", mytitle);
But using scriptlets in JSPs is bad practice. I don't know where your story bean comes from, but it's probably a request attribute. If so, you can define a new page-scope attribute this way, using the JSTL:
<c:set var="mytitle" value="${story.title}"/>
This is unnecessary though, since you could use this expression directly in the tiles tag:
<tiles:putAttribute name="pageTitle" value="${story.title}"/>
Read more about the JSP EL in this tutorial.

Best way to set HTML head title in a Spring+Tiles2 application?

I have a usability problem in my Spring webapp which uses Tiles as the view technology.
At the moment all of the pages display the same HEAD_TITLE and the PAGE_TITLE is page specific:
<html>
<head><title>HEAD_TITLE</title></head>
<body>
<h1>PAGE_TITLE</h1>
</body>
</html>
This is a major usability problem as the browsers history lists all different pages of the application with the same title. The reason why the HEAD_TITLE is same for all pages is that I haven't found a reasonable way to use the PAGE_TITLE as the HEAD_TITLE.
In most cases the PAGE_TITLE comes from a message bundle with <fmt:message /> tag and
some parameters are passed to it. The Tiles layout is such that the HEAD_TITLE should be already set at that point because all pages of the webapp use the same common layout which defines the <HEAD> elements of the pages amongst other stuff.
Any suggestions how to fix this usability problem? Should I set a "pageTitle" request attribute in my Spring controllers for all pages and use that as the PAGE_TITLE and also as the HEAD_TITLE? Or is it possible to somehow set the HEAD_TITLE in the page specific JSP?
Create a general definition and define headTitle and pageTitle attributes.
<definition name="threeColumnLayout" template="/WEB-INF/ThreeColumnLayout.jsp" >
<put-attribute name="headTitle" value="" />
<put-attribute name="pageTitle" value="" />
<put-attribute name="left" value="/WEB-INF/left.jsp" />
<put-attribute name="middle" value="" />
<put-attribute name="right" value="/WEB-INF/right.jsp" />
</definition>
Set appropriate values in more specific definition.
<definition name="/user/new" extends="threeColumnLayout">
<put-attribute name="headTitle" value="Administration" />
<put-attribute name="pageTitle" value="Create User" />
<put-attribute name="middle" value="WEB-INF/views/UserCreate.jsp" />
</definition>
Use <tiles:getAsString /> tag to retrieve such values in jsp page.
<head>
<title><tiles:getAsString name="headTitle"/></title>
</head>
<body>
<h1>
<title><tiles:getAsString name="pageTitle"/></title>
</h1>
</body>
Reference:- http://tiles.apache.org/framework/tiles-jsp/tagreference.html#tiles:getAsString

Resources