某个方法不会被视图调用

时间:2014-12-28 11:14:28

标签: jsf jsf-2 primefaces

我对JSF有一个非常奇怪的问题,但我不能自己解决它,因为我没有可以google的错误消息。问题是,我有一个提交新文章或更新现有文章的视图。

方法getArticle()返回 - 如果?id = x是通过url设置的 - 文章POJO的id为x。否则是一篇纯空的新文章。根据id的设置,模式editArticle设置为true或false。

因此,如果我通过URL传递ID,则表单将更改为“更新文章”,并显示文章的值。但如果我按下提交按钮,则没有任何反应。没有输出,没有错误。在Firefox中使用HTTP Live Headers,我看到了对服务器的请求。在环回接口上查看wireshark,也有流量。

但另一方面,“创建新文章”-Button(如果未设置id)没有问题。

环境:JSF 2.2,Apache Tomcat 7/8,Java 7/8,Windows / Ubuntu(尝试过不同的环境,但总是一样,所以它似乎是“设计”的问题:-))。尝试重命名方法,通过ActionListener调用,更改panelGrids的顺序......但没办法。通过点击按钮永远不会调用bean的方法updateArticle()。

<h:form id="idMasterForm" enctype="multipart/form-data">
    <h:panelGroup rendered="#{userController.loggedIn}">
            <h:panelGrid columns="2" cellpadding="5">
                <p:outputLabel for="title" value="Title" />
                <p:inputMask id="title"
                    value="#{editArticleController.article.title}" />
                <p:outputLabel for="author" value="Author" />
                <p:inputText id="author" value="#{userController.id}"
                    readonly="true" />
                <p:outputLabel for="date" value="Date" />
                <p:inputMask id="date"
                    value="#{editArticleController.article.date}" readonly="true" />
                <p:outputLabel for="link" value="File" />
                <p:selectOneMenu id="link"
                    value="#{editArticleController.article.link}">
                    <f:selectItems value="#{uploadFilesController.filesItems}" />
                </p:selectOneMenu>
                <p:outputLabel for="editor" value="" />
                <p:editor id="editor" widgetVar="editorWidget"
                    value="#{editArticleController.article.text}" width="600" />
            </h:panelGrid>

            <h:panelGrid columns="2" style="margin-top: 10px" rendered="#{!editArticleController.editArticle}">
                <p:commandButton value="Submit new article"
                    action="#{editArticleController.createArticle()}"
                    icon="ui-icon-disk" />
                <p:commandButton value="Clear" type="button"
                    onclick="PF('editorWidget').clear();" icon="ui-icon-close" />
            </h:panelGrid>

            <h:panelGrid columns="2" style="margin-top: 10px" rendered="#{editArticleController.editArticle}" >
                <p:commandButton value="Update article"
                    action="#{editArticleController.updateArticle()}"
                    icon="ui-icon-disk" />
                <p:commandButton value="Clear" type="button"
                    onclick="PF('editorWidget').clear();" icon="ui-icon-close" />
            </h:panelGrid>
            <h:outputText rendered="#{ ! userController.loggedIn}"
                value="#{res.globNotLoggedIn}" />
    </h:panelGroup>
</h:form>

这是Managed Bean:

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import hwr.Util;
import hwr.g2.model.Archive;
import hwr.g2.model.Article;
import hwr.g2.model.Author;
import hwr.user.UserController;

@ManagedBean
public class EditArticleController {
    private Article article;
    private Boolean editArticle;
    private Integer editArticleID;

    public EditArticleController() {
        this.article = new Article();
        System.out.println("EditArticleController was called");
    }

    public Article getArticle() {
        //System.out.println("getArticle() was called");
        if (this.getEditArticle()) {
            this.article = new Article(this.editArticleID);
        }
        return this.article;
    }

    public void setArticle(Integer articleID) {
        this.article = new Article(articleID);
    }

    public void setArticle(Article article) {
        System.out.println("setArticle(Article article) was called");
        this.article = article;
    }

    public void setText(String text) {
        this.article.setText(text);
    }

    public String getText() {
        return this.article.getText();
    }

    public void setTitle(String title) {
        this.article.setTitle(title);
    }

    public String getTitle() {
        return this.article.getTitle();
    }

    public Boolean getEditArticle() {
        // https://stackoverflow.com/questions/550448/get-request-and-session-parameters-and-attributes-from-jsf-pages

        HttpServletRequest req = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
        try {
            this.editArticleID = Integer.valueOf(req.getParameter("id"));
            if (this.editArticleID > 0) {
                this.setEditArticle(true);
            } else {
                this.setEditArticle(false);
            }
        } catch (Exception e) {
            this.setEditArticle(false);
        }
        return editArticle;
    }

    public void setEditArticle(Boolean editArticle) {
        this.editArticle = editArticle;
    }

    public Date getActualDate() {
        Date d = new Date();
        return d;
    }

    public void rangChanged() {
        // nothing
    }

    public String createArticle() {
        System.out.println("New Article...");
        Util util = new Util();
        UserController user = (UserController) util.getBean("userController");

        this.article.setAuthor(new Author(user.getId()));
        this.article.setDate(new Date());

        try {
            Archive archive = new Archive();
            archive.addArticle(this.article);
        } catch (Exception e) {
            System.out.println("something goes wrong here...");
            e.printStackTrace();
        }
        return "g2_home?faces-redirect=true";
    }

    public String updateArticle() {
        System.out.println("*** Update Article... ***");

        System.out.println("Debug: Update article, ID "
                + this.article.getId() + "! title: " + this.article.getTitle()
                + " and text: " + this.article.getText()
                + " and written by "
                + this.article.getAuthor().getFirst());

        try {
            Archive archive = new Archive();
            archive.updateArticle(this.article);
        } catch (Exception e) {
            System.out.println("something goes wrong here...");
            e.printStackTrace();
        }
        return "g2_home?faces-redirect=true";

    }
}

我使用了BalusC(action method is not called in JSF)的清单,但没有发现任何东西。 如果有人能指出我正确的方向,那真的很棒。顺便问一下,有没有更好的方式填写表格?以我的方式,视图中多次调用getArticle()方法。

2 个答案:

答案 0 :(得分:1)

我认为您应该定义托管bean范围@ViewScoped或您想要的任何其他范围。 看看这个问题[问题]:What is the default Managed Bean Scope in a JSF 2 application?

答案 1 :(得分:0)

如果您没有设置bean的范围,默认情况下它将是@RequestScoped,这意味着将根据用户请求重新创建bean。至少将它更改为@ViewScoped,以便在用户与多个请求(例如ajax请求)中的同一视图进行交互时,bean保持活动状态。

@ManagedBean
@ViewScoped
public class EditArticleController {
    //rest of class definition
}