0

Business requirement

We had to let the user enter the employer name and employer title for his employer (put in a div lets say the class="empInfo"). Now the user may even have multiple employers, and so we provide a button which on click will create another div(I basically clone the div).

Work Flow

There is a form Start page(.xhtml file written in jsf ) where the user can enter the details of the form including the employer info(s). The user then clicks continue and then they go to the review page(also another JSF page) where the user can review add/delete more employer info.

How I am approaching this problem.

  1. We had to let the user enter the employer name and employer title for his employer.

    • Put two text field in a div with class="empInfo".
    • Add button for the add/delete using Jquery to clone/delete the div with class
  2. Putting the value back in the managed bean.
    This is where It gets tricky (for me,given my relatively less experience in J2ee and especially JSF).

    • The bean property must be mapped to two ArrayList
      • one for the employer Name
      • one for the employer Title
    • I cannot write a forEach JSF/JSP tag because the values cannot be directly mapped to the backend after I clone a div.
    • So I added two hidden field (one for each bean property).
    • When the user clicks on the navigation button to go to a different page I use JS to collect all the value in the div with class ="empInfo" and I set the two hidden value field which is mapped to the Array Lists of the bean.
    • I know,the value from the view is always String.
    • So I decided to implement a converter to convert the string to the AL.

Problem

I am not able to get the value from the view to be sucessfully passed to the overridden converter method in the validater class.

Hidden Fields in JSF (Using tomahawk tags)

<div class="XYZ">   
    <t:inputHidden value="#{Ctrlr.employerName}" id="eName" forceId="true" converter="jsArrayConverter"></t:inputHidden>        
</div>

Converter implementation

import javax.faces.convert.Converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

public class FMLAConverter implements Converter{

@Override
public ArrayList<String> getAsObject(FacesContext context, UIComponent   component,String ret) {
    System.out.println("Inside Converter function, getting value from the view    and putting it in the model");    
    System.out.println("I am working with "+ret +"Hmmm");
    ArrayList<String> list = new ArrayList<String>();
    int j=0;
    int i=ret.indexOf(',');
    int len = ret.length();
    if (i == -1){
        if(len==0){
            System.out.println(list);
            return list;
        }
        else
        {
            list.add(ret);
        }
    }
    while(i>0)
    {
        list.add(ret.substring(j,i));
        j=i+1;
        if(i+1<len)
            i = ret.indexOf(',', i+1);
        else
            i=-1;   
    }
    System.out.println(list);
    return list;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    ArrayList <String> list = (ArrayList <String>) value;
    int i=0;
    StringBuilder str= new StringBuilder();
    if(list==null)
        { 
            System.out.println("It is null");
            return str.append("").toString();
        }
    if(list.size()==0){
        System.out.println("Really");
        str.append("");
        return str.toString();
    }

    for(String string:list)
    {   
        if(i++==0)  
            str.append(string); 
        else
            str.append(",").append(string);         
    }
    System.out.println("Not bad");
    return str.toString();
    }
    }

Eliminating silly errors

  • JS correctly add the the value to the hidden field.
  • Attaching screenshot from the chrome console.

    google console screenshot

Debugging steps

  • Working at it for the last 48 hours,cannot understand why its not working.Did my research,can not find any thing.
  • Looking in the system out log on the server I see the log

    I am working with Hmmm

  • So I made the inference the value back from the hidden field is "" an empty string which is strange.(unless If i have not completely understand the JSF lifecycle)

  • To be sure, I see that the ret value is after the user clicks on the next button in the form. I added a checkpoint at window.unload and took the screen shot from the console, so JS is fine.
  • Any help is greatly appreciated thanks.I already went through BALUS's tutorials(not all,but a lot),which was very helpful.Also any other links or tutorials to JSF would be more helpful.
5
  • In short, the browser didn't send the value of the hidden input field on submit? Have you confirmed it by inspecting the HTTP traffic monitor ("Network" tab in Chrome developer toolset). You can indeed not do much with a converter then.
    – BalusC
    Jul 27, 2015 at 20:18
  • I did and that was the scrrenshot. Jul 27, 2015 at 20:27
  • I am setting the value on window.unload(),could that be a problem. Jul 27, 2015 at 20:28
  • Screenshot only shows JS console, not HTTP traffic monitor. As to setting the value, it should be set before form submit request is prepared. That's definitely not during window's unload event, but during form's submit event (or submit button's click event).
    – BalusC
    Jul 27, 2015 at 20:34
  • @BalusC you were right. Thanks for the help. Jul 27, 2015 at 20:38

1 Answer 1

0

If the submitted value is null or empty, then it simply means that the webbrowser didn't send the expected input value. Not any kind of code logic in the JSF converter can help in solving this.

Take a step back and fix the client side code accordingly that the webbrowser actually sends the expected input value. As per the question's comments it turns out that you're setting the value during window's unload event. That's too late already. That event is fired when the request is already sent for long. You need to set the value during form's submit event or the submit button's click event. Instead of fiddling in browser's JS console, you'd better have tabbed from Console tab to Network tab and inspect the HTTP request payload over there. Is the request parameter present or not?

As to the concrete question, how to properly implement a Converter is elaborately explained in this answer: Conversion Error setting value for 'null Converter'

2
  • whatever you have said is perfectly right , the issue was in the client side I did not set the value in the hidden field properly. the converter interface worked fine. I just wanted to know since I am new to JSF,if for the requirement I mentioned how senior developers would have approached the same requirement.would they have approached the way I did using hidden fields,JS and jsf or is there a more elegant way. Once again thanks for your help. Jul 30, 2015 at 23:04
  • Clone the div in server side instead of client side. The answer to this related question may contain clues: stackoverflow.com/q/21290191
    – BalusC
    Jul 31, 2015 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.