New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ViewScoped(saveInViewState = true) is recreated on postback when <f:viewAction action> is present #357

Closed
instantiationexception opened this Issue Feb 14, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@instantiationexception

instantiationexception commented Feb 14, 2017

When I click Update button I get NullPointerException. During debugging I noticed that Backing is recreated on postback.

When I change @ViewScoped(saveInViewState = true) to @ViewScoped, everything works as expected - Project object is updated and I see Updated! message.

Backing.java

package app;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.inject.Named;

import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.util.Messages;

@ViewScoped(saveInViewState = true)
@Named
public class Backing implements Serializable {

    private Project project;

    @PostConstruct
    void postConstruct() {
        System.out.println("postConstruct");
    }

    public void init() {
        project = new Project();
    }

    public void update() {
        Messages.addGlobalInfo("Updated!");
    }

    public Project getProject() {
        return project;
    }
}

Project.java

package app;

import java.io.Serializable;

public class Project implements Serializable {

    private String name;

    public String getName() {
        return name;
    }

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app

edit.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <meta charset="utf-8"/>
    </h:head>
    <h:body>

        <f:metadata>
            <f:viewAction action="#{backing.init}"/>
        </f:metadata>

        <h:form id="project-edit">
            <h:messages/>

            <h:panelGrid columns="3" styleClass="ui-panelgrid-blank">
                <h:inputText id="name" value="#{backing.project.name}"/>
                <h:message id="name-message" for="name"/>
            </h:panelGrid>

            <h:commandButton id="update"
                             value="Update"
                             action="#{backing.update}">
                <f:ajax execute="@form" render="@form"/>
            </h:commandButton>
        </h:form>
    </h:body>
</html>
@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Feb 17, 2017

Member

Reproduced. It's caused by creating a new Project inside <f:viewAction> method. When you move that line back into @PostConstruct, then it works fine. The <f:viewAction> is unnecessary in this construct.

@Named
@ViewScoped(saveInViewState = true)
public class Backing implements Serializable {

	private Project project;

	@PostConstruct
	public void postConstruct() {
		System.out.println("postConstruct");
		project = new Project();
	}

	public void update() {
		Messages.addGlobalInfo("Updated!");
	}

	public Project getProject() {
		return project;
	}

}

I will investigate when time allows me. Thank you for reporting!

Member

BalusC commented Feb 17, 2017

Reproduced. It's caused by creating a new Project inside <f:viewAction> method. When you move that line back into @PostConstruct, then it works fine. The <f:viewAction> is unnecessary in this construct.

@Named
@ViewScoped(saveInViewState = true)
public class Backing implements Serializable {

	private Project project;

	@PostConstruct
	public void postConstruct() {
		System.out.println("postConstruct");
		project = new Project();
	}

	public void update() {
		Messages.addGlobalInfo("Updated!");
	}

	public Project getProject() {
		return project;
	}

}

I will investigate when time allows me. Thank you for reporting!

@BalusC BalusC changed the title from @ViewScoped(saveInViewState = true) is recreated on postback to @ViewScoped(saveInViewState = true) is recreated on postback when <f:viewAction action> is present Apr 4, 2017

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC May 20, 2017

Member

I've fixed it in today's latest 2.6.3-SNAPSHOT.

Can you let me know if it works for you too?

Member

BalusC commented May 20, 2017

I've fixed it in today's latest 2.6.3-SNAPSHOT.

Can you let me know if it works for you too?

@BalusC BalusC closed this in 3e94ee4 May 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment