Conditional HTTP 404 response, only with validation?

Splash Forums PrettyFaces Users Conditional HTTP 404 response, only with validation?

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by  Zhomart 6 years, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17916

    Zhomart
    Participant

    Hi guys,

    Just downloaded PrettyFaces 3.2.0, seems very clean & simple solution, but faced one thing, hope you guys could help me with it:

    I’ve got following classical case:

    http://website/product#{No} “Where #{No} is some number (ID)”

    So, my configs should contain something like this:

    <url-mapping id=”product-page”>

    <pattern value=”/product#{pid : backing.pid}” />

    <view-id>…</view-id>

    </url-mapping>

    Since, one of the main aims of the pretty faces is SEO-friendliness, I wish there could be some convenient way of giving 404 response on non-existing product ID’s.

    After reading docs, I came up with following:

    1) <action>#{backing.loadItem}</action>

    But, it’s not possible, as I know, to bind some outcome to 404, without redirecting to some “fake” outcome, which would result in redirection to non-existing page.

    2) adding validation, the problem is validation is done before values are applied to the beans, it would be inconvenient workaround to prevent “product double-loading”.

    Any suggestions?

    #20883

    PrettyFaces allows to send custom HTTP response codes using the PrettyContext class. Here is a simple example that shows how I solve the validation issue in my projects:

    <url-mapping id="report">
    <pattern value="/report/#{reportBean.id}/details" />
    <view-id>...</view-id>
    <action>#{reportBean.loadData}</action>
    </url-mapping>

    @Named
    @RequestScoped
    public class ReportBean {

    private Long id;

    @Inject
    private ReportDao reportDao;

    @URLAction
    public void loadData() {

    Report report = reportDao.getById(id);
    if(report == null) {
    PrettyContext.getCurrentInstance().sendError(404);
    return;
    }

    // more stuff here

    }

    /* Getters + Setters */

    }

    I hope this helps! :)

    #20884

    Zhomart
    Participant

    Thanks a lot!

    That’s awesome!

    #20885

    Sounds like you like this feature! :)

    #20886

    Zhomart
    Participant

    Yep, it is exactly what I wanted.

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

You must be logged in to reply to this topic.

Comments are closed.