Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing content of inputText to bean

 
Matthew Cserhati
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have an xhtml file with this content:

<h:inputText tabindex="7" styleClass="input" id="title" value="#{register.title}"
required="true">
</h:inputText>

Basically I want the user to enter his title (e.g. Mr., Dr.), which would be passed on to a bean which would send this information to me in email.

I also have a bean called register, along with setters and getters:

private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

I want to pass this information to a String called body, within the bean:

body = body+"Title: "+this.title+"\n";

I would then send the String in an email.
Any suggestions?
Thanks!

Matthew
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF is a Model/View/Controller (MVC) framework where you write the View templates (xhtml) and the Models (backing beans) and the Controllers are supplied by JSF itself.

If you submit a form with valid data in it, JSF will automatically invoke the appropriate "set" methods for the properties you referenced in the backing bean. You don't have to write any Java code.

Conversely, whatever value you set in a backing bean will be reflected in the View (web page) the next time the View is (re)displayed. Again, done automatically, as JSF's Controllers invoke the propery "get" methods.

Within the action method (actions are not part of MVC, they're the business interface to the Model) the action code can likewise obtain the value posted to the backing bean from the form by invoking the property "get" method. Or by direct field access, if you prefer, but I like to use the get/set methods, since they're easier to trace and the compiler optimizes them anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic