pretty url broken

Splash Forums PrettyFaces Users pretty url broken

This topic contains 5 replies, has 2 voices, and was last updated by  Christian Kaltepoth 2 months, 3 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #27577

    hairi11
    Participant

    Hi.

    I got problem to show view page with pretty url after submit the form for saving data.

    after submit form, I navigate to view page using url no 2(actual) using ‘externalContext.redirect()’.

    1. edit page edit/PBB/8123

    2. after save & redirect ->
    expected: view/PBB/8123
    actual : view.xhtml?publicationId=8123&pubCategoryCode=PBB&faces-redirect=true

    Thanks.

    /Hairi

    #27578

    Please share your configuration, which dependencies your are using and the relevant parts of your code.

    #27579

    hairi11
    Participant

    Hi.

    Please see code snipped. thx

    <url-mapping parentId="ilibrary" id="publication-edit">
    	<pattern value="/edit/#{pubCategoryCode}/#{publicationId}" />
    	<view-id value="/pages/protected/isralib/publication/edit.xhtml" />
    </url-mapping>
    
    <url-mapping parentId="ilibrary" id="publication-view">
    	<pattern value="/view/#{pubCategoryCode}/#{publicationId}" />
    	<view-id value="/pages/public/isralib/publication/view.xhtml" />
    </url-mapping>
    
    <h:form id="edit" enctype="multipart/form-data">
    	<p:commandButton value="#{msg['global.btn.save']}"
    				 actionListener="#{bean.save}"
    				 ajax="false" styleClass="GreenButton">
    		<f:param name="pubCategoryCode" value="PBB"/>
    	</p:commandButton>
    </h:form>
    
    save() {
    	FacesContext facesContext = FacesContext.getCurrentInstance();
    	ExternalContext externalContext = facesContext.getExternalContext();
    	HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    	String url = request.getRequestURL().toString().replace(request.getRequestURI().substring(0), "") + request.getContextPath();
    	String path = "/pages/public/isralib/publication/view.xhtml?publicationId=8123&pubCategoryCode=PBB&faces-redirect=true";
    
    	try {
    	    externalContext.redirect(url + path);
    	} catch (IOException e) {
    	    getLogger().error("Redirect failed", e);
    	}
    
    }
    
    #27581

    Why are you using this weird way of redirecting via ExternalContext? Why not just:

    return "/pages/public/isralib/publication/view.xhtml?publicationId=8123&pubCategoryCode=PBB&faces-redirect=true";
    
    #27582

    hairi11
    Participant

    because I’m using actionListener=”#{bean.save}”.

    of course there is lots of operation in the save method.

    i just put a snipped of code

    #27583

    I guess you need something like this:

    String rewritten = externalContext.encodeRedirectURL( path, new HashMap<>() );
    externalContext.redirect(url + rewritten);
    

    Outbound URLs are rewritten using HttpServletResponse.encodeRedirectURL(), so if you are doing redirects manually, you will have to call this method yourself.

    BTW: I think you don’t need url in your code. Just do:

    externalContext.redirect(request.getContextPath() + rewritten);
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

Comments are closed.