JSF - selectonemenu (nullPointer) - jsf

Form snippet:
<h:selectOneMenu id="supervisor" value="#{newTopic.supervisor}">
<s:objectConverter/>
<f:selectItems value="#{supervisors}" var="_supervisor"
itemValue="#{_supervisor}"
itemLabel="#{_supervisor.name.firstName} #{_supervisor.name.lastName}"></f:selectItems>
</h:selectOneMenu>
<h:commandButton action="#{newTopicAction.createTopic}" value="Create topic"/>
Controller action:
#Model
public class NewTopicAction implements Serializable{
#Inject
private TopicManager topicManager;
private ThesisTopic newTopic;
public String createTopic(){
topicManager.createTopic(newTopic);
return "topics?faces-redirect=true";
}
#PostConstruct
public void init(){
newTopic = new ThesisTopic();
}
#Produces
#Model
public ThesisTopic getNewTopic(){
return newTopic;
}
}
Why isn't newTopic.supervisor populated with selected supervisor, when newTopicAction.createTopic is invoked? Every other field works as expected, except this one. It may be something about selectOneMenu what i don't understand properly.

Related

JSF websocket update viewScope

I have a bean named sampleBean scoped by viewScope .
this bean load some data from database (MySQL) .
my problem is some records shared between users .
now maybe [USER A] delete that shared record and i want to update view of other users .
I can not change scope to ApplicationScope because all records shared to all users .
How can fix this problem ?
Note : I read this post but can not understand how can fix this problem .
Note : I use Liberty 18.0.0.4 by JavaEE 8 (webProfile)
I fixed problem by this simple code . (I shared this code for you)
public class Information {
private String name ;
private String family ;
// constructor
// Getter & Setter
// override equal and hashCode
}
This is a simple service . (I simulated database on this class)
#Stateless
public class InformationService {
private static final List<Information> db = new ArrayList<>();
#Inject
#Push(channel = "infoChannel")
PushContext push;
#PostConstruct
public void init() {
Information userA = new Information("John", "Vankate");
Information userB = new Information("Julius", "Sampao");
db.add(userA);
db.add(userB);
}
public void remove(Information info) {
db.remove(info);
push.send("deleteInfo");
}
public List<Information> findAll() {
return db;
}
}
and simple JaxRs resources :
#Path("/info")
#RequestScoped
public class InformationResources {
#EJB
private InformationService informationService;
#Path("/delete")
#POST
#Consumes("application/json")
public String send(Information information) {
informationService.remove(information);
return "Receive : " + information;
}
}
Now Start JSF :
#Named
#ViewScoped
public class InformationBean implements Serializable {
private Information info ;
private List<Information> informationList ;
#EJB
private InformationService informationService ;
#PostConstruct
public void init() {
informationList = informationService.findAll();
info = new Information() ;
}
public void deleteInformation() {
informationService.remove(info);
}
public Information getInfo() {
return info;
}
public void setInfo(Information info) {
this.info = info;
}
public List<Information> getInformationList() {
return informationList;
}
public void setInformationList(List<Information> informationList) {
this.informationList = informationList;
}
}
and xhtml :
<h:body>
<p:dataTable value="#{informationBean.informationList}" var="info" id="infoTable">
<p:column rowHeader="name">
<h:outputText value="#{info.name}"/>
</p:column>
<p:column rowHeader="family">
<h:outputText value="#{info.family}"/>
</p:column>
<p:column rowHeader="action">
<h:form>
<p:commandButton value="Delete" action="#{informationBean.deleteInformation}">
<f:setPropertyActionListener value="#{info}" target="#{informationBean.info}"/>
</p:commandButton>
</h:form>
</p:column>
</p:dataTable>
<hr/>
<f:websocket channel="infoChannel">
<p:ajax event="deleteInfo" update="infoTable"/>
</f:websocket>
</h:body>
I already thought , PushContext must implemented on JSF beans , Now I understand can implement that in service or business logic layer .
Now you can remove information from JaxRs (Rest API) and record removed from p:dataTable without refresh page .
Note : this example use #ViewScoped

How to create a new object instance in jsf? [duplicate]

Does it make sense to use Entities as JSF Backing Beans?
#Entity
#ManagedBean
#ViewScoped
public class User {
private String firstname;
private String lastname;
#EJB
private UserService service;
public void submit() {
service.create(this);
}
// ...
}
Or is it better to keep them separately and transfer the data from the backing bean to the entity at the end?
#ManagedBean
#ViewScoped
public class UserBean {
private String firstname;
private String lastname;
#EJB
private UserService service;
public void submit() {
User user = new User();
user.setFirstname(firstname);
user.setLastname(lastname);
service.create(user);
}
// ...
}
You could do so. It's technically possible. But it does (design)functionally not make any sense. You're basically tight-coupling the model with the controller. Usually the JPA entity (model) is a property of a JSF managed bean (controller). This keeps the code DRY. You don't want to duplicate the same properties over all place, let alone annotations on those such as bean validation constraints.
E.g.
#ManagedBean
#ViewScoped
public class Register {
private User user;
#EJB
private UserService service;
#PostConstruct
public void init() {
user = new User();
}
public void submit() {
service.create(user);
}
public User getUser() {
return user;
}
}
with this Facelets page (view):
<h:form>
<h:inputText value="#{register.user.email}" />
<h:inputSecret value="#{register.user.password}" />
<h:inputText value="#{register.user.firstname}" />
<h:inputText value="#{register.user.lastname}" />
...
<h:commandButton value="Register" action="#{register.submit}" />
</h:form>
See also:
What components are MVC in JSF MVC framework?
JSF Controller, Service and DAO
Contradictory explanations of MVC in JSF
JSF 2 reusing the validation defined in the JPA entities?
why shouldn't entity bean be managed by JSF framework?

For exact same JSF EL expression, getter is being called for ouputText but not for inputText. Why?

This is my JSF pages
<h:form>
First Name: <h:inputText value="#{userBean.first}" valueChangeListener="#{userBean.updateLastName}" onblur="submit()"/><br/>
Last Name: <h:inputText value="#{userBean.last}"/><br/>
Last Name: <h:outputText value="#{userBean.last}"/><br/> </h:form>
Following is the managed bean code.
#Named
#RequestScoped
public class UserBean {
private Map<String, String> names;
private String first, last;
public UserBean() {
names = new HashMap<>();
names.put("first1", "last1");
names.put("first2", "last2");
}
public String getFirst() {return first;}
public void setFirst(String first) {this.first = first;}
public String getLast() {return last;}
public void setLast(String last) {this.last = last;}
public void updateLastName(ValueChangeEvent e){
last = names.get(e.getNewValue().toString());
FacesContext.getCurrentInstance().renderResponse();
}
}
Now, when I type "first1" on the FirstName field and tab out, I see "last1" on the outputText field but inputText field remains empty.
I did the debugging and found getter is being called only once, so I wonder why getter is not being called for the inputText? Considering EL Expression is exactly same and based on my knowledge getter should be called during Render Response phase???

JSF 2 <f:selectItems> not recognizing itemLabel and itemValue attributes

I have the following dropdown in a Facelets page:
<h:selectOneMenu value="#{contactBean.selectedContact}" converter="#{contactConverter}">
<f:selectItems value="#{contactsHolder.contacts}" var="contact"
itemLabel="#{contact.firstName}" itemValue="#{contact}" />
</h:selectOneMenu>
The problem is, no matter what I put in for itemLabel (JSF EL expression or just plain text), it doesn't display. Any idea what I'm doing wrong?
Here's ContactConverter:
#ManagedBean(name = "contactConverter")
#RequestScoped
public class ContactConverter implements Converter, Serializable {
#ManagedProperty(value = "#{contactsHolder}")
private ContactsHolder contactsHolder;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return contactsHolder.getContacts().get(value);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((Contact) value).getContactID();
}
//getter & setters...
}
And ContactsHolder:
#ManagedBean
#SessionScoped
public class ContactsHolder implements Serializable {
private Map<String, Contact> contacts;
#PostConstruct
public void init() {
contacts = new LinkedHashMap<String, Contact>();
//get Contacts data and populate map...
}
//getters & setters...
}
You're feeding a Map<String, Contact> to <f:selectItems value>. Each item in var attribute will be a Map.Entry<String, Contact> which has only getKey() and getValue() methods returning the String map key and Contact map value respectively. The Map.Entry class indeed doesn't have a getFirstName() method.
Fix it accordingly:
<f:selectItems value="#{contactsHolder.contacts}" var="contact"
itemLabel="#{contact.value.firstName}" itemValue="#{contact.value}" />
Or, if you target a Servlet 3.0 / EL 2.2 capable container which allows invoking non-getter methods, so that you can use Map#values() to get a Collection<Contact>:
<f:selectItems value="#{contactsHolder.contacts.values()}" var="contact"
itemLabel="#{contact.firstName}" itemValue="#{contact}" />
Or, make the #{contactsHolder.contacts} a List<Contact> instead so that your initial view code will work:
<f:selectItems value="#{contactsHolder.contacts}" var="contact"
itemLabel="#{contact.firstName}" itemValue="#{contact}" />

#ManagedProperty - Inject one request scoped bean into another request scoped bean

I have this SearchBean:
#ManagedBean(name = "searchBean")
#RequestScoped
public class SearchBean implements Serializable
{
private String input = null;
// getter methods
public String getInput() {
return input;
}
// setter method
public void setInput(String input) {
this.input = input;
}
public String Submit() {
return null;
}
}
Can I inject it into another bean using #ManagedProperty. For example:
#ManagedBean(name = "bookBean")
#RequestScoped
public class BookBean implements Serializable
{
#ManagedProperty(value = "#{searchBean}")
private SearchBean searchBean;
#PostConstruct
public void init()
{
System.out.println("Value: " + searchBean.getInput());
}
public SearchBean getSearchBean() {
return searchBean;
}
public void setSearchBean(SearchBean searchBean) {
this.searchBean = searchBean;
}
}
And the Facelet (search.xhtml):
<h:form id="formSearch">
<h:commandButton value="Search" action="#{searchBean.Submit}" />
</h:form>
UPDATE: I have search.xhtml inserted into book.xhtml via a ui:insert component as follow:
<h:form id="formBooks">
<ui:insert name="search">
<ui:include src="/templates/common/search.xhtml"/>
</ui:insert>
</h:form>
The searchBean.getInput() method above should return a value as a result of a form's submission. Is the above method of injection possible?
I assume that SearchBean.input will be bound to an input field:
public class SearchBean implements Serializable {
private String input = null;
Something like this:
<h:inputText value="#{searchBean.input}" />
If so, then this will be null:
#PostConstruct
public void init()
{
System.out.println("Value: " + searchBean.getInput());
}
But, assuming a value has been set, it will not be null when this method is invoked:
public String Submit() {
return null;
}
Image from Richard Hightower's JSF for nonbelievers: The JSF application lifecycle.
The reason is due to how the JSF lifecycle works:
When #{searchBean...} is first resolved and found not to exist:
The bean is instantiated
Any dependency injections are performed (there aren't any in this case)
#PostConstruct method is invoked
The bean is placed into scope
Assuming the Apply Request Values and Validations phases succeed, SearchBean.setInput(String) is invoked in the Update Model Values phase
SearchBean.Submit() is invoked in the Invoke Application phase
This process is defined in the JSF specification.
Now, if SearchBean.input were injected directly from the parameter map, it would not be null during #PostConstruct:
#ManagedProperty(value = "#{param.someParamName}")
private String input;
However, there aren't any real advantages to this - you're skipping any input validation and you can't use SearchBean.input as a field binding because it will be overwritten in the Update Model Values phase.
The SearchBean.Submit() method is where your application logic for performing the search should go.

Resources