title: JSF bean action method return string test
body: <p>I tried to understand from this <a href="https://stackoverflow.com/questions/8744162/difference-between-returning-null-and-from-a-jsf-action">question</a> the difference between returning null and "" in JSF action, but couldn't experience in real.</p>
  
  <p>Below is the quote form the OP</p>
  
  <blockquote>
    <p>From what I understand, when a JSF action returns "" (empty String)
    the user stays on the current page but the view is refreshed. However,
    when the action returns null the user still stays on the current page
    but the old view is reused</p>
  </blockquote>
  
  <p>I am in a confusion of understanding the difference between returning <code>""</code>, <code>null</code> and <code>"viewid?faces-redirect=true"</code> from a JSF Backing bean. I tried to understand the difference with a small example, but I couldn't experience the actual difference. Below are the code snippets of my example.</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
    &lt;display-name&gt;Jsf Questions&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt;
      &lt;param-value&gt;.jspx&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/jsfintroapp/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>faces-config.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xi="http://www.w3.org/2001/XInclude"
   xmlns: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-facesconfig_1_2.xsd"&gt;
   &lt;managed-bean&gt;
    &lt;managed-bean-name&gt;userLogin&lt;/managed-bean-name&gt;
    &lt;managed-bean-class&gt;com.srk.beans.UserLogin&lt;/managed-bean-class&gt;
    &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
   &lt;/managed-bean&gt;
   &lt;navigation-rule&gt;
    &lt;description&gt;Contains list of all navigation rules here&lt;/description&gt;
    &lt;from-view-id&gt;/*&lt;/from-view-id&gt;
    &lt;navigation-case&gt;
     &lt;display-name&gt;WELCOME&lt;/display-name&gt;
     &lt;from-outcome&gt;welcome&lt;/from-outcome&gt;
     &lt;to-view-id&gt;/geek_examples/authorized_user.jspx&lt;/to-view-id&gt;
    &lt;/navigation-case&gt;
  &lt;/faces-config&gt;
  </code></pre>
  
  <p>My managed bean is a request scoped bean, as declared above.</p>
  
  <p>UserLogin.java (Backing bean) code snippet</p>
  
  <pre><code>public String saveData() {
      //String returnString = "";//Case 1 : works - user stays on the same page
      //String returnString = null;//Case 2: works - user stays on the same page
      // Case 3:
      String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
      LOG.debug("view id = "+ viewId);
      String returnString = "?faces-redirect=true";//viewId+
      LOG.debug("return string = "+ returnString);
      LOG.debug("Username = "+ getName() + " password = "+ getPassword());
      return returnString;
  }
  </code></pre>
  
  <p>login.jspx</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
  &lt;f:view&gt;
  &lt;head&gt;
  &lt;title&gt;JSF Question&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h:form&gt;
          &lt;h3&gt;User Name and Password&lt;/h3&gt;
          &lt;table&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;h:outputLabel value="Name"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;h:inputText value="#{userLogin.name}"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;h:outputLabel value="Password"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;h:inputText value="#{userLogin.password}"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;h:commandButton value="Sign In" action="#{userLogin.saveData}"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/h:form&gt;
  &lt;/body&gt;
  &lt;/f:view&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Libraries used in this example</p>
  
  <p><img src="https://i.stack.imgur.com/OOOSh.png" alt="enter image description here"></p>
  
  <p>I tried to access the login.jspx page, entered values in the fields of form and clicked on Sign In. I can always see the same page, no matter what I return there in my saveData() method which is not showing any difference in behavior, Can somebody throw some light with respect to this concept?</p>