PrettyFaces + Primefaces url trouble

Splash Forums PrettyFaces Users PrettyFaces + Primefaces url trouble

This topic contains 8 replies, has 3 voices, and was last updated by  Lincoln Baxter III 4 years, 6 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #18970

    hgkkjh
    Member

    I want to create the url like:

    http://example.com/system/grupo/20

    To edit the entity with id 20 .

    But I have the following:

    <p:commandButton id="edit" icon="ui-icon-pencil" action="#{grupoController.getFormPath}" ajax="false">
    <f:setPropertyActionListener value="#{entity}" target="#{grupoController.entity}" />
    </p:commandButton>

    So, when the user clicks on the edit button, we set the var using setPropertyActionListener….

    pretty-config:

    <url-mapping id="grupoEditor">
    <pattern value="/grupo/#{ grupoController.entity.id }" />
    <action></action>
    <view-id value="/view/grupo/form.jsf" />
    </url-mapping>

    It works, but it doesn´t load the object, because that is done by clicking on the button.

    How can i make this work?

    thanks

    #23178

    I see you have specified an <action></action> configuration, but you are not calling a method. This method should be used to load your data:

    <action>{ grupoController.loadEntity}</action>

    Hope this helps,

    ~Lincoln

    #23179

    hgkkjh
    Member

    Sorry Lincoln thats not it.

    the empty action tag wass my fault. lol

    forget about that.

    As i said, the data is beeing loaded when the user clicks on the button . :(

    i don´t have an action to load the object. thanks!

    #23180

    You write that you want an URL like this:

    http://example.com/system/grupo/20

    In this case you have to implement some method that loads the corresponding data from the database. Think of a case where somebody bookmarks this URL and loads it again after some minutes. In this case there is no button that is clicked.

    PrettyFaces is all about building URLs that work independently from each others. You will have to do a bit of refactoring of you code if you want such pretty URLs.

    Please have a look at this post I wrote some time ago, which explains the general pattern for implementing such URLs:

    http://ocpsoft.org/support/prettyfaces-users/prettyfaces-hibernate-spring-lazy-initialization#post-3355

    I hope this helps. :)

    Christian

    #23181

    hgkkjh
    Member

    I really didn´t want to change the structure of my code…

    But its better to do that, than having some ugly URLs :)

    let me just ask you something about the post you showed to me.

    @URLAction
    public void init() {
    }

    Doesn´t all actions in JSF return a “String”?

    So that is not simular to the action atribute, it is similar to the actionListener attribute.

    Another thing, i really need to use pretty link and f:param?

    <pretty:link mappingId="editUser">
    <f:param value="#{user.id}" />
    Edit
    </pretty:link>

    Can’t I just do like this:

    <p:commandButton id="edit" icon="ui-icon-pencil"
    action="#{grupoController.getFormPath}" actionListener="#grupoController.loadObject(entity.id)" ajax="false">
    </p:commandButton>

    and them i would have:

    public void loadObject(Integer id){
    // loads object here.
    }

    would that work in this configuration:

    <url-mapping id="grupoEditor">
    <pattern value="/grupo/#{ grupoController.entity.id }" />
    <view-id value="/view/grupo/form.jsf" />
    <action>{ grupoController.loadObject}</action>
    </url-mapping>

    or that is not the right/better way to do it?

    #23182

    I know Christian prefers annotations, I think it’s better to do everything in XML. the way you have it configured now looks good

    #23183

    hgkkjh
    Member

    <action>{ grupoController.loadObject}</action>

    pretty faces will call the method with the right ID number?

    #23184

    Yes, the post I referred to uses annotations instead of the XML configuration. You can choose which way you prefer. :)

    Regarding your question:

    @URLAction
    public void init() {
    // something
    }

    That’s not a JSF action method. It’s a method that gets invoked by PrettyFaces when the user accesses the pretty URL. So the method doesn’t require a return value.

    And no, in your setup loadObject won’t be called with the ID. The id will be injected into the bean property you are referencing with your pattern. So in your case you could do something like this.

    <url-mapping id="grupoEditor">
    <pattern value="/grupo/#{ grupoController.id }" />
    <action>#{ grupoController.loadObject}</action>
    <view-id value="/view/grupo/form.jsf" />
    </url-mapping>

    And a bean like this:

    public class GrupoController {

    private Long id;

    private Grupo grupo;

    public void loadObject() {
    this.grupo = someDao.loadById(id);
    }

    /* getters + setters */

    }

    You should have a look at this chapter of the PrettyFaces configuration:

    http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html

    #23185

    No, PrettyFaces expects a method with no arguments, but the ID will already have been set in the entity. You could *try* this:

    <action>{ grupoController.loadObject(grupoController.entity.id)}</action>

    But no guarantees if it will work or not… I doubt it will.

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

You must be logged in to reply to this topic.

Comments are closed.