How to dynamically inject parameters without having to inject target backingbean

Splash Forums PrettyFaces Users How to dynamically inject parameters without having to inject target backingbean

This topic contains 9 replies, has 2 voices, and was last updated by  Lincoln Baxter III 5 years, 2 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #18552

    nena_99
    Participant

    Hi There,

    I’m using JBoss 7.0 Final, Pretty faces 3.3.3, JSF2.1 and prime faces 3.3.1. I don’t have a pretty-config.xml file or anything specific to pretty faces in my web.xml and/or faces-config.xml. I simply use the @URLMapping annotations at the top of my backing beans.

    Now – onto my problem. The background is this: I have a combo-box that contains a set of menu links – from the current page in view, the user can navigate to any one of the actions in the combo-box and the application *should* navigate them to the desired location. My problem is this: in my backing bean, I have an action handler for each of the combo-box items – BUT I do not want to inject the target view’s backing bean into my current backing bean – because there are simply too many of these target URLs.

    My question is – is there an elegant way to send the user to a page without having to inject the target view/backing bean?

    For example, in current backing bean, I might have:

    public String handleEmployeeReportsNavigation() {

    return “pretty:employeeReports”; // HERE I would like to append or include somehow the ID of the employee in question

    }

    Any help/insight that you could provide would be most appreciated – thanks!

    #22800

    nena_99
    Participant

    Note: I tried something like the following:

    public String handleEmployeeReportsNavigation() {

    return buildURL(“pretty:employeeReports”, getId());

    }

    protected String buildURL(String mappingId, Object… params) {

    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

    PrettyContext context = PrettyContext.getCurrentInstance(request);

    PrettyURLBuilder builder = new PrettyURLBuilder();

    UrlMapping mapping = context.getConfig().getMappingById(mappingId);

    String targetURL = builder.build(mapping, true, params);

    }


    This returns a URL of the format: /employees/employeeReports/#id (replace #id with the actual ID) – however, the application does not navigate to the given resource. I’m not sure why – any help would be appreciated…

    Thanks

    #22801

    If you are using JSF2, I recommend that you just use a normal JSF2 navigation string, like:

    /pages/employeeReports.xhtml?id=" + id&faces-redirect=true

    This will trigger the same type of navigation that PrettyFaces would have done. We don’t currently support the type of navigation that you are trying to do using pretty: navigation strings. I agree it would be nice, but we will have to put it into PrettyFaces4, since 3.x is frozen for new features, and is only on bug fixes going forward.

    Let me know if that helps,

    Thanks!

    Lincoln

    #22802

    Also, in the future, if you could post your configuration (annotations or XML) that would help a lot!

    Cheers,

    Lincoln

    #22803

    nena_99
    Participant

    Thanks for replying. Can I use PrettyURLBuilder for this? I noticed that PrettyURLBuilder forces an ID at the end. For example, the targetURL that it builds is something like:

    /employees/employeeReports/#id

    Whereas, what you suggest as url would be something like /employees/employeeReports?id=#id&faces-redirect=true

    Also, I tried just tacking the ?id=#id&faces-redirect=true, eg my backing bean action handler returns:

    /employees/employeeReports/#id?id=#id&faces-redirect=true – however, the navigation did not redirect me to a different page for some reason. It just reloads the current page. For security reasons, I can’t post the actual source code. However,if you’d like more information, I can post modified pieces.

    #22804

    What’s the ‘#’ sign that you are including in your URL? I don’t think that’s a valid query-string parameter.

    #22805

    You have to specify the literal path to the XHTML file, you can also omit the suffix ‘.xhtml’ but you cannot end with a slash ‘/’

    #22806

    nena_99
    Participant

    Actually – Lincoln, I have come to the conclusion that returning a url with the parameters at the end isn’t very nice – it doesn’t keep the nice “clean” & bookmarkable URLS. So I have decided to do something like the following in the action handler:

    protected String navigate(String mappingId, Object… params) {

    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

    PrettyContext context = PrettyContext.getCurrentInstance(request);

    PrettyURLBuilder builder = new PrettyURLBuilder();

    UrlMapping mapping = context.getConfig().getMappingById(mappingId);

    String targetURL = builder.build(mapping, true, getId());

    HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();

    try {

    response.sendRedirect(request.getContextPath() + targetURL);

    } catch (IOException ioe) {

    logger.error(ioe.getMessage());

    }

    return null;

    }

    But – I was wondering, is there a PrettyRedirector for doing this? Could you possibly point me to an example? Thanks in advance!

    #22807

    nena_99
    Participant

    the # sign was just a “placeholder” – in the code, the #id was replaced with the actual ID.

    #22808

    I think I need to see how your project is laid out. This should be working and still create pretty URLs. Think you could paste your project layout / where your HTML files are, and also paste your mapping configuration?

    Thanks.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

Comments are closed.