简体   繁体   English

不调用JSF Action

[英]JSF Action is not called

Once again I have a problem which I can't find the solution to. 我再次遇到一个无法找到解决方案的问题。

A managed bean 托管bean

@Named(value="changeInfoBean")
@RequestScoped
public class ChangeInfoBean {

    private String email;
    private String firstName;
    private String lastName;

    /** Creates a new instance of ChangeInfoBean */
    public ChangeInfoBean() {
            FacesContext context = FacesContext.getCurrentInstance();
            // Gets the user which is currently logged in
            LoginBean bean = (LoginBean) context.getExternalContext().getSessionMap().get("loginBean");
            BasicUser user = bean.getUser();
            this.email = user.getEmail();
            this.firstName = user.getFirstName();
            this.lastName = user.getLastName();

    }

    public String changeName() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Transaction tx = session.beginTransaction();
            BasicUser updateUser = (BasicUser) session.load(BasicUser.class, this.email);
            updateUser.setFirstName(firstName);
            updateUser.setLastName(lastName);
            tx.commit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            session.close();
            return "succes";
        }
    }

The return "succes" is just to return something. 回归“成功”只是为了回报一些东西。 Part of a page that uses the bean 使用bean的页面的一部分

<h:panelGroup rendered="#{param.change == 'name'}">
            <h:form id="changeNameForm">
                <h:messages/>
                <h:panelGrid id="panel1" columns="2">
                    First Name:
                    <h:inputText id="firstName" value="#{changeInfoBean.firstName}"/>
                    Last Name:
                    <h:inputText id="lastName" value="#{changeInfoBean.lastName}"/>
                    <f:facet name="footer">
                        <h:panelGroup style="display:block; text-align:center">
                            <h:commandButton value="Submit Name" action="#{changeInfoBean.changeName}"/>
                        </h:panelGroup>
                    </f:facet>
                </h:panelGrid>
            </h:form>
        </h:panelGroup>

I have checked that all it runs trough every lifecycle phase, tried to make the bean session scoped and tried to set immediate="true". 我已经检查了所有它在每个生命周期阶段运行,尝试使bean会话作用域并尝试设置immediate =“true”。 I have tried to delete the inputText fields and only have the commandButton left. 我试图删除inputText字段,只剩下commandButton。 In every case the method changeName() is not called. 在每种情况下,都不会调用方法changeName()。 What can be done? 可以做些什么?

Apparently the #{param.change} didn't equal to "name" during the form submit request which caused the h:panelGroup not to render which in turn caused the button's action not to be invoked. 显然, #{param.change}在表单提交请求期间不等于"name" ,导致h:panelGroup不呈现,这反过来又导致不调用按钮的操作。

Add the following to your form: 将以下内容添加到表单中:

<input type="hidden" name="change" value="#{param.change}" />

This will retain the #{param.change} for the subsequent request. 这将保留后续请求的#{param.change} Placing the bean in session scope would only have worked when it was been a #{sessionBean.change} . 将bean放在会话范围中只有在它是#{sessionBean.change}

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM