开发者

JSF 2.0 query string propagation

开发者 https://www.devze.com 2023-02-06 23:03 出处:网络
I\'ve a page that receives an id number via querystring and shows the relative article. I use <f:viewParams> with preRenderView event and the data are loaded from db in @PostConstruct method:

I've a page that receives an id number via querystring and shows the relative article.

I use <f:viewParams> with preRenderView event and the data are loaded from db in @PostConstruct method:

@Named
@RequestScoped
public class Bean {
    private Long id;

    @PostCostruct
    public void init() {
        if (this.id != null) {
            // load data from db
        }
    }

    public String modify() {
  开发者_JS百科      // update data
    }
}

When I call the page the data are correctly loaded but when I click modify button

<h:commandButton value="Modify" action="#{bean.modify}" />

I obtain an error because no query string parameter is sent to the bean and, thereby, no data are loaded.

How can I propagate query string parameters?


afaik, modify method should return an action string that ends with "?includeViewParams=true". Only then view params are propagated (it should, in theory, also work with faces-include-view-params, which hurts eyes less when paired with faces-redirect).

ADDENDUM: if you need (as stated in the comment) to use the parameter in your init method, then they are not - from JSF perspective - a real input. You do not get the validation, conversion etc. But if you just want to grab the raw value, use:

@ManagedParam("#{param.id}")
private String id;

// getter and setter for param goes here (obligatory!)

Id will be injected before calling init. Note that this is basically a way to go around viewparams. You could use some other type than String, but any conversion error would end in an exception, so it's really better to leave it as a string and do any conversion manually.

But the root of your problem seems a misuse of @PostConstruct; probably you want to fill your been in the prerender phase, roughly like so:

<f:event type="preRenderView" listener="#{beanThatNeedsId.init}"/>  
0

精彩评论

暂无评论...
验证码 换一张
取 消