有关JSF-JPA集成报 Could not find a setter for property message in class ..问题
JSF做前端,后台接收数据,用JPA处理数据时就报错代码如下:JSF端:index.jsp<body><f:view><h2>请输入要查询的用户ID</h2><h:for...
JSF做前端,后台接收数据,用JPA处理数据时就报错代码如下:
JSF端:index.jsp
<body>
<f:view>
<h2>请输入要查询的用户ID</h2>
<h:form id="f">
名称ID:<h:inputText value="#{user.userid}" id="userid">
</h:inputText>
<h:commandButton action="#{useraction.getById}" value="确定">
</h:commandButton>
</h:form>
</f:view>
</body>
</html>
JSF配置文件:
<managed-bean>
<managed-bean-name>useraction</managed-bean-name>
<managed-bean-class>com.weiztek.action.UserAction</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/pages/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/show.jsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
JPA代码:
package com.weiztek.action;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.wiztek.bean.User;
public class UserAction {
public EntityManagerFactory getEMFactory(){
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");
return f;
}
public String getById(){
int id=new Integer(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get( "f:userid"));
//EntityManager em=getEMFactory().createEntityManager();
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");//加上这句话就报错
EntityManager em=f.createEntityManager();
User u=em.find(User.class, id);
System.out.println(id);
em.close();
f.close();
return "success";
}
} 展开
JSF端:index.jsp
<body>
<f:view>
<h2>请输入要查询的用户ID</h2>
<h:form id="f">
名称ID:<h:inputText value="#{user.userid}" id="userid">
</h:inputText>
<h:commandButton action="#{useraction.getById}" value="确定">
</h:commandButton>
</h:form>
</f:view>
</body>
</html>
JSF配置文件:
<managed-bean>
<managed-bean-name>useraction</managed-bean-name>
<managed-bean-class>com.weiztek.action.UserAction</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/pages/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/show.jsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
JPA代码:
package com.weiztek.action;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.wiztek.bean.User;
public class UserAction {
public EntityManagerFactory getEMFactory(){
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");
return f;
}
public String getById(){
int id=new Integer(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get( "f:userid"));
//EntityManager em=getEMFactory().createEntityManager();
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");//加上这句话就报错
EntityManager em=f.createEntityManager();
User u=em.find(User.class, id);
System.out.println(id);
em.close();
f.close();
return "success";
}
} 展开
2个回答
展开全部
报错的意思是,你在往某个类的对象放值的时候,该类里面缺少set方法,类名就是你“Could not find a setter for property message in class ..”省略号那部分,你没打出来的那个类,而我没有看错的话应该就是你最后这个类“UserAction”(虽然你改名字了)。
你看下你给的代码最后一个类,报错的这句话
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");
这里定义了一个EntityManagerFactory对象,然后把值ss放到里面,而一般的类做“放入”操作的时候都要用到setXX()方法的,而你上面只有EntityManagerFactory getEMFactory()也就是get()方法,却没有set()方法,所以在你做set操作的时候却有没有set方法,自然会报出“Could not find a setter for property message in class ..”这样的错误提示
你看下你给的代码最后一个类,报错的这句话
EntityManagerFactory f=Persistence.createEntityManagerFactory("ss");
这里定义了一个EntityManagerFactory对象,然后把值ss放到里面,而一般的类做“放入”操作的时候都要用到setXX()方法的,而你上面只有EntityManagerFactory getEMFactory()也就是get()方法,却没有set()方法,所以在你做set操作的时候却有没有set方法,自然会报出“Could not find a setter for property message in class ..”这样的错误提示
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询