How to enforce jsf to create new instance of bean instead of throwing NullPointerException?
I'm almost sure that I do something wrong and thus the question's title is a bit incorrect.
I have a form with several fields for creating a new User-objects (fields like login, password, birthday etc). And I have 2 buttons - Cancel and Create. I didn't finish Create yet :) , but when I press Cancel I see NullPointerException. Here is simplified code:
<f:view>
<h:form>
<h:panelGroup layout="block">
<h:inputText id="add_login" value="#{userSupport.user.login}"/>
<h:inputSecret id="add_password" value="#{userSupport.user.password}"/>
<h:inputText id="add_name" value="#{userSupport.开发者_如何学编程user.name}"/>
<h:inputText id="add_surname" value="#{userSupport.user.surname}"/>
<h:commandButton value="Cancel" action="cancel"/>
</h:panelGroup>
</h:form>
</f:view>
UserSupport class has field user
with getter and setter and some other methods. It's a Spring bean with Session scope.
When I press cancel, I see NPE because jsf tries to save values from inputs in user-object, but user-object is null.
What is the correct way of doing this?
on your cancel button set immediate="true"
This will skip all the "unwanted" phases.
The same problem, however, will arise with your "create" button, and you shouldn't use immediate
there.
Better initialize the user
property - either in constructor, or using the @PostConstruct
annotation on a method, which does the initialization actions.
精彩评论