Mark Griffin
Mark Griffin

Reputation: 1

JSF 2.2 page, backing bean, page rendering?

I have a requirement where 2 input values are entered onto JSF 2.2 page. We are using Primefaces controls. These values are then submitted to managed bean method via h:command button.

Then, based upon the values, I want to set a separate output field on the same JSF page to a specific value.

So my issue at the moment is trying to wire a managed bean so that it returns a value back to field on my JSF page which in this case field name is mgrs. The main issue is that I'm dealing with is the third party library that produces the return values and I'm not sure the best approach on how to interface with this library so that I can return the values that I need from it. Also from within my JSF page what would be the best approach from the commandbutton to get the value back from the bean code?

Here is the working portion of my JSF page

     <p:panel id="horizontal" header="Horizontal Toggle" toggleable="true" 
      toggleOrientation="horizontal">
     <h:panelGrid columns="2" cellpadding="10" styleClass="left">

     <h:outputLabel for="basic" value="Enter Latitude:" />
     <p:inplace id="lat">
      <p:inputText value="Latitude" />
     </p:inplace>

      <h:outputLabel for="basic" value="Enter Longitude:" />
      <p:inplace id="long">
        <p:inputText value="Longitude" />
       </p:inplace>

       <p:inplace id="mgrs">
        <p:inputText value="Longitude" />
       </p:inplace>

       <h:commandButton actionlistener="#{coordinates.mgrsFromLatLon(lat, long)}"  update="mgrs" />

Here is the third party API:

  package com.berico.coords;

  import gov.nasa.worldwind.geom.Angle; 
  import gov.nasa.worldwind.geom.coords.MGRSCoord;
  import javax.faces.bean.ManagedBean;

  @ManagedBean(name="coordinates")

  public class Coordinates {

public static String mgrsFromLatLon(double lat, double lon){

    Angle latitude = Angle.fromDegrees(lat);

    Angle longitude = Angle.fromDegrees(lon);

    return MGRSCoord
            .fromLatLon(latitude, longitude)
            .toString();
}

public static double[] latLonFromMgrs(String mgrs){

    MGRSCoord coord = MGRSCoord.fromString(mgrs);

    return new double[]{ 
        coord.getLatitude().degrees, 
        coord.getLongitude().degrees 
    };
}

}

Upvotes: 0

Views: 1106

Answers (1)

Subodh Joshi
Subodh Joshi

Reputation: 13492

Yes Why not its quite simple.

Changes in JSF page

  1. Put your elements inside a <h:panelGrid/> or something similar component.
  2. Give one id to your <h:panelGrid id="panelID"/>
  3. Bind your <h:outputtext/> with a variable from your bean.
  4. Use rendered attribute in your <h:outputtext rendered="#{bean.showOutputBox}" />
  5. Now in your button with action use render attribute and give id of pandelgrid which added in Step2

Changes in Java/bean side

  1. Create a variable(boolean) showOutputBox = false with get/set method.

  2. When you click on sumbitmethod call a action in bean ,in that action method make this(above) declare variable true rest JSF will take care.

Upvotes: -1

Related Questions