Iceface 2.0 commandLink 部分提交不起作用

发布于 2024-10-04 17:51:19 字数 1309 浏览 4 评论 0原文

我有一个页面,它接受地点的请求参数,然后生成信息, 例如,http://example.com/xxx/weather.jsf?place=california。 这样做的目的是让用户为链接添加书签。

在weather.jsf 中,有两个输出文本和一个命令链接:

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

在managementBean 中:

@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();
 }
}

好的,页面生成得很完美。 但是,当我单击 doLike commandLink 时, 它总是先触发 init 方法,然后调用 doLike 方法。 由于请求参数为空,所有其他值都会重置。 有什么方法可以防止刷新页面或调用 init 方法吗? 我尝试了部分提交或立即提交,但没有运气。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2024-10-11 17:51:19

您的 bean 是 @RequestScoped,因此在执行 JSF 生命周期后,您的 bean 实例将丢失,直到下一个请求到来,此时您将获得 bean 的新实例,并且 PostContruct 重新执行。

尝试将 bean 的范围更改为寿命更长的内容,例如 @ViewScoped。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文