dúvida jsf2

3 respostas
R

tenho o seguinte exemplo em jsf2 e nao consigo adicionar bugs em projects, seguinte erro:


HTTP Status 500 - javax.el.ELException: Cannot convert proj01 of type class java.lang.String to class sessionbeans.ProjectRepository

type Exception report

message javax.el.ELException: Cannot convert proj01 of type class java.lang.String to class sessionbeans.ProjectRepository

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.el.ELException: Cannot convert proj01 of type class java.lang.String to class sessionbeans.ProjectRepository
javax.faces.webapp.FacesServlet.service(FacesServlet.java:229)

root cause

org.apache.myfaces.view.facelets.el.ContextAwareELException: javax.el.ELException: Cannot convert proj01 of type class java.lang.String to class sessionbeans.ProjectRepository
org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.setValue(ContextAwareTagValueExpression.java:166)
javax.faces.component.UIInput.updateModel(UIInput.java:406)
javax.faces.component.UIInput.processUpdates(UIInput.java:328)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIForm.processUpdates(UIForm.java:263)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIViewRoot._processUpdatesDefault(UIViewRoot.java:1411)
javax.faces.component.UIViewRoot.access$600(UIViewRoot.java:74)
javax.faces.component.UIViewRoot$UpdateModelPhaseProcessor.process(UIViewRoot.java:1549)
javax.faces.component.UIViewRoot._process(UIViewRoot.java:1372)
javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:806)
org.apache.myfaces.lifecycle.UpdateModelValuesExecutor.execute(UpdateModelValuesExecutor.java:38)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

root cause

javax.el.ELException: Cannot convert proj01 of type class java.lang.String to class sessionbeans.ProjectRepository
org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420)
org.apache.el.parser.AstValue.setValue(AstValue.java:218)
org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:253)
org.apache.webbeans.el.WrappedValueExpression.setValue(WrappedValueExpression.java:93)
org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.setValue(ContextAwareTagValueExpression.java:153)
javax.faces.component.UIInput.updateModel(UIInput.java:406)
javax.faces.component.UIInput.processUpdates(UIInput.java:328)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIForm.processUpdates(UIForm.java:263)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1477)
javax.faces.component.UIViewRoot._processUpdatesDefault(UIViewRoot.java:1411)
javax.faces.component.UIViewRoot.access$600(UIViewRoot.java:74)
javax.faces.component.UIViewRoot$UpdateModelPhaseProcessor.process(UIViewRoot.java:1549)
javax.faces.component.UIViewRoot._process(UIViewRoot.java:1372)
javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:806)
org.apache.myfaces.lifecycle.UpdateModelValuesExecutor.execute(UpdateModelValuesExecutor.java:38)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

note The full stack trace of the root cause is available in the Apache Tomcat (TomEE)/7.0.34 logs.
Apache Tomcat (TomEE)/7.0.34

entidades:
package entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Bug {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id", nullable=false, columnDefinition="integer")
	private Long id;

	private String description;

	private String severity;

	@ManyToOne
	private Project project;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSeverity() {
		return severity;
	}

	public void setSeverity(String severity) {
		this.severity = severity;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	@Override
	public int hashCode() {
		return this.description.length() * 23;
	}

	@Override
	public String toString() {
		return description;
	}

	@Override
	public boolean equals(Object obj) {
		if ((obj instanceof Bug)
				&& (((Bug) obj).getDescription().equals(this.description))) {
			return true;
		} else {
			return false;
		}
	}

}
package entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Project {

	@Id
	@GeneratedValue
	@Column(name="id", nullable=false, columnDefinition="integer")
	private Long id;

	private String name;

	private String description;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int hashCode() {
		return this.name.length() * 23;
	}

	@Override
	public String toString() {
		return name;
	}

	@Override
	public boolean equals(Object obj) {
		if ((obj instanceof Project)
				&& (((Project) obj).getName().equals(this.name))) {
			return true;
		} else {
			return false;
		}
	}
}
MBeans
package managedbeans;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;

import sessionbeans.BugRepository;
import sessionbeans.ProjectRepository;
import entities.Bug;
import entities.Project;

@Model
@RequestScoped
public class BugMB implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1339958063986622041L;
	@Inject
	private BugRepository bugRepository;
	@Inject
	private ProjectRepository projectRepository;
	private Bug bug = new Bug();
	private Long projectId;
	private List<Bug> bugs;

	public void save() {
		Project project = this.projectRepository.findById(this.projectId);
		this.bug.setProject(project);

		if (this.getBug().getId() == null) {
			this.bugRepository.add(this.getBug());
		} else {
			this.bugRepository.edit(this.getBug());
		}
		this.bug = new Bug();
		this.bugs = null;
	}

	public void delete(Long id) {
		this.bugRepository.removeById(id);
		this.bug = null;
	}

	public void prepareEdit(Long id) {
		this.bug = this.bugRepository.findById(id);
	}

	public Bug getBug() {
		return bug;
	}

	public List<Bug> getBugs() {
		if (this.bugs == null) {
			this.bugs = this.bugRepository.findAll();
		}
		return bugs;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	public Long getProjectId() {
		return projectId;
	}

	public BugRepository getBugRepository() {
		return bugRepository;
	}

	public void setBugRepository(BugRepository bugRepository) {
		this.bugRepository = bugRepository;
	}

	public ProjectRepository getProjectRepository() {
		return projectRepository;
	}

	public void setProjectRepository(ProjectRepository projectRepository) {
		this.projectRepository = projectRepository;
	}

	public void setBug(Bug bug) {
		this.bug = bug;
	}

	public void setBugs(List<Bug> bugs) {
		this.bugs = bugs;
	}

}
package managedbeans;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;

import sessionbeans.ProjectRepository;
import entities.Project;

@SuppressWarnings("serial")
@Model
@RequestScoped
public class ProjectMB implements Serializable {
	@Inject
	private ProjectRepository projectRepository;

	private Project project = new Project();

	private List<Project> projects;

	public void save() {
		if (this.getProject().getId() == null) {
			this.projectRepository.add(this.getProject());
		} else {
			this.projectRepository.edit(this.getProject());
		}
		this.project = new Project();
		this.projects = null;
	}

	public void delete(Long id) {
		this.projectRepository.removeById(id);
		this.projects = null;
	}

	public void prepareEdit(Long id) {
		this.project = this.projectRepository.findById(id);
	}

	public ProjectRepository getProjectRepository() {
		return projectRepository;
	}

	public void setProjectRepository(ProjectRepository projectRepository) {
		this.projectRepository = projectRepository;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<Project> getProjects() {
		if (this.projects == null) {
			this.projects = this.projectRepository.findAll();
		}
		// List<Project> projects = new ArrayList<Project>();
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}
}
paginas bugs.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">

<h:head>
	<title>Bugs</title>
</h:head>

<h:body>
	<ui:include src="/menu.xhtml" />

	<hr />

	<h1>New Bug</h1>
	<h:form>
		<h:panelGrid>
			<h:inputHidden value="#{bugMB.bug.id}" />

			<h:outputLabel value="Severity:" />
			<h:selectOneMenu value="#{bugMB.bug.severity}">
				<f:selectItem itemValue="LOW" />
				<f:selectItem itemValue="MEDIUM" />
				<f:selectItem itemValue="HIGH" />
			</h:selectOneMenu>


			<h:outputLabel for="description" value="Description:" />
			<h:inputTextarea id="description" value="#{bugMB.bug.description}" />

			<h:outputLabel for="selecaoProjeto" value="Project:" />
			
			<h:selectOneMenu id="selecaoProjeto" value="#{bugMB.projectRepository}">			
				<f:selectItems value="#{projectMB.projects}" var="project"
					itemLabel="#{project.description}" itemValue="#{project}"/>
			</h:selectOneMenu>

			<h:commandButton action="#{bugMB.save}" value="Save" />
		</h:panelGrid>
	</h:form>

	<hr />

	<h1>Bug List</h1>
	<h:dataTable value="#{bugMB.bugs}" var="bug"
		rendered="#{not empty bugMB.bugs}" border="1">
		<h:column>
			<f:facet name="header">Id</f:facet>
 #{bug.id}
 </h:column>

		<h:column>
			<f:facet name="header">Project</f:facet>
 #{bug.project.name}
 </h:column>

		<h:column>
			<f:facet name="header">Severity</f:facet>
 #{bug.severity}
 </h:column>

		<h:column>
			<f:facet name="header">Description</f:facet>
 #{bug.description}
 </h:column>

		<h:column>
			<f:facet name="header">Delete</f:facet>
			<h:form>
				<h:commandLink action="#{bugMB.delete(bug.id)}">delete</h:commandLink>
			</h:form>
		</h:column>
		<h:column>
			<f:facet name="header">Edit</f:facet>
			<h:form>
				<h:commandLink action="#{bugMB.prepareEdit(bug.id)}">edit</h:commandLink>
			</h:form>
		</h:column>
	</h:dataTable>
</h:body>
</html>
projects.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">

<h:head>
	<title>Projects</title>
</h:head>

<h:body>
	<ui:include src="/menu.xhtml" />

	<hr />

	<h1>New Project</h1>
	<h:form id="form">
		<h:panelGrid id="grid">
			<h:inputHidden value="#{projectMB.project.id}" />

			<h:outputLabel for="input1" value="Name:" />
			<h:inputText id="input1" value="#{projectMB.project.name}" />

			<h:outputLabel for="input2" value="Description:" />
			<h:inputTextarea id="input2" value="#{projectMB.project.description}" />

			<h:commandButton action="#{projectMB.save}" value="Save" />
		</h:panelGrid>
	</h:form>

	<hr />
	

	<h1>Project List</h1>
	<h:dataTable id="dataTable" value="#{projectMB.projects}" var="project"
		rendered="#{not empty projectMB.projects}" border="1">
		<h:column>
			<f:facet name="header">Id</f:facet>
 #{project.id}
 </h:column>

		<h:column>
			<f:facet name="header">Name</f:facet>
 #{project.name}
 </h:column>

		<h:column>
			<f:facet name="header">Description</f:facet>
 #{project.description}
 </h:column>

		<h:column>
			<f:facet name="header">Delete</f:facet>
			<h:form>
				<h:commandLink action="#{projectMB.delete(project.id)}">delete</h:commandLink>
			</h:form>
		</h:column>
		<h:column>
			<f:facet name="header">Edit</f:facet>
			<h:form>
				<h:commandLink action="#{projectMB.prepareEdit(project.id)}">edit</h:commandLink>
			</h:form>
		</h:column>
	</h:dataTable>
</h:body>
</html>

Alguem pode ajudar?

3 Respostas

jomello_br

Amigo eu não conheço de sessionBeans, mas me parece que um dos elementos do sessionbeans.ProjectRepository esta tentando converter para String algo que tem um valor diferente de String ok.

Acho que disse o obvio, mas tentei ajudar ok :slight_smile:

Abraços

Jomello

R

jomello_br:
Amigo eu não conheço de sessionBeans, mas me parece que um dos elementos do sessionbeans.ProjectRepository esta tentando converter para String algo que tem um valor diferente de String ok.

Acho que disse o obvio, mas tentei ajudar ok :slight_smile:

Abraços

Jomello

alguma dica de onde alterar?

LPJava

e não converter, até pq o JSF por default trata String, double, float etc. Veja se em ProjectRepository não tem nada que retorne uma String e que atenda ao que vc quer.

Criado 4 de junho de 2013
Ultima resposta 7 de jun. de 2013
Respostas 3
Participantes 3