views:

932

answers:

2

Hey,

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, Daniel.

A: 

you could have one backing bean as an instance variable of the other bean. and configure it in spring or whatever you are using.

mR_fr0g
+2  A: 

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:

<managed-bean>
 <managed-bean-name>simpleBean</managed-bean-name>
 <managed-bean-class>simplebean.SimpleBean</managed-bean-class>
 <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

...can be processed using the this JSP:

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"&gt;
  <jsp:directive.page language="java"
    contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" />
  <jsp:text>
    <![CDATA[<?xml version="1.0" encoding="UTF-8" ?>]]>
  </jsp:text>
  <jsp:text>
    <![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;]]&gt;
  </jsp:text>
  <html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Enter Name</title>
  </head>
  <body>
  <f:view>
    <h:form>
      <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:panelGrid>
      <h:commandButton action="#{simpleBean.processData}"
        value="process" />
    </h:form>
  </f:view>
  </body>
  </html>
</jsp:root>

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.

McDowell
Thank you again McDowell! Didn't think about putting "that" logic in the same bean, however it's pretty obvious. So I'm just calling the needed method in the 'processData' like: method(this), and all working.
wheelie