How to validate boolean with XML using Expression - struts2

I have boolean field in action
testMeAction.class
private boolean testMe;
// with setter and getter.
testMeAction-validation.xml
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="expression" short-circuit="true">
<param name="expression"><![CDATA[(testMe == false)]]></param>
<message key="user.message" />
</validator>
</validators>
I would like to validate and display message when testMe = false;
But for some reason, it always displaying message, what ever value of testMe is.
Please can someone advice, where I am going wrong.

Related

How to migrate JSF Validator to Bean Validation?

The usual way to perform JSF validation in an imperative manner is to add validator tag to JSF and have a corresponding Validator class like the following :
View:
<h:outputLabel for="email" value="*Enter Email: " title="Primary Email"/>
<h:inputText id="email" value="#{personView.per.email}" required="true" requiredMessage="email is required"
title="Primary Email" tabindex="3">
<f:validator validatorId="emailValidator" />
</h:inputText>
<h:message id="m2a" for="email" style="color:red"/> <p></p>
Validator:
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
log("Validating submitted email -- " + value.toString());
matcher = pattern.matcher(value.toString());
if (!matcher.matches()) {
FacesMessage msg =
new FacesMessage(" E-mail validation failed.",
"Please provide E-mail address in this format: abcd#abc.com");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
Now here the validation is getting triggered by the JSF. I want to change that part and want that the validation is instead triggered from another Controller via a Bean Validation constraint. That means that, I want to remove the following part :-
<f:validator validatorId="emailValidator" />
and want that this validation is triggered from another controller via a Bean Validation constraint.
Note that, I cannot add the constraint directly on the entity as the entity is a generated file. I want do the validation in the controller itself such that incase of error I could show the error message on the UI.
How can I go about it?
Explanation with some sample code would be appreciated.

struts2 with 3 dependent dropdowns without using ajax

I'm using Struts 2 and MySQL.
In my JSP I have 3 select controls:
<s:select list="country" name="country" onchange="getstate();" />
<s:select list="state" name="state" onchange="getcity();" />
<s:select list="city" name="city" />
In my action class:
public string displayDetails() {
getCountry();
getState(getCountry());
getCity(getState());
}
If I want to get state I have to access getCountry() method again. How can I avoid that call every time?
If I'm not adding all the action in one method I'm getting "list field is not filled" error.

Data Validation: Inheritance and lists

I am used WebWork and I am not able to validate my data properly.
The main two things I don't know how to do are:
-Validate a field with the validator of its super class.
Example: Say we have class Person and Class Employee. I want to validate an attribute of class Employee with my Person-validation.xml. Is it possible?
-Validate the length of a list:
I have an attribute that is a list, how could I check the length of the list and afterwards check every item within the list with its appropriate validator?
I tried:
<field name="list">
<field-validator type="visitor">
<message />
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">
list.size() < 2
</param>
<message key="too much items"/>
</field-validator>
</field>
but it is now working.
Thanks
For you "-Validate the length of a list" problem:
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[2 > list.size]]></param>
<message key="too much items"/>
</field-validator>
If someone has the same problem: At the end it is done automatically!!!
The validator of the super class is called by default :)
and use
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[2 > list.size]]></param>
<message key="too much items"/>
</field-validator>
for the list

primefaces autocomplete when cleared field giving previuos results of page on clicking submit button

I am using an autocomplete tag of prime faces which retrieves results from a database.
The problem is that when I submit the form leaving the autocomplete field empty the results I get on the page are those of the previous request (the previously selected autocomplete value) - it only gets cleared when I refresh the page.
I want that on each submit, without refreshing the browser page, if i clear out the value in the field using backspaces and submit the form it should give the correct result for this particular instance, not previous one.
I am also using some textfields in the jsf page form but those don't have this problem.
Can anyone offer guidance as to how this problem can be corrected?
EDITED:
Code:
<h:form>
<h:dataTable id="Ressult" value="#{input.searchResults}" var="r">
<h:column>#{r.ID}</h:column>
<h:column>#{r.Name}</h:column>
</h:dataTable>
<tr>
<td>Current Education Level</td>
<td>
<h:panelGrid styleClass="text-box">
<p:autoComplete id="education" value="#{input.education}"
completeMethod="#{input.getautocomplete}" var="a"
itemLabel="#{a.Name}" itemValue="#{a}"
converter="edConverter" forceSelection="true" />
<p:column>#{a.Name} - #{a.id}</p:column>
</h:panelGrid>
</td>
</tr>
<tr>
<td>City</td>
<td>
<h:selectOneMenu id="txtCity" styleClass="select-field"
value="#{input.cityId}">
<f:selectItem itemLabel=" Please Select" itemValue="0">
</f:selectItem>
<f:selectItems value="#{input.cities}"></f:selectItems>
</h:selectOneMenu>
</td>
</tr>
<tr>
<td>Name of Person</td>
<td>
<h:inputText id="txtName" value="#{input.nameOfPerson}"
styleClass="text-box"></h:inputText>
</td>
</tr>
<h:commandButton id="btnSearch" value="Search"
action="#{input.searching}">
<f:ajax execute="#form" render="Ressult"></f:ajax>
</h:commandButton>
</h:form>
And here is the bean code:
public class Input
{
private Education education;
private List<SelectItem> cities;
private Integer cityId;
private String nameOfPerson;
private List<Results> searchResults;
//getters and setters
public String searching()
{
searchResults=dao.getSearchResults(cityId,education,nameOfPerson);
return "success";
}
public void autocomplete(String query)
{
//AUTOCOMPLTE lIST CODE HERE
}
}
By update, if you mean new results to be shown when new items selected, then yes - the form should be updated but autocomplete somehow takes the previously selected value and shows results according to that. At least until I refresh the page - only then is autocomplete's previous is removed.
I had the same problem with my autocomplete widget.
When I removed its id attribute it worked. Maybe a bug in Primefaces.
You may have two things to do:
Prevent the user from submission by pressing the Enter key by doing the following in your form:
<h:form onkeypress="return event.keyCode != 13;">
Using itemSelect/itemUnselect features provided to empty the field in the Bean:
<p:ajax event="itemSelect"
listener="#{autoCompleteBean.handleSelect}" global="false"
update=":some:where" />
<p:ajax event="itemUnselect"
listener="#{autoCompleteBean.handleUnselect}" global="false"
update=":some:where" />
<!-- rest of your facelet stuff -->
In the Bean:
public void handleSelect(final SelectEvent event) {
final Search search = (Search) event.getObject();
// do your addition here
}
public void handleUnselect(final UnselectEvent event) {
final Search search = (Search) event.getObject();
// do your substraction here
}
Well if i understand your question correctly your list of auto completion is shown after the post. And you use your form to submit time after time to the same page.
Your bean looks a little bit odd. Because you're calling in the page the autocomplete method: getautocomplete but that one doesn't exists in your bean.
Use the autocomplete in this way:
<p:autoComplete id="education" value="#{input.education}" completeMethod="#{input.autocomplete}" var="a" itemLabel="#{a.Name}" itemValue="#{a}" converter="edConverter" forceSelection="true" />
And in your bean:
public List<Education> autocomplete(String query)
{
List<Education> educations = new ArrayList<Education>();
//search with query in your dao something like:
educations = dao.searchEducation(query);
return educations;
}
fixed on 5.2, upgrade your primefaces jar
here the log issue
https://code.google.com/p/primefaces/issues/detail?id=7592
To fix the subject issue you just remove the forceselection
or make it as false.
Just process event to server. Works with forceselection="true"
<p:ajax event="itemUnselect" global="false" />

Empty input value does not triggger #NotNull but it triggers #NotBlank

i have this entity:
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
#Entity
#XmlRootElement
#Table
public class Supplier extends AbstractEntity
{
#Column
#NotNull
private String name;
#Column
#NotBlank
private String representative;
...
}
and this jsf form:
<h:form>
<p:commandButton value="save" action="#{supplierController.create}" ajax="false"/>
<h:panelGrid columns="3" cellpadding="4">
<h:outputLabel value="#{bundle['Name']}" for="name"/>
<p:inputText id="name" value="#{supplierController.value.name}"/>
<p:message for="name"/>
<h:outputLabel value="#{bundle['Representative']}" for="representative"/>
<p:inputText id="representative" value="#{supplierController.value.representative}"/>
<p:message for="representative"/>
</h:panelGrid>
</h:form>
so, why is #NotBlank triggered and #NotNull is not?
i'm using mojarra-2.1.3_01 (ships hibernate-validator-4.2.0.Final?) under glassfish-3.1.1
Due to the nature of HTTP, empty request parameters are retrieved as empty string instead of null. So the #NotNull validator will never be triggered. It will only be triggered when the field is missing in the form or when it is disabled.
You can however configure JSF 2.x to interpret empty submitted values as null by adding the following context paramter to the web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
This way the #NotNull will be triggered for empty fields.

Resources