Illegal Syntax for Set Operation - jsf

I have a problem in connecting My xhtml page to the managed bean, the action on the commandButton works but when it comes to passing values it doesn't work.
here is my jsf code:
<h:form id="form" class="form-signin">
<p:panel id="panel" header=" Authentification" style="" >
<h:panelGrid columns="2" rowClasses="3">
<h:outputLabel for="login" value="Nom d'utilisateur :" styleClass=""/>
<p:inputText id="login" value=" #{authenticationBean.profil.login }" required="true" label="login" >
<f:validateLength minimum="4" />
</p:inputText>
<h:outputLabel for="password" value="Mot de passe :" />
<p:password id="password" value=" #{authenticationBean.profil.password }" required="true" label="password" styleClass=""/>
<p:row>
<p:commandButton id="loginButton" value="Login" ajax="false" action="#{authenticationBean.validate}" />
<h:messages id="messages" globalOnly="false"/>
</p:row>
</h:panelGrid>
</p:panel>
</h:form>
i'm using morphia to map data to mongo db, i have also an entitie called profil and one bean to manage authenfication. here is my athentication bean Code :
public class AuthenticationBean implements Serializable {
private static final long serialVersionUID = 1L;
private Profil profil;
private ProfilDAO profileDao = DAOFactory.getProfilDAO();
public void validate() {
FacesMessage message = new FacesMessage("Succès de l'inscription !");
FacesContext.getCurrentInstance().addMessage(null, message);
}
// getters and setters
here is my profil entitie code :
#Entity("profils")
public class Profil {
#Id protected ObjectId _id;
protected String nomProfil,prenomProfil,login,password;
#Embedded protected List<Droit> droits;
#Reference protected Admin admin;
public Profil() {
}
//getters and setters ...
this is the eror i get when i submit some data and click the submit button :
javax.el.PropertyNotWritableException: /index.xhtml #29,125 value=" #{authenticationBean.profil.login }": Illegal Syntax for Set Operation

Look closer at the value and compare with what all sane JSF tutorials/examples try to show you:
value=" #{authenticationBean.profil.login }"
Whitespace is significant in attributes and EL expressions. Get rid of it:
value="#{authenticationBean.profil.login}"

Related

jsf managed bean returns 2 values how to display in JSF

I have an issue where I have a primefaces outputtext field and from the managed bean, I'm attempting to return an array type back to this field. Something is amiss in that I'm getting no return value back to my outputtext field. I will admit that I'm a bit rusty on my Java coding skills and it could be that I'm not performing something correctly within my bean method with respect to my return value.
My JSF page outputtext field
<p:panel id="horizontal" header="Conversion from Lat Long to MGRS" toggleable="true" toggleOrientation="horizontal">
<h:panelGrid columns="2" cellpadding="10" styleClass="left">
<h:outputLabel for="lat" value="Enter Latitude:" />
<p:inplace id="lat">
<p:inputText value="Latitude" />
</p:inplace>
<h:outputLabel for="long" value="Enter Longitude:" />
<p:inplace id="long">
<p:inputText value="Longitude" />
</p:inplace>
<h:outputLabel for="mgrs" value="MGRS conversion value:" />
 <h:outputText id="mgrs" value="#{coordinates.MGRSCoordreturn}" />
<p:commandButton value="Submit" update="mgrs" icon="ui-icon-check" actionListener="#{coordinates.mgrsFromLatLon(lat, long)}"/>
</h:panelGrid>
</p:panel>
<h:outputLabel for="mgrs_input" value="Enter MGRS:" />
<p:inplace id="mgrs_input">
<p:inputText value="MGRS" />
</p:inplace>
<h:outputLabel for="mgrs_output" value="Lat Long conversion values:" />
<h:outputText id="mgrs_output" value="#{coordinates.latLongVReturn}" />
<p:commandButton value="Submit" update="mgrs_output" icon="ui-icon-check" actionListener="#{coordinates.latLonFromMgrs(mgrs_input)}"/>
My managed bean code:
#ManagedBean
#SessionScoped
public class Coordinates implements Serializable{
private String MGRSCoordreturn;
private Double LatLongVReturn;
public String mgrsFromLatLon(double lat, double lon){
// 37.10, -112.12
Angle latitude = Angle.fromDegrees(lat);
Angle longitude = Angle.fromDegrees(lon);
MGRSCoordreturn = MGRSCoord.fromLatLon(latitude, longitude).toString();
return MGRSCoord.fromLatLon(latitude, longitude).toString();
}
public String getMGRSCoordreturn() {
return MGRSCoordreturn;
}
public void setMGRSCoordreturn(String MGRSCoordreturn) {
this.MGRSCoordreturn = MGRSCoordreturn;
}
public double[] latLonFromMgrs(String mgrs){
MGRSCoord coord = MGRSCoord.fromString("31NAA 66021 00000");
double LatLongVReturn[] = new double[]{
coord.getLatitude().degrees,
coord.getLongitude().degrees
};
return new double[]{
coord.getLatitude().degrees,
coord.getLongitude().degrees
};
}
public Double getLatLongVReturn() {
return LatLongVReturn;
}
public void setLatLongVReturn(Double LatLongVReturn) {
this.LatLongVReturn= LatLongVReturn;
}
}

Call Java method from Primefaces dialog

I want to call Java method from Primefaces dialog. I tested this code:
<h:form>
<p:dialog header="New Sensor" widgetVar="dlg" focus="name" modal="true" showEffect="fade">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="name" value="Name" />
........
<p:inputText id="enabled" label="enabled" value="#{newSensor.sensor.enabled}" />
</h:panelGrid>
<f:facet name="footer">
<p:commandButton id="ajax" value="Create Sensor" styleClass="ui-priority-primary" type="button" actionListener="#{newSensor.saveRecord()}"/>
</f:facet>
</p:dialog>
</h:form>
Java bean:
#Named
#RequestScoped
public class NewSensor implements Serializable
{
private SensorObj sensor = new SensorObj();
public SensorObj getSensor()
{
return sensor;
}
public void setSensor(SensorObj sensor)
{
this.sensor = sensor;
}
public void saveRecord(){
System.out.println(">>>>>>>!!!!!! " + sensor.getName());
}
}
Wehn I click the button nothing happens. Can you give some advice how I can fix this issue?
You should remove type="button" in your commandButton because it will prevent the button from sending a request.
Additionally, you are using actionListener in your commandButton.
Your method in the bean should have ActionEvent as its parameter.
public void saveRecord(ActionEvent actionEvent) {
System.out.println(">>>>>>>!!!!!! " + sensor.getName());
}
Please refer here for additional information.

PrimeFaces splitButton: immediate="true" does not seem to work

Sidenote: It works with Command-Buttons.
What I Have:
A simple form where name and address from a customer are entered. In a data-table below, each row contained (in the working version) the following fields:
Name of the customer
A Command-Button which redirects to another page and shows the customers orders (This worked using <p: commandButton immediate="true">)
Another Command-Button which displayed the past orders.
Another Command-Button which was responsible for updating customer-data.
Since I didn't want to have three Command-Buttons in each row I decided to use a Split-Button.
Problem:
<p:splitButton immediate="true">
The form asks me to fill the missing data (name and address) which are set to required="true.
Question:
As far as I understand the attribute immediate="true is used to overcome this issue. So what am I missing ?
Code:
<h:form>
<p:growl id="growl" sticky="false" life="3500" showDetail="false"/>
<h:panelGrid id="customer_grid" columns="2" cellspacing="1" cellpadding="5">
<h:outputLabel id="label" for="name" value="Kunde:" style="font-weight:bold"/>
<p:inputText id="name" value="#{customerController.customer.name}" required="true" requiredMessage="Name eingeben!"/>
<h:outputLabel for="address" value="Adresse:" style="font-weight: bold" />
<p:inputText id="address" value ="#{customerController.customer.address}" required="true" requiredMessage="Adresse eingeben!"/>
<p:commandButton action = "#{customerController.createCustomer}" value="Speichern" style="margin-right:10px"
actionListener="#{growlController.saveMessage}" ajax="true"
onclick="PF('blockUIWidget').block()" oncomplete="PF('blockUIWidget').unblock()" styleClass="save-button"/>
<p:commandButton onclick="history.back(); return false;" value="Abbrechen" ajax="true"/>
<pe:blockUI widgetVar="blockUIWidget">
<h:panelGrid columns="2">
<p:graphicImage id="loader" name="images/ajax-loader.gif" style="margin-right: 12px; vertical-align: middle;" rendered="true"/>
<h:outputText value="Please wait..." style="white-space: nowrap;"/>
</h:panelGrid>
</pe:blockUI>
</h:panelGrid>
<br/>
<br/>
<p:dataTable var="customer" value="#{customerController.allCustomers}" resizableColumns="true" tableStyle="width: auto"
rendered="#{not empty customerController.allCustomers}">
<p:column headerText="customer" style="width: 300px">
<h:outputText value="#{customer.name}" />
</p:column>
<p:column>
<p:splitButton value="current order" action="#{userController.setup(customer, 'lastOrder')}" immediate="true">
<p:menuitem value="old orders" action="#{userController.setup(customer, 'oldOrders')}" immediate="true"/>
<p:menuitem value="edit" action="#{userController.setup(customer, 'update')}" immediate="true"/>
</p:splitButton>
</p:column>
</p:dataTable>
EDIT:
According to the comment of BalusC I've put the two parts in separate forms.
Effect: The message to fill out the above form does not show up, but the redirect is not happening either.
EDIT2:
The purpose of the method userController.setup(customer, 'String') is basically to inject the customer who is represented for each row. The String is returned for redirecting purposes which are set in the faces-config.xml and as I said: It works when I use Command-Buttons instead.
CODE:
#Named
#SessionScoped
public class UserController implements Serializable{
#Inject
private Customer customer;
#EJB
private CustomerService customerService;
public UserController(){
}
public List<Item> getItems(){
return customerService.getItems(customer);
}
public Ordery getCurrentOrder(){
return customerService.getCurrentOrder(customer);
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String setup(Customer customer, String nav){
this.customer = customer;
return nav;
}
public void update(){
customerService.update(customer);
}
}

Show faces message below input fields

I am working with JSF (PrimeFaces)/EJB/JPA and a MySQL database. What I want to do is to show AT THE TOP OF THE XHTML BODY "User was successfully added to database" info message when an user is introduced successfully in the DB. I know I can use h:messages but then, also the validation error messages `are shown at the top of the body, and I want the error messages to be shown below the input fields. How could I do this?
Facelet code
<h:body>
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>`<!--WRONG-->
<p:inputText id="name" required="true" value="#{usersManagedBean.username}" requiredMessage="requiered field">
<f:validator validatorId="validators.NameValidator"/>
</p:inputText>
<p:watermark for="name" value="User" />
<p:message for="name" />
<p:inputText id="age" required="true" value="#{usersManagedBean.username}" requiredMessage="requiered field">
<f:validator validatorId="validators.AgeValidator"/>
</p:inputText>
<p:watermark for="age" value="Age" />
<p:message for="age" />
<p:commandButton value="Save user" action="#{usersManagedBean.saveUser}" ajax="false" />
</h:body>
Managed Bean code
//imports
#Named(value = "usersManagedBean")
#SessionScoped
public class UsersManagedBean implements Serializable
{
String username;
int userage;
#PostConstruct
public void init()
{
username="";
userage=0;
}
//Getters and Setters
public void saveUser()
{
User eUser = new Users();
eUser.setName(username);
eUser.setAge(userage);
ejbUsersDAO.create(eUser); //DAO
addSuccessMessage("User was successfully added to database");
}
public static void addSuccessMessage(String msg)
{
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage("successInfo", facesMsg);
}
}
Try setting globalOnly="true" attribute in your <h:messages> and change your addSuccessMessage to
fc.addMessage(null, facesMsg);

How validate two password fields by ajax?

I'm trying to validate two password fields with JSF but no good until now, I search for it on google but everything was about JSF 1.2 and pretty confusing, I'm using JSF 2.0.
This is what I'm doing so far:
<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{register.user.password}" >
<f:ajax event="blur" listener="#{register.validatePassword}" render="m_password" />
</h:inputSecret>
<rich:message id="m_password" for="password"/>
<h:outputLabel for="password_2" value="Password (again):" />
<h:inputSecret id="password_2" value="#{register.user.password_2}" >
<f:ajax event="blur" listener="#{register.validatePassword}" />
</h:inputSecret>
This is how I it is my controller:
public void validatePassword() {
FacesMessage message;
if (!user.getPassword().equals(user.getPassword_2()) ){
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "different password");
}else{
message = new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok");
}
FacesContext.getCurrentInstance().addMessage("form:password", message);
}
Any idea guys ?
First of all, use a real Validator to validate the input. Don't do it in an action event method.
As to your concrete problem, you just need to specify the both fields in the execute attribute of the <f:ajax>, it namely defaults to the current component only. If you attach a validator to the first input and send the the value of the second input along as a <f:attribute>, then you will be able to grab it in the validator. You can use the binding attribute to bind the component to the view. This way you can pass its submitted value along by UIInput#getSubmittedValue().
Here's a kickoff example:
<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{bean.password}" required="true">
<f:validator validatorId="confirmPasswordValidator" />
<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
<f:ajax event="blur" execute="password confirm" render="m_password" />
</h:inputSecret>
<h:message id="m_password" for="password" />
<h:outputLabel for="confirm" value="Password (again):" />
<h:inputSecret id="confirm" binding="#{confirmPassword}" required="true">
<f:ajax event="blur" execute="password confirm" render="m_password m_confirm" />
</h:inputSecret>
<h:message id="m_confirm" for="confirm" />
(note that I added required="true" to both components and also note that you don't necessarily need to bind the confirm password component value to a managed bean property, it's worthless over there anyway)
with this validator
#FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {
#Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String password = (String) value;
String confirm = (String) component.getAttributes().get("confirm");
if (password == null || confirm == null) {
return; // Just ignore and let required="true" do its job.
}
if (!password.equals(confirm)) {
throw new ValidatorException(new FacesMessage("Passwords are not equal."));
}
}
}
With seam 2 you have the component <s:validateEquality> and you don't need to write code. For JSF2 then you have Seam 3 modules, particulary Faces module and Cross-field Form Validation. An example :
First you have to use the s:validateForm tag:
<h:form id="passwordForm">
<h:inputSecret id="newPassword"
required="true"
redisplay="true"
value="#{passwordController.newPassword}">
</h:inputSecret>
<h:inputSecret id="confirmationPassword"
value="#{passwordController.confirmPassword}"
required="true"
redisplay="true">
</h:inputSecret>
<h:commandButton id="submit" value="Submit" action="#{passwordController.submitPassword}" />
<s:validateForm validatorId="passwordValidator" />
</h:form>
and the corresponding Validator for the password form above would look like this:
#FacesValidator("PasswordValidator")
public class PasswordValidator implements Validator
{
#Inject
#InputField
private String newPassword;
#Inject
#InputField
private String confirmPassword;
#Override
public void validate(final FacesContext context, final UIComponent comp, final Object values) throws ValidatorException
{
if (!confirmPassword.equals(newPassword))
{
throw new ValidatorException(new FacesMessage("Passwords do not match!"));
}
}
}
You can use Primefaces p:password tag. Please see demo example. It has match attribute which should be the id of confirm password.
<p:panel header="Match Mode">
<p:messages id="messages" showDetail="true" autoUpdate="true"/>
<h:panelGrid columns="2" id="matchGrid">
<h:outputLabel for="pass" value="Password " />
<p:password id="pass" value="#{passwordBean.password}" match="confirmPass" required="true"/>
<h:outputLabel for="confirmPass" value="Confirm Password " />
<p:password id="confirmPass" value="#{passwordBean.confirmPassword}" required="true"/>
</h:panelGrid>
<p:commandButton id="saveButton" update="matchGrid" value="Save" />
</p:panel>

Resources