Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NumberFormatException #713

Closed
ruanfelisky opened this issue Apr 20, 2017 · 10 comments
Closed

java.lang.NumberFormatException #713

ruanfelisky opened this issue Apr 20, 2017 · 10 comments

Comments

@ruanfelisky
Copy link

After upgrading to the latest 1.1.0-SNAPSHOT version, on my screens I'm getting some errors like these below:

Javax.faces.FacesException: java.lang.NumberFormatException: For input string: "com.mesure.model.Veiculo@24"

Java.lang.NumberFormatException: For input string: "com.mesure.model.Colaborador@26"

It sounds like something related to my converter, but I have not changed them recently. This started after the last version 1.1.0-SNAPSHOT

@ruanfelisky
Copy link
Author

ruanfelisky commented Apr 20, 2017

I realized that the error occurs when I use a <b: selectOneMenu>.

Before the new version, it did not call my converter (ColaboradorConverter.java), and now started calling.

But the "value" parameter of the "getAsObject" method is coming as "com.mesure.model.Colaborador@23" instead of just "23", and in the "new Long (value)" command the error occurs.

<b:column span="3">
	<b:selectOneMenu label="Colaborador" value="#{programacaoExecucaoBean.filtroColaborador}" fieldSize="sm" 
		process="@this" update="programacoesTable btn-incluir">
		<f:selectItem itemLabel="Selecione" itemValue="" noSelectionOption="true" />
		<f:selectItems value="#{programacaoExecucaoBean.colaboradores}" var="cl" itemLabel="#{cl.nome}" itemValue="#{cl}" />
	</b:selectOneMenu>	    			
</b:column>

Method in ColaboradorConverter.java:

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
	Colaborador retorno = null;
	if (StringUtils.isNotEmpty(value)) {
		retorno = colaboradorRepository.porId(new Long(value));
	}
	return retorno;
}

If I delete the converter class (ColaboradorConverter.java) from my project, the error no longer occurs. So I don't need converter in this case?

@ruanfelisky
Copy link
Author

Class ColaboradorConverter:

@FacesConverter(forClass = Colaborador.class)
public class ColaboradorConverter implements Converter {

	@Inject
	private ColaboradorRepository colaboradorRepository;
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		Colaborador retorno = null;
		if (StringUtils.isNotEmpty(value)) {
			retorno = colaboradorRepository.porId(new Long(value));
		}
		return retorno;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if (value != null) {
			Colaborador colaborador = (Colaborador) value;
			return colaborador.getId() == null ? null : colaborador.getId().toString();
		}
		return "";
	}	
}

@ruanfelisky
Copy link
Author

But I need the converter when I call my form passing the collaborator by parameter, for example:

http://localhost:8080/Mesure/colaboradores/CadastroColaborador.xhtml?colaborador=3

In CadastroColaborador.xhtml:

...
xmlns:o="http://omnifaces.org/ui"
...
...
<f:metadata>
     <o:viewParam name="colaborador" value="#{cadastroColaboradorBean.colaborador}" />
</f:metadata>
...

I realized that <b: selectOneMenu> (Bootsfaces) doesn't have the attribute "convert", But <p:selectOneMenu> (Primefaces) has. Can I configure so that <b: selectOneMenu> does not use converter? That way I can keep my ColaboradorConverter.class for when I call the form with parameter.

@ggam
Copy link
Collaborator

ggam commented Apr 20, 2017

Have you tried setting the ìtemValue to #{cl.id}? You are now passing the whole object, but your converter expects a number.

That worked on 1.0.2 since BootsFaces didn't allow to use converters on SelectOneMenu components (it uses a generic one).

You have multiple options here:

  • Change the itemValue attribute. That's everything you need I think.
  • Change the @FacesConverter(forClass = Colaborador.class) declaration to @FacesConverter("colaboradorConverter") and <o:viewParam name="colaborador" value="#{cadastroColaboradorBean.colaborador}" /> to <o:viewParam name="colaborador" value="#{cadastroColaboradorBean.colaborador}" converter="colaboradorConverter" />
  • Maybe (I haven't tried if it works) you can nest an <f:converter /> with a generic converter like http://showcase.omnifaces.org/cdi/FacesConverter

@ruanfelisky
Copy link
Author

Hi @ggam

I think your suggestion to change the @FacesConverter and the viewParam would be better in my case.

But I have a question: in the current version of BootsFaces does the SelectOneMenu component still use a generic converter? If so, I don't need to create a specific converter for each class, because by default all classes will work with SelectOneMenu, correct?

Thanks.

@ggam
Copy link
Collaborator

ggam commented Apr 20, 2017 via email

@ruanfelisky
Copy link
Author

Ok I got it.

I haven't tried changing the itemValue property yet, but I think it would be better to change the conversor/viewParam, because in this case, I only use the converter when I need, right?

Tks.

@stephanrauh
Copy link
Collaborator

I've added a few lines to the documentation because it's sort of a breaking change. Is it OK to close the ticket?

@dguna
Copy link

dguna commented May 16, 2018

Hi,
I am started to using bootsfaces. I came across this issue of <b:selectOneMenu> issue. Why it is not working as <h:selectOneMenu>. I have my converters as follows @FacesConverter(forClass = CategoryProduct.class). It is not detected, what should I do. It will be time consuming for me to change all the selectOneMenu. Any help will be appreciated.

@stephanrauh
Copy link
Collaborator

BootsFaces doesn't support this because I simply didn't know about @FacesConverter(forClass=...) when I implemented the selectOneMenu.

In the meantime, I've implemented converters, but only the old syntax (<f:converter converterId="com.mkyong.URLConverter" /> in the JSF file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants