Java commandButton操作未在JSF中调用bean方法
我是JSF的新手,所以我正在做一个学习项目。一切都很好,但当我试图编辑一个记录时,我陷入了我的项目中 首先,我将制作一个CRUD示例,为此,我将使用以下两页:-Java commandButton操作未在JSF中调用bean方法,java,jsf,jpa,ejb,Java,Jsf,Jpa,Ejb,我是JSF的新手,所以我正在做一个学习项目。一切都很好,但当我试图编辑一个记录时,我陷入了我的项目中 首先,我将制作一个CRUD示例,为此,我将使用以下两页:- xhtml-显示数据库中的发布者。它工作正常 add_publisher.xhtml-要添加和编辑发布服务器,当我添加发布服务器时,它可以正常工作,但当我编辑发布服务器时,它甚至不调用该方法 这是我的密码: publisher.xhtml: @ManagedBean @RequestScoped public class Publish
@ManagedBean
@RequestScoped
public class PublisherManager implements Serializable {
private static final long serialVersionUID = 2142383151318489373L;
@EJB
private PublisherBean publisherBean;
private static final Logger logger = Logger.getLogger("bookstore.web.PublisherManager");
private List<Publisher> publishers;
private String newName;
private String newDescription;
private Integer currentId;
private String operation = "add";
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getNewName() {
logger.log(Level.INFO,"Getname called");
return newName;
}
public void setNewName(String newName) {
logger.log(Level.INFO,"Setname called");
this.newName = newName;
}
public String getNewDescription() {
logger.log(Level.INFO,"GetDescription called");
return newDescription;
}
public void setNewDescription(String newDescription) {
logger.log(Level.INFO,"SetDescription called");
this.newDescription = newDescription;
}
public Integer getCurrentId() {
logger.log(Level.INFO,"getCurrentId called");
return currentId;
}
public void setCurrentId(Integer currentId) {
logger.log(Level.INFO,"setCurrentId called");
this.currentId = currentId;
}
public List<Publisher> getPublishers(){
try{
this.publishers = publisherBean.getAllPublishers();
}catch(Exception e){
logger.warning("Can't get publishers");
}
return publishers;
}
public void addPublisher(){
logger.log(Level.INFO,"addPublisher called");
try{
publisherBean.createPublisher(currentId, newName, newDescription, operation);
logger.log(Level.INFO,"Publisher added successfully");
this.currentId = null;
this.newName = null;
this.newDescription = null;
FacesContext.getCurrentInstance().getExternalContext().redirect("publishers.xhtml");
}catch(Exception e){
e.printStackTrace();
logger.warning("Problem when adding publisher");
}
}
}
@Stateful
public class PublisherBean {
@PersistenceContext
private EntityManager em;
private static final Logger logger = Logger.getLogger("bookstore.ejb.RequestBean");
public void createPublisher(Integer id,String name, String description, String operation){
logger.log(Level.INFO,"createPublisher called");
logger.log(Level.INFO,"operation = "+operation);
if(operation.equals("add")){
try{
Publisher publisher = new Publisher(name, description);
logger.log(Level.INFO, "Created Publisher {0}-{1}", new Object[]{name, description});
em.persist(publisher);
logger.log(Level.INFO, "Persisted Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
if(operation.equals("update")){
try{
Publisher publisher = em.find(Publisher.class, id);
publisher.setName(name);
publisher.setDescription(description);
em.merge(publisher);
logger.log(Level.INFO, "Updates Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
}
public List<Publisher> getAllPublishers(){
List<Publisher> publishers = (List<Publisher>) em.createNamedQuery("Publisher.findAll").getResultList();
return publishers;
}
}
没有。
名称
出版
阿西翁
add\u publisher.xhtml:
@ManagedBean
@RequestScoped
public class PublisherManager implements Serializable {
private static final long serialVersionUID = 2142383151318489373L;
@EJB
private PublisherBean publisherBean;
private static final Logger logger = Logger.getLogger("bookstore.web.PublisherManager");
private List<Publisher> publishers;
private String newName;
private String newDescription;
private Integer currentId;
private String operation = "add";
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getNewName() {
logger.log(Level.INFO,"Getname called");
return newName;
}
public void setNewName(String newName) {
logger.log(Level.INFO,"Setname called");
this.newName = newName;
}
public String getNewDescription() {
logger.log(Level.INFO,"GetDescription called");
return newDescription;
}
public void setNewDescription(String newDescription) {
logger.log(Level.INFO,"SetDescription called");
this.newDescription = newDescription;
}
public Integer getCurrentId() {
logger.log(Level.INFO,"getCurrentId called");
return currentId;
}
public void setCurrentId(Integer currentId) {
logger.log(Level.INFO,"setCurrentId called");
this.currentId = currentId;
}
public List<Publisher> getPublishers(){
try{
this.publishers = publisherBean.getAllPublishers();
}catch(Exception e){
logger.warning("Can't get publishers");
}
return publishers;
}
public void addPublisher(){
logger.log(Level.INFO,"addPublisher called");
try{
publisherBean.createPublisher(currentId, newName, newDescription, operation);
logger.log(Level.INFO,"Publisher added successfully");
this.currentId = null;
this.newName = null;
this.newDescription = null;
FacesContext.getCurrentInstance().getExternalContext().redirect("publishers.xhtml");
}catch(Exception e){
e.printStackTrace();
logger.warning("Problem when adding publisher");
}
}
}
@Stateful
public class PublisherBean {
@PersistenceContext
private EntityManager em;
private static final Logger logger = Logger.getLogger("bookstore.ejb.RequestBean");
public void createPublisher(Integer id,String name, String description, String operation){
logger.log(Level.INFO,"createPublisher called");
logger.log(Level.INFO,"operation = "+operation);
if(operation.equals("add")){
try{
Publisher publisher = new Publisher(name, description);
logger.log(Level.INFO, "Created Publisher {0}-{1}", new Object[]{name, description});
em.persist(publisher);
logger.log(Level.INFO, "Persisted Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
if(operation.equals("update")){
try{
Publisher publisher = em.find(Publisher.class, id);
publisher.setName(name);
publisher.setDescription(description);
em.merge(publisher);
logger.log(Level.INFO, "Updates Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
}
public List<Publisher> getAllPublishers(){
List<Publisher> publishers = (List<Publisher>) em.createNamedQuery("Publisher.findAll").getResultList();
return publishers;
}
}
名称
描述
PublisherManager.java:
@ManagedBean
@RequestScoped
public class PublisherManager implements Serializable {
private static final long serialVersionUID = 2142383151318489373L;
@EJB
private PublisherBean publisherBean;
private static final Logger logger = Logger.getLogger("bookstore.web.PublisherManager");
private List<Publisher> publishers;
private String newName;
private String newDescription;
private Integer currentId;
private String operation = "add";
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getNewName() {
logger.log(Level.INFO,"Getname called");
return newName;
}
public void setNewName(String newName) {
logger.log(Level.INFO,"Setname called");
this.newName = newName;
}
public String getNewDescription() {
logger.log(Level.INFO,"GetDescription called");
return newDescription;
}
public void setNewDescription(String newDescription) {
logger.log(Level.INFO,"SetDescription called");
this.newDescription = newDescription;
}
public Integer getCurrentId() {
logger.log(Level.INFO,"getCurrentId called");
return currentId;
}
public void setCurrentId(Integer currentId) {
logger.log(Level.INFO,"setCurrentId called");
this.currentId = currentId;
}
public List<Publisher> getPublishers(){
try{
this.publishers = publisherBean.getAllPublishers();
}catch(Exception e){
logger.warning("Can't get publishers");
}
return publishers;
}
public void addPublisher(){
logger.log(Level.INFO,"addPublisher called");
try{
publisherBean.createPublisher(currentId, newName, newDescription, operation);
logger.log(Level.INFO,"Publisher added successfully");
this.currentId = null;
this.newName = null;
this.newDescription = null;
FacesContext.getCurrentInstance().getExternalContext().redirect("publishers.xhtml");
}catch(Exception e){
e.printStackTrace();
logger.warning("Problem when adding publisher");
}
}
}
@Stateful
public class PublisherBean {
@PersistenceContext
private EntityManager em;
private static final Logger logger = Logger.getLogger("bookstore.ejb.RequestBean");
public void createPublisher(Integer id,String name, String description, String operation){
logger.log(Level.INFO,"createPublisher called");
logger.log(Level.INFO,"operation = "+operation);
if(operation.equals("add")){
try{
Publisher publisher = new Publisher(name, description);
logger.log(Level.INFO, "Created Publisher {0}-{1}", new Object[]{name, description});
em.persist(publisher);
logger.log(Level.INFO, "Persisted Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
if(operation.equals("update")){
try{
Publisher publisher = em.find(Publisher.class, id);
publisher.setName(name);
publisher.setDescription(description);
em.merge(publisher);
logger.log(Level.INFO, "Updates Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
}
public List<Publisher> getAllPublishers(){
List<Publisher> publishers = (List<Publisher>) em.createNamedQuery("Publisher.findAll").getResultList();
return publishers;
}
}
@ManagedBean
@请求范围
公共类PublisherManager实现可序列化{
私有静态最终长serialVersionUID=2142383151318489373L;
@EJB
私人出版社;
私有静态最终记录器Logger=Logger.getLogger(“bookstore.web.PublisherManager”);
私人名单出版商;
私有字符串newName;
私有字符串描述;
私有整数currentId;
私有字符串操作=“添加”;
公共字符串getOperation(){
返回操作;
}
公共void设置操作(字符串操作){
这个操作=操作;
}
公共字符串getNewName(){
logger.log(Level.INFO,“调用Getname”);
返回newName;
}
public void setNewName(字符串newName){
logger.log(Level.INFO,“调用Setname”);
this.newName=newName;
}
公共字符串getNewDescription(){
logger.log(Level.INFO,“调用GetDescription”);
返回newDescription;
}
public void setNewDescription(字符串newDescription){
logger.log(Level.INFO,“调用SetDescription”);
this.newDescription=newDescription;
}
公共整数getCurrentId(){
logger.log(Level.INFO,“调用getCurrentId”);
返回当前ID;
}
public void setCurrentId(整数currentId){
logger.log(Level.INFO,“调用setCurrentId”);
this.currentId=currentId;
}
公共列表getpublisher(){
试一试{
this.publisher=publisherBean.getAllPublishers();
}捕获(例外e){
logger.warning(“无法获取发布服务器”);
}
归还出版商;
}
public void addPublisher(){
logger.log(Level.INFO,“addPublisher被调用”);
试一试{
createPublisher(当前ID、新名称、新描述、操作);
logger.log(Level.INFO,“Publisher添加成功”);
this.currentId=null;
this.newName=null;
this.newDescription=null;
FacesContext.getCurrentInstance().getExternalContext().redirect(“publisher.xhtml”);
}捕获(例外e){
e、 printStackTrace();
logger.warning(“添加发布服务器时出现问题”);
}
}
}
PublisherBean.java:
@ManagedBean
@RequestScoped
public class PublisherManager implements Serializable {
private static final long serialVersionUID = 2142383151318489373L;
@EJB
private PublisherBean publisherBean;
private static final Logger logger = Logger.getLogger("bookstore.web.PublisherManager");
private List<Publisher> publishers;
private String newName;
private String newDescription;
private Integer currentId;
private String operation = "add";
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getNewName() {
logger.log(Level.INFO,"Getname called");
return newName;
}
public void setNewName(String newName) {
logger.log(Level.INFO,"Setname called");
this.newName = newName;
}
public String getNewDescription() {
logger.log(Level.INFO,"GetDescription called");
return newDescription;
}
public void setNewDescription(String newDescription) {
logger.log(Level.INFO,"SetDescription called");
this.newDescription = newDescription;
}
public Integer getCurrentId() {
logger.log(Level.INFO,"getCurrentId called");
return currentId;
}
public void setCurrentId(Integer currentId) {
logger.log(Level.INFO,"setCurrentId called");
this.currentId = currentId;
}
public List<Publisher> getPublishers(){
try{
this.publishers = publisherBean.getAllPublishers();
}catch(Exception e){
logger.warning("Can't get publishers");
}
return publishers;
}
public void addPublisher(){
logger.log(Level.INFO,"addPublisher called");
try{
publisherBean.createPublisher(currentId, newName, newDescription, operation);
logger.log(Level.INFO,"Publisher added successfully");
this.currentId = null;
this.newName = null;
this.newDescription = null;
FacesContext.getCurrentInstance().getExternalContext().redirect("publishers.xhtml");
}catch(Exception e){
e.printStackTrace();
logger.warning("Problem when adding publisher");
}
}
}
@Stateful
public class PublisherBean {
@PersistenceContext
private EntityManager em;
private static final Logger logger = Logger.getLogger("bookstore.ejb.RequestBean");
public void createPublisher(Integer id,String name, String description, String operation){
logger.log(Level.INFO,"createPublisher called");
logger.log(Level.INFO,"operation = "+operation);
if(operation.equals("add")){
try{
Publisher publisher = new Publisher(name, description);
logger.log(Level.INFO, "Created Publisher {0}-{1}", new Object[]{name, description});
em.persist(publisher);
logger.log(Level.INFO, "Persisted Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
if(operation.equals("update")){
try{
Publisher publisher = em.find(Publisher.class, id);
publisher.setName(name);
publisher.setDescription(description);
em.merge(publisher);
logger.log(Level.INFO, "Updates Publisher {0}-{1}", new Object[]{name, description});
}catch(Exception e){
throw new EJBException(e.getMessage());
}
}
}
public List<Publisher> getAllPublishers(){
List<Publisher> publishers = (List<Publisher>) em.createNamedQuery("Publisher.findAll").getResultList();
return publishers;
}
}
@Stateful
公共类PublisherBean{
@持久上下文
私人实体管理者;
私有静态最终记录器Logger=Logger.getLogger(“bookstore.ejb.RequestBean”);
public void createPublisher(整数id、字符串名称、字符串描述、字符串操作){
logger.log(Level.INFO,“调用createPublisher”);
logger.log(Level.INFO,“operation=“+operation”);
if(运算等于(“加法”)){
试一试{
Publisher Publisher=新发布者(名称、说明);
logger.log(Level.INFO,“创建的发布服务器{0}-{1}”,新对象[]{name,description});
em.persist(出版商);
log(Level.INFO,“持久化发布服务器{0}-{1}”,新对象[]{name,description});
}捕获(例外e){
抛出新的EJBException(e.getMessage());
}
}
if(operation.equals(“更新”)){
试一试{
Publisher-Publisher=em.find(Publisher.class,id);
publisher.setName(name);
publisher.setDescription(description);
em.merge(publisher);
log(Level.INFO,“更新发布服务器{0}-{1}”,新对象[]{name,description});
}捕获(例外e){
抛出新的EJBException(e.getMessage());
}
}
}
公共列表getAllPublishers(){
List Publisher=(List)em.createNamedQuery(“Publisher.findAll”).getResultList();
归还出版商;
}
}
当我在添加或编辑时单击add_publisher.xhtml页面上的submit按钮时,会调用相同的方法;当我添加发布者时,该方法会调用并插入记录,但当我编辑产品时,则应调用相同的方法,但这次是用于编辑产品。
但是PublisherManager.java的addPublisher()方法不调用
请帮我找出我的错误
提前感谢…不要对JSF操作使用
form
,只需h:form
。另外,h:commandLink
的action
方法并不像您那样用于指定导航结果。使用h:link
+outcome
或h:commandLink
+服务器端逻辑方法感谢您的回答…它解决了我的问题…非常感谢。请添加您的答案,以便对其他人有所帮助…再次感谢。不客气。如果您愿意,请自己回答,这是一个基本的JSF问题;-)