6

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.

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
  • And how does it render now ? It should be rendered without a value in your case. – Fallup Jan 18 '13 at 14:22
  • if you want to clear the value after clicking on commandButton, just clear the testString in the testAction method. Initially your inputText will be already empty, if it is not being used by other beans. – cubbuk Jan 18 '13 at 14:25
  • If it's there and `rendered="true"`, you're going to see it. – kolossus Jan 18 '13 at 14:39
  • kolossus, I can see this textInput, but I want to see it value-free! – bsiamionau Jan 18 '13 at 14:48
  • @BahdanSiamionau, my point exactly. If the value is bound and is not Null, it's mandatory you see it. Hiding it means a little creativity on your part by binding it's rendered attribute to a conditional expression maybe like `rendered="#{bean.shown}"`, a condition which you have to then ajax-manage – kolossus Jan 18 '13 at 15:00

3 Answers3

7

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:


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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm sorry, I forgot to show you my class annotations, this bean is session-scoped! – bsiamionau Jan 18 '13 at 14:40
  • Well, then you should surely understand that the very same bean instance is simply shared across multiple browser tabs/windows in the same HTTP session? If you want to store the form data on a per-request or view basis (like as every sane developer would usually do ;) ), then you should make the bean request or view scoped instead. – BalusC Jan 18 '13 at 14:41
  • I sure, that this bean have to be session-scoped (if I correctly got Your question of course ;) ). My `TestBean` should work like `Struts` `ActionForm`. – bsiamionau Jan 18 '13 at 14:46
  • Struts `ActionForm` is under the covers request scoped. Carefully read the linked answer. – BalusC Jan 18 '13 at 14:47
  • 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. – bsiamionau Jan 18 '13 at 14:55
  • You didn't made that very clear in the question. In that case, use a view scoped bean with 2 properties. In submit button, transfer the submitted value to the other property and make the submitted value `null`. – BalusC Jan 18 '13 at 14:57
  • I hoped, that there are other, more simple way. Thank you for attention, I'll choose your answer as accepted because you spent so much time for me) Thankx – bsiamionau Jan 18 '13 at 15:03
  • There may be if you state the concrete functional requirement more clearly. It's still very strange. I haven't seen a real world situation like that. – BalusC Jan 18 '13 at 15:04
  • I'll try to describe my situation, if you're interested. I have some manager of news, there are 3 pages: with full list of brief news (news.xhtml), with one full news content (view.xhtml) and page for adding new news (add.xhtml). add.xhtml page contents 'cancel' button, which turns user back to previous page. All textinputs from form linked with fields of backing bean (and value of this fields I show at view.xhtml page). If user will press 'cancel' button after visiting view.xhtml page, he will lost news data (in case of clearing backing bean's field. Is it clear? It's my situation. Thanks! – bsiamionau Jan 18 '13 at 15:12
  • If I didn't misunderstand you, keeping the news item in the session scope doesn't seem right for this scenario. Just make the view.xhtml's backing bean viewScoped bean and set the selected news when you navigate to this page, do whatever you want with it in view.xhtml and when the user click cancel, just navigate to other page without persisting the news item to your datastore. – cubbuk Jan 18 '13 at 16:06
2

You should bind the input text to some other field in your backing bean. And if you want to use that field for yourtestString, copy the entered value to testString in the testAction method.

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

public String testAction()
{
    testString = copyTestString;
    return "destinationPage";
}
cubbuk
  • 7,800
  • 4
  • 35
  • 62
  • Good idea, but I hope, that there are any other variant to solve problem, more elegant. In Struts it possible by setting value attribute to empty string :) (you know what I mean) – bsiamionau Jan 18 '13 at 15:00
  • 1
    Well I don't know Strut, but I can't come up with any other solution for this particular case using jsf. – cubbuk Jan 18 '13 at 15:01
  • Thank you, I'll use your and BalusC decisions (they're the same) – bsiamionau Jan 18 '13 at 15:15
1

Some Browsers ignore autocomplete - it can help to put autocomplete in form tag:

<h:form autocomplete="off">
A. Roth
  • 11
  • 1