JSF bean操作方法返回字符串测试 [英] JSF bean action method return string test

查看:109
本文介绍了JSF bean操作方法返回字符串测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从此问题中了解在JSF动作中返回null和"之间的区别,但无法真正体验.

I tried to understand from this question the difference between returning null and "" in JSF action, but couldn't experience in real.

下面是OP的报价

据我了解,当JSF操作返回"(空字符串)时 用户停留在当前页面上,但视图已刷新.然而, 当操作返回null时,用户仍停留在当前页面上 但旧视图已重用

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

我对理解从JSF Backing bean返回""null"viewid?faces-redirect=true"之间的区别感到困惑.我尝试通过一个小例子来了解差异,但是我无法体验实际的差异.以下是我的示例的代码段.

I am in a confusion of understanding the difference between returning "", null and "viewid?faces-redirect=true" 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.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>Jsf Questions</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.jspx</param-value>
  </context-param>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/jsfintroapp/*</url-pattern>
  </servlet-mapping>
</web-app>

faces-config.xml

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
 <managed-bean>
  <managed-bean-name>userLogin</managed-bean-name>
  <managed-bean-class>com.srk.beans.UserLogin</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <description>Contains list of all navigation rules here</description>
  <from-view-id>/*</from-view-id>
  <navigation-case>
   <display-name>WELCOME</display-name>
   <from-outcome>welcome</from-outcome>
   <to-view-id>/geek_examples/authorized_user.jspx</to-view-id>
  </navigation-case>
</faces-config>

我的托管bean是如上所述的请求范围的bean.

My managed bean is a request scoped bean, as declared above.

UserLogin.java(备用Bean)代码段

UserLogin.java (Backing bean) code snippet

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;
}

login.jspx

login.jspx

<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">
<f:view>
<head>
<title>JSF Question</title>
</head>
<body>
    <h:form>
        <h3>User Name and Password</h3>
        <table>
            <tr>
              <td><h:outputLabel value="Name"/></td>
              <td><h:inputText value="#{userLogin.name}"/></td>
            </tr>
            <tr>
              <td><h:outputLabel value="Password"/></td>
              <td><h:inputText value="#{userLogin.password}"/></td>
            </tr>
            <tr>
              <td><h:commandButton value="Sign In" action="#{userLogin.saveData}"/></td>
            </tr>
        </table>
    </h:form>
</body>
</f:view>
</html>

此示例中使用的库

我试图访问login.jspx页面,在表单字段中输入值,然后单击Sign In.无论我在saveData()方法中返回的内容是什么,在行为上都没有任何区别,我总是可以看到同一页面.有人可以对此概念发表一些看法吗?

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?

推荐答案

如该答案所示,返回nullvoid将重用相同的JSF视图,包括与其关联的所有视图作用域的Bean.

As answered in that answer, returning null or void will reuse the same JSF view, including all view scoped beans attached to it.

然而,JSF 1.2没有视图作用域bean"的概念.这是在JSF 2.0中引入的.而且,您有一个请求范围的bean.无论您如何导航,都将始终在每个请求上重新创建它.因此,通过观察bean的行为并不是很明显.仅当您通过在UIViewRoot中放置一个属性,然后检查请求范围内的bean的构造函数是否仍然存在来手动模仿JSF 2.0视图范围时,才会引起注意.

JSF 1.2, however, has no concept of a "view scoped bean". This was introduced in JSF 2.0. Moreover, you've a request scoped bean. It'll always be recreated on every request, regardless of how you navigate. So it's not really noticeable by looking at how the bean behaves. It's only noticeable if you manually mimic the JSF 2.0 view scope by putting an attribute in the UIViewRoot and then checking in the constructor of the request scoped bean if it's still present.

如果您使用了JSF 2.x视图作用域的Bean,则在使用非null结果导航时,您会注意到它已被重新创建(即,它被再次调用(后)构造函数).另请参阅如何选择正确的bean作用域?

If you have used a JSF 2.x view scoped bean, you'd have noticed that it's recreated (i.e. it's (post)constructor is invoked again) when you navigate using a non-null outcome. See also a.o. How to choose the right bean scope?

并且,?faces-redirect=true查询字符串也是JSF 2.x特定的,JSF 1.x无法识别.在JSF 1.x中,要实现相同的效果,请将<redirect/>添加到<navigation-case>,或使用ExternalContext#redirect().

And, the ?faces-redirect=true query string is also JSF 2.x specific and not recognized by JSF 1.x. In JSF 1.x, to achieve the same effect, either add <redirect/> to the <navigation-case>, or use ExternalContext#redirect().

public void saveData() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String url = ec.getRequestContextPath() + "/login.jsf";
    ec.redirect(url);
}

此相关问题详细说明了选择哪种导航方法:

Which navigation approach to choose is elaborated in this related question: How to navigate in JSF? How to make URL reflect current page (and not previous one). Generally, in UX and SEO perspective, using POST for page-to-page navigation is bad practice. Always use GET for that or perform a redirect after POST.

无论如何,在使用JSF 1.x进行开发时,您不应查看针对JSF 2.x的答案/资源.这只会引起混乱,因为在JSF 2.x中许多事情都做得不同(更好!). JSF 2.x已经存在超过5年了,而JSF 1.x几乎已经停产了.继续研究过时的技术是没有意义的. 考虑迁移.

In any case, when developing with JSF 1.x, you should not be looking at JSF 2.x targeted answers/resources. It'll only lead to confusion because many things are done differently (better!) in JSF 2.x. JSF 2.x exist more than 5 years already and JSF 1.x was EOL for nearly as long. It doesn't make sense to keep working on dead technology. Consider migrating.

这篇关于JSF bean操作方法返回字符串测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆