Dynamically call methods in JSF

Introduction

I want to change the screen after storing the primary key of the data on the screen in the session. When transitioning from which screen to which screen, I always wanted to call the managed bean method (store the key in the session) of my screen, so make a note of the implementation method I did at that time.

environment

Eclipse4.8 Photon JavaEE6(Java7) JSF2.0 GlassFish 3.1.2.2 Windows10

ui: param tag

sample.xhtml


<h:body>
 <ui:param name="targetView" value="#{sampleView}"/>
  <!--abridgement-->
</h:body>

sample.java


@ManagedBean(name = "sampleView")
public class sample{
 public void setMoveKey(ActionEvent event){
  //Store key in session
 }
}

Add the ui: param tag to the Jsf page. Write the name of the managed bean in value. Add this to all screens where you want the key to be stored in the session at screen transition.

header.xhtml


<h:body>
 <h:form>
  <div>
   <ui>
    <li><h:commandLink value="sample"
         actionListener="#{targetView.setMoveKey}"
         action="Screen transition method(abridgement)"/></li>
  <li><h:commandLink value="Sample 2"
         actionListener="#{targetView.setMoveKey}"
         action="Screen transition method(abridgement)"/></li>
  <li><h:commandLink value="Sample 3"
         actionListener="#{targetView.setMoveKey}"
         action="Screen transition method(abridgement)"/></li>
  <!--abridgement-->
</h:body>

Add actionListener attribute to h: commandLink tag of screen transition Describe in the format of "# {name of ui: param. method name you want to call}.

That's it. When transitioning from the sample screen to the sample 2 screen, SetMoveKey of sampleView (ManagedBean of sample.xhtml) is executed before the screen transition method is called. When transitioning from the sample 2 screen to the sample screen, setMoveKey of sampleView2 (ManagedBean of sample2.xhtml) is now executed.

Recommended Posts

Dynamically call methods in JSF
About validation methods in JUnit
Helper methods available in devise
Test private methods in JUnit
Test private methods in JUnit
Mock static methods in Mockito 3.4
About methods often used in devise
Call Chain from Chain in Spring Integration
Differences in split methods of StringUtils
How to call classes and methods
Frequently used methods in Active Record
Make a SOAP call in C #
Ruby methods often used in Rails
Call the super method in Java