Java – Passing a backing bean instance as parameter for another backing bean method


After the user fills my backing bean with info through the forms, I want to process the instance in Java code (such as JAXB marshalling).

So at the moment i'm doing this like so:

<% OtherBean.method(myBackingBean); %>

which is – if i'm right – not quite an up to date solution 🙂 So how can I make this happen in a 'better' way?

Thanks in advance,

Best Solution

One solution is to add an action method to the bean - it is possible to have it process itself.

For example, this simple bean:

public class SimpleBean {

  private String forename;
  private String surname;

  public String processData() {
    // TODO: real work
    System.out.println("forename=" + forename);
    System.out.println("surname=" + surname);
    return null; // optional navigation rule

  public String getForename() {
    return forename;

  public void setForename(String forename) {
    this.forename = forename;

  public String getSurname() {
    return surname;

  public void setSurname(String surname) {
    this.surname = surname;


...defined in WEB-INF/faces-config.xml:


...can be processed using the this JSP:

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="" version="2.0"
  < language="java"
    contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" />
    <![CDATA[<?xml version="1.0" encoding="UTF-8" ?>]]>
    <![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">]]>
  <html xmlns="">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Enter Name</title>
      <h:panelGrid columns="2">
        <h:outputText value="forename:" />
        <h:inputText id="it1" value="#{simpleBean.forename}" />
        <h:outputText value="surname:" />
        <h:inputText id="it2" value="#{simpleBean.surname}" />
      <h:commandButton action="#{simpleBean.processData}"
        value="process" />

Note the method binding #{simpleBean.processData}. This must be a public method that takes no arguments and returns a String argument (which can be used for page navigation if desired).

This isn't the only way to go about this, but it is fairly straightforward.

Related Question