Redirect to current view with updated parameter

Splash Forums PrettyFaces Users Redirect to current view with updated parameter

This topic contains 3 replies, has 2 voices, and was last updated by  Lincoln Baxter III 1 year, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26447

    martino
    Participant

    Hey!

    I’m afraid I don’t completely understand the JSF environment, as I’ve been struggling to implement URL parameter navigation with JSF and PrettyFaces in a maven JavaEE6 TomEE project. I have a primefaces menu model which has items dynamically generated from DB-rows. These items have attached a command for switching to a Course parameter, like this;

     for (Course course : courseList) {
                DefaultMenuItem item = new DefaultMenuItem(course.getName());
                item.setIcon("ui-icon-home");
                item.setCommand("#{controllerCoursesMenu.switchToCourse(\"" + course.getName() + "\")}");
                item.setAjax(false);
                model.addElement(item);
           }

    The function looks like this:

    public String switchToCourse(String coursename) {
            Course courseRequest = this.getControllerUser().getUser().findCourseParticipationByName(coursename);
            if (courseRequest != null) {
                this.getControllerUser().setUserCourseParticipation(courseRequest);
                
                return "pretty:";
            } else {
                return null;
            }
        }

    Now, when I press the items the default (first value) overwrites the value of the item I’m pressing.
    It works when I just do “pretty:course”, but then it redirects to the parent viewId named course. I have lots of ‘child’views in the prettyconfig. Say I am inside /course/coursename/module, when I press one of the navigation items I want to end up at /course/coursename2/module, as opposed to just /course/coursename2.

    This is the action method for the parent viewId;

    public String checkCourseRequest() {
            Map<String, String> params = FacesContext.getCurrentInstance().
                    getExternalContext().getRequestParameterMap();
            String courseRequestParameter = params.get("course");
            Course currentCourseParticipation = this.getControllerUser().getUserCourseParticipation();
            requestedCourse = this.getControllerUser().getUser().findCourseParticipationByName(courseRequestParameter);
            if (requestedCourse != null) {
                if (currentCourseParticipation.getIdCourse().equals(requestedCourse.getIdCourse())) {
                    return null;
                } else {
                    this.getControllerUser().setUserCourseParticipation(requestedCourse);
                    return null;
                }
            }
            return "pretty:login";
        }

    Don’t know if it matters, but the controllerUser class, which the courses is retrieved via, is SessionScoped, and the code included here is in a requestscoped class named controllerCoursesMenu.

    This is the url-mapping;

    <url-mapping id="course">
            <pattern value="/course/#{course : controllerUsers.userCourseParticipation.name}" />
            <view-id value="/faces/home.xhtml" />
            <action>#{controllerCoursesMenu.checkCourseRequest}</action>
    </url-mapping>

    So basically I’d like to update the parameter to the current course when navigating, then redirect to the same view, just with an updated URL parameter.
    I’d appreciate any advice on the matter! Thanks

    martino

    • This topic was modified 1 year, 10 months ago by  martino.
    • This topic was modified 1 year, 10 months ago by  martino.
    #26463

    This can probably be solved simply by using the org.ocpsoft.rewrite.faces.navigate.Navigate utility:

    public String switchToCourse(String coursename) {
            Course courseRequest = this.getControllerUser().getUser().findCourseParticipationByName(coursename);
            if (courseRequest != null) {
                this.getControllerUser().setUserCourseParticipation(courseRequest);
                
                return Navigate.to("/faces/home.xhtml").with("course", coursename).build();
            } else {
                return null;
            }
        }
    }
    #26465

    martino
    Participant

    Thank you! That worked brilliantly. 🙂

    #26469

    Glad this helped! Thanks for letting me know that it worked 🙂 In general, you can use standard JSF navigation strings (which is all this class is building for you.) PrettyFaces/Rewrite will figure out what you meant and do the right thing. This is the safest way to navigate and keep code JSF-standard the functionality of JSF.

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

You must be logged in to reply to this topic.

Comments are closed.