Search code examples
jsfjsf-2icefaces

Iceface 2.0 commandLink partial submit doesn't work


I have a page which takes in request params for place, then generate information, for example, http://example.com/xxx/weather.jsf?place=california. The purpose of doing this is to let user bookmark the link.

In the weather.jsf, there are two outputtext and a commandlink:

Humidity : <ice:outputText value="#{weatherBean.humidity}"/>
Visibility : <ice:outputText value="#{weatherBean.visibility}"/>
<ice:commandLink id="likeButton" 
  value="Like"
  actionListener="#{weatherBean.doLike}" />

In the managedBean:

@ManagedBean(name="weatherBean")
@RequestScoped
public class WeatherBean
{
 String humidity;
 String visibility;
 int numLike; 

 @PostConstruct
     public void init()
 {
  System.out.println("init called");
  HttpServletRequest request= (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
  String place = request.getParameter("place");
  setHumidity(WeatherDao.getHumidity(place));
  setVisibility(WeatherDao.getVisibility(place));
  setNumLike(GeneralDao.getNumLike());
 }

 public void doLike(ActionEvent event)
 {
  System.out.println("doLike called");
  GeneralDao.addNumberLike();
 }
}

Alright, the page generated perfectly. However, when I click the doLike commandLink, it always triggers the init method first, then call doLike method. Since the request param is empty, all the other values reset. Is there any way to prevent a refresh of the page or calling of init method? I tried partialsubmit or immediate, but no luck.


Solution

  • Your bean is @RequestScoped, so after executing the JSF lifecycle, your bean instance is lost, until the next request comes in, at which point you get a new instance of your bean, and the PostContruct re-executes.

    Try changing the scope of your bean to something longer lived, like @ViewScoped.