简体   繁体   English

将JSF inputText与支持bean的字段链接而不显示其值

[英]Linking JSF inputText with backing bean's field without showing its value

I have backing bean like this: 我有这样的支持bean:

@ManagedBean
@SessionScoped
public class TestBean {

    private String testString;

    public String getTestString() {
        return testString;
    }

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

And my xhtml page pretty simple too: 我的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"
      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>

Everything I want - to render my h:inputText element without value (empty). 我想要的一切 - 渲染我的h:inputText元素没有值(空)。
I'm new to JSF, so, could you help me? 我是JSF的新手,所以,你能帮助我吗?
With best regards! 最诚挚的问候!

UPD! UPD!
It's simplified code, I'm using testString in other places and testString have value, which I want to hide! 这是简化的代码,我在其他地方使用testStringtestString有值,我想隐藏它! And I want to keep this value. 我想保持这个价值。

Provided that it's really a request/view scoped bean, you're likely victim of browser's builtin autocomplete/autofill feature. 如果它确实是请求/视图范围的 bean,那么您可能是浏览器内置自动完成/自动填充功能的牺牲品。 You can turn it off by adding autocomplete="off" to the input component in question. 您可以通过向相关输入组件添加autocomplete="off"来关闭它。

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

Note again that it's not JSF who has filled the inputs, but the webbrowser itself. 再次注意,不是JSF填充了输入,而是webbrowser本身。 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. 根据浏览器品牌/版本,您还可以将其重新配置为自动填充不那么急切。


Update : as per your question update, your bean turns out to be session scoped. 更新 :根据您的问题更新,您的bean结果是会话作用域。 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. 会话范围的bean实例在同一HTTP会话中的所有浏览器窗口/选项卡(读取:所有请求/视图)之间共享。 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

See also: 也可以看看:


Update 2 based on the comment, 根据评论更新2

Oh, you right, it's better for me to use @RequestScoped. 哦,对,我最好使用@RequestScoped。 But it doesn't resolve my problem - I want to keep this value, but I don;t want to show it in textInput. 但它并没有解决我的问题 - 我想保留这个值,但我不想在textInput中显示它。 This value is important in context of request-response cycle. 在请求 - 响应周期的上下文中,此值很重要。

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: 在这种情况下,使用带有2个属性的视图范围bean,如下所示:

@ManagedBean
@ViewScoped
public class TestBean {

    private String testString;
    private String savedTestString;

    public void testAction() {
        savedTestString = testString;
        testString = null;
    }

    // ...
}

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. 或者,您也可以将其存储在数据库或注入的托管bean的属性中,该托管bean实际上也是在会话范围内。

You should bind the input text to some other field in your backing bean. 您应该将输入文本绑定到辅助bean中的其他字段。 And if you want to use that field for your testString , copy the entered value to testString in the testAction method. 如果要将该字段用于testString ,请将输入的值复制到testAction方法中的testString

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

public String testAction()
{
    testString = copyTestString;
    return "destinationPage";
}

有些浏览器会忽略自动填充功能 - 它可以帮助将自动填充功能放在表单标记中:

<h:form autocomplete="off">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM