0

I am submitting a page once an item from a list box as been selected as follows:

  onchange="submit()"

As soon as the selection is made, the page immediately scrolls to the top of the page with new information which depends on the selection.

Is there a way in jsf that scrolling from happening?

Best regards Edmond

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
edmond
  • 833
  • 3
  • 13
  • 31

1 Answers1

1

This is not specific to JSF. This is specific to HTML. When you let JSF generate a onchange="submit()" attribute on the generated HTML input/select element, you're basically instructing the HTML input/select element to invoke the submit function of the parent HTML <form> element. This will trigger a full synchronous request which causes a full page reload. The default behavior is that the top of the page will be shown, exactly like as when you open the page by a normal link or performs a refresh.

If you want to perform an asynchronous submit, just throw in some ajax. JSF 2.x has builtin ajax support via <f:ajax> tag.

So, instead of

<h:selectOneMenu ... onchange="submit()">
    ...
</h:selectOneMenu>

do

<h:selectOneMenu ...>
    ...
    <f:ajax />
</h:selectOneMenu>

In case you intend to update a specific component elsewhere in the same view, specify its absolute or relative client ID in the render attribute.

<h:selectOneMenu ...>
    ...
    <f:ajax render="results" />
</h:selectOneMenu>
<h:panelGroup id="results">
    ...
</h:panelGroup>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the precision. I will try to use ajax. – edmond Sep 18 '15 at 09:59
  • Which libraries are needed for JSF 2.0? – edmond Sep 18 '15 at 13:15
  • Is this an existing or new project? What's the target server make/version? JSF2 has been out since 2009 already so it would be quite surprising if it were a new project (this is a strong hint that your learning resources are totally outdated). If it's however an existing project, or if the target server is also that outdated, then upgrading to JSF2 may not be that trivial. – BalusC Sep 18 '15 at 13:18
  • Hi BalusC. have a look at my post below. The project has been around for very long time using JSF 1.x. Since you advice to solve the issue I first submitted is to use JSF 2.x, I have been trying to switch to JSF 2.0 – edmond Sep 18 '15 at 13:24
  • Since I'm using wildfly, I have remove all JSF librairies from my WEB-INF/libs except for tomahawk20-1.1.14.jar. And now I get the following error: javax.faces.FacesException: Cannot add the same component twice. – edmond Sep 21 '15 at 09:27
  • That's a different problem than being asked here. In case you couldn't figure out the answer based on exception message and stack trace (just copypasting the exception message and the 1st line of stack trace (without line number) into a search engine usually gives a lot of clues), just ask a new Question. Do not forget to include the entire stack trace, as that usually represents the whole answer at is own (which we then basically just translate into layman's terms). – BalusC Sep 21 '15 at 09:40
  • Is there a way to achieve that partial refreshing of the page with JSF 1.2? Actually I can't upgrade to JSF 2 since the platform I'm working is running for years and an upgrade will mean a tremendous amount of effort. – edmond Oct 30 '15 at 08:30
  • You could try Ajax4jsf 1.1 (not RichFaces). The `` is the predecesor of ``. Your problems are likely caused by classpath pollution. WildFly ships with builtin Mojarra 2.2.x. In order to downgrade it to 2.1.x, make sure you follow instructions in http://stackoverflow.com/questions/17085717/upgrade-jsf-mojarra-in-jboss-as-eap-wildfly – BalusC Oct 30 '15 at 09:01
  • Great Answer! It works just great. Thanks a lot BalusC – edmond Oct 30 '15 at 10:51
  • Is it valid if the render attribute references the id of a non-jsf tag (e.g. ` – user1156544 May 17 '16 at 14:19
  • Thanks! I guess the answer is: no, so wrap it inside a `` – user1156544 May 17 '16 at 14:23