how to validate dynamic map elements - java

i have a map and i want to validate in struts 2 validation framework by using expression validation how can i access the elements of the map dynamically?
if Map myMap; how can i validate the map with dynamic key? if mymap has static key like "Salary", i could validate like
<field
name="myMap['Salary']">
<field-validator
type="regex">
<param
name="expression">[0-9]+[.][0-9]+</param>
<message>${getText("errors.validation.number")}</message>
</field-validator>
</field>
thanks,
Helen

There are two ways you can use the myApp in declarative validation.
By using myMap.salary
By using myMap['salary']
You will need to use one of the above notations which is based on how you have your input form fields defined.
For example, if your input form looks something like below then you need to use the . operator as you are using the . operator while defining the name of input field.
<s:form action="sayHello">
<s:textfield name="myMap.salary" label="Salary">
<s:submit/>
</s:form>
If you use [] opertaor for defining the name of input field then use the [] operator to access the property in validator.
So, your validation code above is correct, you just need to define your input field at input form correctly with [] operator.
Hope this helps

Hibernate Validator Framework has a recursive validation feature.
And there is a plugin for Struts2 that uses this:
Full Hibernate Plugin: http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html

I don't think you can do this in the declarative validation. I'd suggest dropping down to the validation method for this, or doing the validation in javascript if you're using the javascript validation that is generated by the framework.

Related

How to store several inputs to an Array in Spring Framework

I'm new to Spring MVC and I'm creating a form with several attributes that the user needs to complete and I want to store a list of numbers into an Array and not create a variable for each field
I try this code, but doesn't work :
Linea 1 <form:input type="number" path="numberArray[0]"/>
Linea 2 <form:input type="number" path="numberArray[1]"/>
...
In Spring 3+, <form:input/> cannot be used for this because the W3C HTML Specification changed to disallow [] in form input name attributes. Here is a good writeup of using the raw HTML input elements to support rows/arrays of fields (it's essentially what you were trying to do with numberArray[index] but using the raw HTML input element).

Struts 2 OGNL - Comparing two string values in validation.xml

I am new to Struts2 and OGNL and am making a simple web application with a registration page. There are two fields, password and repassword (to re-enter the password) and using the validation framework i would like to validate that the two passwords match (I know that I can do it easily with JavaScript). Here is what I've got so far. All of the field-validators are working fine. This is my first non-field validator and I just cant get it to work.
<validator type="expression">
<param name="expression">${password}!=${repassword}</param>
<message>Passwords must match.</message>
</validator>
I tried both with
${password}!=${repassword}
and without
password!=repassword
the OGNL tags.
The expression validator is a Non-Field Level validator. Use fieldexpression validator which is a Field Level validator and validates using OGNL expression. And it must be equals (==) check.
<field name="password">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[password == repassword]]></param>
<message>Passwords must match.</message>
</field-validator>
</field>
The expression validator adds action errors. The fieldexpression validator adds field errors.
try
%{password == repassword}
The validator checks to boolean OGNL expression that both are equal.

html:text how to set placeholder attribute

I mostly do HTML/CSS/JS so i'm kind'a lost here, so no idea if this is possible the way i want it anyway, this is it:
I have this code
<html:text styleClass="span4" title="No spaces or dashes" />
I want this input to render with the attribute "placeholder". How can i go about this?
Thanks in advance!
If struts doesn't provide then you could inject it using jQuery if you need it
$("#idOfInputText").attr('placeholder', 'some text')
you can supply id using styleId attribute of struts's tag

small program on struts2 and jsp

i have done some learning on struts based on one project that i got.Now i have to build 2 to 3 struts jsp pages.
I have following scenario..
<action name="BackAction" class="ClassnamePath">
<result name="user_validated" type="redirectAction">
<param name="actionName">welcome</param>
</result>
<result name="user_profile_found_in_database">/resources/userprofile.jsp</result>
what does param will tell..what is the significance of param(i do not know about this)
my scenario is like this In the class(ClassnamePath) i have done one java program which bring the data from the database and put the values in the userprofile.java(for example the userprofile has variable members like name,email,phone,pin)
that values are come from database and stored in the object of the class userprofile.
i have a task that whenever the result "user_profile_found_in_database" has been done those values should be presented in the jsp as result of the "user_profile_found_in_database"
Is the result "user_profile_found_in_database" that i mentioned in the action tag correct?
the jsp page should be having userprofile with labels as the fields of the userprofile and values should be in the text boxes.
i do not know anything about jsp pages...even web programming..but i am learning on my own..(i am having one doubt how jsp pages are different from struts jsp pages)
with the above tasks i can learn lot in struts and jsp..
Please give some knowledge on this to build further.
JSP pages are not way different than simple JSP pages and in short Struts2 will provides a set of tags which will help we as an end developer to build application fast as these tags provides a easy to access functionality about various features S2 providing, few of them are
Accessing Value Stack using OGNL
Data conversion from server to client and other way around
Features to access other things like request/response/session in most easy and flexible way.
In end when you browser will render the jsp page it will be simple HTML and S2 tags one using in there application will be converted to the HTML as browser will understand HTML.
regarding second part accessing user profile in your jsp do the following gin your Action class
Create an instance of UserProfile in you action class
Create getter and setter for the UserProfile instance
Fill the value in the user-profile (you will fetch that from your DB call)
when your action will send back the response, S2 will place the user-profile instance on top of Value stack and we can access its properties using S2 tags like
<s:textfield name="user_name" value="%{name}"/>
<s:textfield name="user_age" value="%{age}"/>
here name and age are the properties of your user-profile.param in your redirectAction configuration is being used to provide parameters to the result, for more information about what parameters do please read official document.
redirect-action-result
.
Ya i got your question like this,
what is the exact meaning of param tag?
param tag means:
Struts 2 “param” tag is used to parametrize other tags. However, when you declared the “param” tag, the “parameter value” can be define in two ways :
“value” attribute
Text between the start and end of “param” tag.
For Example:
<param name="fruit">Banana</param>
<param name="fruit" value="Banana"/>
In Your example the 1st case i think
Here see this web site it is present complete structure...
http://www.mkyong.com/struts2/struts-2-param-tag-example/

binding form parameters to a bean using just Servlets and JSP - possible?

I am using Servlet and JSP without a framework to study for my SCWCD. I have a simple form that I want the parameters to bind to a bean automatically. Is this possible without writing binding code or using a framework?
Thanks
Well, without a "framework" you can't do this. But you can use the Jakarta BeanUtils (http://commons.apache.org/beanutils/), more precisely the static method BeanUtils.populate in your servlet. Ex.:
BeanUtils.populate (myBean, request.getParameterMap());
Remember: the input properties names must match with bean attributes, ok?
You can do this with <jsp:useBean>.
<jsp:useBean id="form" class="com.example.Form" scope="request" />
<jsp:setProperty name="form" property="*" />
<jsp:include page="servletUrl" />
All bean properties whose names match the request parameter names -if any- will be set and the bean will be available as request attribute in the servlet matching the url-pattern of /servletUrl.
However, you'd like to use a servlet and/or MVC framework for this since it abstracts it all away and gives a better control over actions and response handling. This is essentially abuse of JSP (as being a view technology) as controller (which should be (in)directly done by a Servlet).
No, it isn't. You should use some framework, which I guess would be an overkill.
So what you can do, is iterate request.getParameterMap() keys and set the values to object with the corresponding field names (via reflection)

Categories

Resources