20080

Use ManagedBean in FacesConverter

Question:

I want to use ManagedBean in my Converter. The ManagedBean is responsible for getting data from database. In Converter I want to convert string into object which must be get from database.

This is my Converter

@FacesConverter(forClass=Gallery.class, value="galleryConverter") public class GalleryConverter implements Converter { // of course this one is null @ManagedProperty(value="#{galleryContainer}") private GalleryContainer galleryContainer; @Override public Object getAsObject(FacesContext context, UIComponent component, String galleryId) { return galleryContainer.findGallery(galleryId); ... } @Override public String getAsString(FacesContext context, UIComponent component, Object gallery) { ... } }

I know that galleryContainer will be null and if I want to inject ManagedBean into Converter I can mark it as ManagedBean too. The problem is that I want to do it in beautiful way, I don't want to look for some 'strange solution'. Maybe the problem is in my application? Maybe there is some other good solution to create object which must get data from database and used in converter? I want also to mention that I will prefer to use DependencyInjection instead of creating new object using new statement (it is easier to test and maintain). Any suggestions?

Answer1:

Instead of using @FacesConverter you should use @ManagedBean, because currently faces converter isn't a valid injection target. Nonetheless, you can choose your converter to be a managed bean, thus refer to it in your view as converter="#{yourConverter}" (by managed bean name) instead of converter="yourConverter" (by converter id).

Basic usage example:

@ManagedBean @RequestScoped public class YourConverter implements Converter { @ManagedProperty... ... //implementation of converter methods }

Of course, reading BalusC's invaluable <a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters" rel="nofollow">Communication in JSF 2.0</a> will shed some light on this question as well.

It is also worth mentioning that the scope of your converter bean may be changed to, for example, application or session, if it is not supposed to hold any state.

Recommend

  • Fatal error when extending included class
  • requestAccessForMediaType doesn't ask for permission
  • Using sleep and select with signals
  • Combining javascript / jQuery and PHP
  • Laravel “envoy run” command not working with ssh key
  • Error message “Program does not contain a static 'Main' method suitable for an entry point
  • GitLab pod. Readiness probe failed: Get http://10.233.124.46:8005/health_check
  • Multi-users chat room data structure in Firebase Database
  • Perform division of a 22 digit number in lotus script
  • Connect SQL Server on button click VB.net
  • 128 bit string to array using boost::spirit::*
  • Jenkins Composite baseline with ClearCase
  • rownum issue in oracle query
  • Test an application behind a proxy server using Robot Framework and Selenium2Library
  • How to install or uninstall SonarQube plug-ins with HTTP?
  • ClearCase can I use clearexport_ccase/clearimport to copy VOB data to a VOB on a different machine
  • JSF with Enum 'Validation Error: Value is not valid'
  • CSS style for container with transparent border images
  • Is there a .NET method equivalent to NETWORKDAYS in Excel?
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • PowerShell script to pass SecureString to Plink as account and sudo passwords
  • How to find angle formed by the blades of a wind turbine with respect to a horizontal imaginary axis
  • AWS RDS Parameter Group not changing MySQL encoding
  • Pandas vs matplotlib datetime
  • Silverlight Event Log in Isolated Storage
  • How to define something in JavaScript [closed]
  • Neo4j…how to get a visual representation of my data?
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to decleare char *const argv[] in swift [duplicate]
  • Bitrate JWplayer
  • Grails - How to implement a foreign key relationship not using an id column?
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Accessing Arguments, Workflow Variables from custom activities
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • JavaScript RegExp Replace