Good time of day, there is a JSF-2 project [Netbeans 8 + JBoss as 7.1.1 + java ee 6] with two xhtml. Problem Description: When setting a value (result variable) on one page and switching to another, the set value is not saved, but if the result variable is made static, it is saved normally. In java ee newbie if you need something else hint ...
Here is the class code:

import java.io.Serializable; import javax.inject.Named; import javax.faces.bean.SessionScoped; @Named("users") @SessionScoped public class UserBean implements Serializable { private String result = ""; public String getResult() { return result; } public void setResult(String newResult) { result = newResult; } } 

Here is the starting xhtml

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Welcome</title> </h:head> <h:body> <h:form> <h3>Please enter result</h3> <table> <tr> <td>Name:</td> <td><h:inputText value="#{users.result}"/></td> </tr> </table> <p><h:commandButton value="get" action="welcome" /></p> </h:form> </h:body> </html> 

Here is the second xhtml

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Welcome</title> </h:head> <h:body> <h3>#{users.result}!</h3> </h:body> </html> 

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" mlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> </web-app> 

  • Use the "Fragment of the code" button only for the code that can actually be executed in the browser. For non-self-sufficient pieces of code, you should use blocks of code that are formatted with an indent of 4 spaces (Ctrl + K). - Mikhail Vaysman
  • Many thanks, I will know - sair

1 answer 1

You have an annotation conflict: one from the javax.inject package, the other from javax.faces . As a result, the life cycle of the component becomes undefined. This is due to the fact that the javax.inject components must be managed by the container (application server), and the javax.faces components by the JSF framework. If you look under debugging, then each time the UserBean methods are UserBean , a new instance of the class is created, that is, the value from the input form is transferred to one object, and after that the value is requested from another object (where it is empty).

Solution: Replace @Named("users") with @ManagedBean(name = "users") (from the javax.faces.bean package). Alternatively, leave @Named("users") and replace javax.faces.bean.SessionScoped with javax.enterprise.context.SessionScoped .

More details can be found in this answer in English SO.

  • Thank you so much, your answer is like a balm for the soul. All you earthly goods! - sair