将JSF inputText与支持bean的字段链接而不显示其值 [英] Linking JSF inputText with backing bean's field without showing its value

查看:117
本文介绍了将JSF inputText与支持bean的字段链接而不显示其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的支持bean:

I have backing bean like this:

@ManagedBean
@SessionScoped
public class TestBean {

    private String testString;

    public String getTestString() {
        return testString;
    }

    public void setTestString(String testString) {
        this.testString = testString;
    }
}

我的xhtml页面也非常简单:

And my xhtml page pretty simple too:

<?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"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >

    <h:head></h:head>

    <h:body>

        <h:form>
            <h:inputText value="#{testBean.testString}"/>
            <h:commandButton action="#{testController.testAction}"/>
        </h:form>

    </h:body>

 </html>

我想要的一切 - 渲染我的 h:inputText 没有值的元素(空)。

我是JSF的新手,所以,你能帮助我吗?

祝你好运!


UPD!

这是简化代码,我正在使用 testString 在其他地方和 testString 有值,我想隐藏!我希望保留这个值。

Everything I want - to render my h:inputText element without value (empty).
I'm new to JSF, so, could you help me?
With best regards!

UPD!
It's simplified code, I'm using testString in other places and testString have value, which I want to hide! And I want to keep this value.

推荐答案

前提是它确实是请求/查看范围 bean,您可能是浏览器内置自动完成/自动填充功能的受害者。您可以通过将 autocomplete =off添加到相关输入组件来关闭它。

Provided that it's really a request/view scoped bean, you're likely victim of browser's builtin autocomplete/autofill feature. You can turn it off by adding autocomplete="off" to the input component in question.

<h:inputText ... autocomplete="off" />

再次注意,不是JSF填充了输入,而是填充了webbrowser本身。清除浏览器缓存,您将看到浏览器不再执行此操作。根据浏览器品牌/版本,您还可以将其重新配置为自动填充不那么急切。

Note again that it's not JSF who has filled the inputs, but the webbrowser itself. Clear the browser cache and you'll see that the browser won't do it anymore. Depending on browser make/version you can also reconfigure it to autocomplete a bit less eagerly.

更新:根据你的问题更新,你的bean结果是会话作用域。这不是基于请求/视图的表单的正常范围。会话范围的bean实例在同一HTTP会话中的所有浏览器窗口/选项卡(读取:所有请求/视图)之间共享。您通常只在会话中存储登录用户及其首选项(语言等)。关闭并重新启动整个浏览器或使用其他浏览器/机器时,您将只获得一个全新的实例。

Update: as per your question update, your bean turns out to be session scoped. This is not the normal scope for request/view based forms. A session scoped bean instance is shared across all browser windows/tabs (read: all requests/views) in the same HTTP session. You usually store only the logged-in user and its preferences (language, etc) in the session. You will only get a brand new instance when you shutdown and restart the entire browser, or use a different browser/machine.

将其更改为请求或视图作用域。在这个特殊的简单示例中,请求范围应该足够:

Change it to be request or view scoped. In this particular simple example, the request scope should suffice:

@ManagedBean
@RequestScoped



参见:




  • 如何选择正确的bean范围?

  • See also:

    • How to choose the right bean scope?
    • 更新2 根据评论


      哦,你说对了,我最好使用@RequestScoped。但它并没有解决我的问题 - 我想保留这个值,但我不想在textInput中显示它。在请求 - 响应周期的上下文中,此值很重要。

      具体功能要求现在更加明确(在未来的问题,在准备问题时请注意,我不知道你最初是这样问的。在这种情况下,使用具有以下2个属性的视图范围bean:

      the concrete functional requirement is now much more clear (in future questions, please pay attention to that while preparing the question, I had no idea that you was initially asking it like that). In that case, use a view scoped bean with 2 properties like this:

      @ManagedBean
      @ViewScoped
      public class TestBean {
      
          private String testString;
          private String savedTestString;
      
          public void testAction() {
              savedTestString = testString;
              testString = null;
          }
      
          // ...
      }
      

      您也可以将它存储在数据库或注入的托管bean的属性中,例如,实际上在会话范围内。

      You can alternatively also store it in the database or a property of an injected managed bean which is in turn actually in the session scope, for example.

      这篇关于将JSF inputText与支持bean的字段链接而不显示其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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