Passing a backup bean instance as a parameter to another backing bean method

After the user fills my backing bean with information via forms, I want to process the instance in Java code (like a JAXB sort).

So at the moment I am doing it like this:

<% OtherBean.method(myBackingBean); %>

      

which is - if I'm right - not really a topical solution :) So how can I make this "better"?

Thanks in advance Daniel.

0


a source to share


2 answers


One solution is to add an action method to the bean - perhaps it handles it 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 handled with 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">
  <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">]]>
  </jsp:text>
  <html xmlns="http://www.w3.org/1999/xhtml">
  <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>

      

Notice the # {simpleBean.processData} binding method. It must be a public method that takes no arguments and returns a String argument (which can be used to navigate the pages if needed).

This isn't the only way to do it, but it's pretty simple.

+3


a source


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



0


a source







All Articles