p:chips and ClassCastException

UI Components for JSF
Post Reply
Charriu
Posts: 19
Joined: 28 Mar 2012, 13:19

12 Apr 2018, 12:31

Hello,
I have a problem with the <p:chips> component. I am using Primefaces 6.1.16 and the compont could properly display values in the list, but fails at saving them back in the list. Here are the code snippets:

XHTML

Code: Select all

<p:chips id="chips" value="#{manageEditServicesBean.extAttributeFeature.chips}" />
<p:commandButton
  id="btnTest"
  value="Test"
  actionListener="#{manageEditServicesBean.extAttributeFeature.chpimunk}"
  process="chips, @this"/>
Java

Code: Select all

public class ExtAttributeFeature {

    @Getter
    @Setter
    private List<String> chips = new ArrayList<>();
   
   ....    
Error

Code: Select all

ullAjaxExceptionHandler: An exception occurred during processing JSF ajax request. Error page '/app/pages/error500.xhtml' will be shown.
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
	at org.primefaces.component.chips.ChipsRenderer.getConvertedValue(ChipsRenderer.java:198)
	at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1045)
	at javax.faces.component.UIInput.validate(UIInput.java:975)
Some of my collegues are using Primefaces 6.1 and the <p:chips> component work, when I am switching back to 6.1, I got the same ClassCastException. Has anybody an idea whats going on here?

Kind regards,
Felix
Primefaces 6.1

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

12 Apr 2018, 15:32

I would clone this project: https://github.com/primefaces/primefaces-test

Create a small example proving the problem. if you can reproduce it then report it here: https://github.com/primefaces/primefaces/issues

Make sure to include your sample .zip project you created so people can check it out and do "mvn clean jetty:run" to reproduce and debug.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Charriu
Posts: 19
Joined: 28 Mar 2012, 13:19

18 Apr 2018, 11:45

Hello Melloware,
checked out the test project and add two p:chips, one with direct access to the List<String> and another one with a container class. In the test projects everything works fine.

It seems not to be a problem of primefaces directly, maybe something else causes interferences. Do you know if there are problems with Lombok setters and getters? Or are there any properties in the web.xml which could cause this problems?

Kind regards,
Felix
Last edited by Charriu on 18 Apr 2018, 15:12, edited 1 time in total.
Primefaces 6.1

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

18 Apr 2018, 14:55

you should create the testproject...

Charriu
Posts: 19
Joined: 28 Mar 2012, 13:19

18 Apr 2018, 15:41

kukeltje wrote:
18 Apr 2018, 14:55
you should create the testproject...
Hello kukeltje,
But where to start? There are different Frameworks, a lot of configuration settings. I had hoped that this error may already narrow down where the error could come from. I tried out Lombok, this ist not the problem. The test project just works fine again. When I take a look to the pom.xml, I saw servlet-api 2.7 (we don't use it), jsp-api 2.1 (we don't use it) and our javax.faces is 2.2.11 and not 2.2.7.

I tried this out and changed the test project to javax.faces 2.2.11, it still works fine. I don't exspect JUnit, Logback, JaxB, Google Gson, Apache Commons, Apache POI, Apache FileUpload or JodaTime to cause the error. The last chance was Omnifaces (for GZIP compression like Melloware did it in https://github.com/primefaces/showcase/issues/52), I disabled it, the error still occurs on the project page.

Kind regards,
Felix
Primefaces 6.1

Charriu
Posts: 19
Joined: 28 Mar 2012, 13:19

18 Apr 2018, 16:50

ERROR CAUSE
Responsible for the error was our TrimInputsPhaseListener, who trims als UIComponents, which are instances of HtmlInputText and HtmlInputTextarea. On another component in the same dialog we found a working p:chips, which was vey strange. The only difference was, that the working p:chips was inside a <ui:repeat>.

Here is the old code:

Code: Select all

private void trimTextInputs(final PhaseEvent event, final UIViewRoot viewRoot, final UIComponent parent) {
              for (final UIComponent child : parent.getChildren()) {
			if (child instanceof HtmlInputText || child instanceof HtmlInputTextarea) {
				final UIInput inputElement = (UIInput) child;
				if (inputElement.getSubmittedValue() != null) {
					inputElement.setSubmittedValue(inputElement.getSubmittedValue().toString().trim());
				}
			}
			trimTextInputs(event, viewRoot, child);
		}
}
SOLUTION
Instances of Chips needs to be excluded in the TrimInputsPhaseListener!

Here is the new code:

Code: Select all

private void trimTextInputs(final PhaseEvent event, final UIViewRoot viewRoot, final UIComponent parent) {
              for (final UIComponent child : parent.getChildren()) {
			if ((child instanceof HtmlInputText || child instanceof HtmlInputTextarea) && !(child instanceof Chips)) {
				final UIInput inputElement = (UIInput) child;
				if (inputElement.getSubmittedValue() != null) {
					inputElement.setSubmittedValue(inputElement.getSubmittedValue().toString().trim());
				}
			}
			trimTextInputs(event, viewRoot, child);
		}
}
Kind regards,
Felix
Primefaces 6.1

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

20 Apr 2018, 12:56

Thanks for posting back! That is why we ask people to do the Test project because sometimes it helps you walk backwards until you find the real problem.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

20 Apr 2018, 15:44

Melloware wrote:
20 Apr 2018, 12:56
Thanks for posting back! That is why we ask people to do the Test project because sometimes it helps you walk backwards until you find the real problem.
+1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 48 guests