0

I've found a lot of topics discussing how to inject property but none of them suggests validation method. Here is my bean:

@ManagedBean
@RequestScoped
public class MyBean {       
    @ManagedProperty(value = "#{param.key}")
    private String keyFromUser;
}

Currently if param.key is missing among GET params I have

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean myBean
alehro
  • 2,198
  • 2
  • 25
  • 41

2 Answers2

2

Use <f:viewParam> instead. It's like <h:inputText>, but then for GET request parameters. It thus allows for registering validators by validator attribute or even <f:validator> and <f:validateXxx> tags. You can even attach a <h:message> to it.

<f:metadata>
    <f:viewParam id="key" name="key" value="#{myBean.keyFromUser}" validator="myValidator" />
</f:metadata>
<h:message for="key" />

You only need to move the @PostConstruct job to <f:event type="preRenderView">.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I have used JSF-beans pretty little so I must say I don't understand your functional requirement. Perhaps annotate a method @PostConstruct and do validation there? That's were I put stuff that needs to be done after depedency injection is resolved

As a side note that does not answer the question directly I think CDI is very strong for scenarios like this.

Would look something like:

@Inject
@RequestParam (validator = MyValidator.class)

if you are interested I can supply the actual full implementation

Karl Kildén
  • 2,415
  • 22
  • 34
  • I'm kind of beginner in JSF too. The funny thing was that I already had @PostConstruct method. But the exception description is kind of misleading, so, I didn't even look on stacktrace. Thank you. – alehro Oct 07 '12 at 19:04
  • If you are beginning and if you like annotations, Consider looking closer at CDI. Glad I could help – Karl Kildén Oct 07 '12 at 19:11