views:

2378

answers:

2

As I debug my seam application, it dawns on me that I don't really understand how parameter passing works. The following terminology really has me confused. So I am asking this very general question in the hopes of getting a good explanation of what works with what and what certain things are for.

First of all, to get from one page to the next you can use h:commandButton or s:button. I understand that s:button does not submit a form, but that doesn't help me understand the difference. If you are not submitting a form by going from one page to the next, then what are you doing?

My app involves entering information in a form, hitting a button and then going to a new page that displays results after running a query. It seems I have seen this activity take place with s:button, so how is that if it's not "submitting a form"? I feel I'm missing something fundamental here.

As for the parameters themselves...from what I have seen you can pass parameters using one of 3 methods:

  1. f:param. This seems to occur more often in combo with s:button than h:commandbutton. Why is that?
  2. Also, you can "pass" (or something) parameters using your page.xml file. The parameter seems to have to appear in both the source page.xml and the target page.xml to make it show up in the URL.
  3. Last of all, there is the option of adding @RequestParameter annotation to your backing bean. I gather that is also used when you set f:param in your view. Does that mean the one in page.xml gets overlooked? I notice in the registration example of the seam distribution, the user bean gets populated without any parameters being passed via page.xml for f:param. How is that possible?

I'm sure this question reveals a great deal of ignorance.

Hopefully one of you eloquent people will "get" what I'm asking and give me an explanation of this process.

Thanks in advance.

TDR

+5  A: 

If you are not submitting a form by going from one page to the next, then what are you doing?

Navigating to another page without submitting any form fields.

My app involves entering information in a form, hitting a button and then going to a new page that displays results after running a query. It seems I have seen this activity take place with s:button

s:button won't submit the form, so the values on your page will not be applied to the model. You must use a commandButton/Link for this. The activity you may have seen is passing an already populated value to another page.

f:param

Used more often with s:button/link as these are often used for navigation. You can use f:param to pass an already populated value across to another page. h:commandButton/Link is used for submitting forms so the values are in form fields. Of course there is nothing stopping you from using f:param for this to.

page.xml

the params used here are for applying request parameters to the model and vice versa.

@RequestParameter

Can be used in conjunction with all of the above but is a little pointless when used with page.xml params as they can be used to do the same job

Example

If you start with this page:

http://mydomain.com/myapp/home.seam?name=damo

And the home.page.xml has:

<param name="name" value="#{person.name}"/>

Then when the page is loaded person.setName("damo") will be called as there is a matching request parameter in the URL.

You can store the value of the param in the link to the next page:

<s:link value="Go to Page 2" view="/page2.xhtml">
  <f:param name="name" value="#{person.name}"/>
</s:link>

When you click the link and navigate to http://mydomain.com/myapp/page2.seam And the page2.page.xml has:

<param name="name" value="#{someOtherBean.name}"/>

Then someOtherBean.setName("damo") will be called.

Page2 may have a s:button like this:

<s:button value="Say Hello" action="#{someOtherBean.sayHello}">
  <f:param name="subject" value="#{someOtherBean.name}"/>
</s:button>

And the method could be:

@Name("someOtherBean")
public class SomeOtherBean {

  @RequestParameter("subject")  //same value as the 'name' in f:param
  private String subject;

  public void sayHello() {
    System.out.println("Hello "+subject);
  }
}

Hope this helps!

Damo
Thank you, Damo, it certainly does.
april26
Clear explanation about the parameters on seam, thanks for that!
Kamia
A: 

Still missing something: what if I want to submit a page and, with the same button, I want to navigate passing a parameter? It seems to me that passing parameters with h:commandButton doesn't work!

Faffinito
Do you use f:param within the h:commandButton? I don't know if it's supposed to work or not. BTW for anyone interested, I posted the same question on the seam forum and got another good answer.http://seamframework.org/Community/UnderstandingSbuttonHcommandButtonAndRequestParameter
april26