Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsf-2

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

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>

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.

like image 624
bsiamionau Avatar asked Jan 18 '13 14:01

bsiamionau


1 Answers

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" />

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.


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

See also:

  • How to choose the right bean scope?

Update 2 based on the comment,

Oh, you right, it's better for me to use @RequestScoped. But it doesn't resolve my problem - I want to keep this value, but I don;t want to show it in 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:

@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.

like image 185
BalusC Avatar answered Sep 20 '22 00:09

BalusC