Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:10:21+00:00 2026-06-03T16:10:21+00:00

I have this f:viewParam that I try to bind validate and convert a userId

  • 0

I have this f:viewParam that I try to bind validate and convert a userId into Player, and I got an unexpected results.

<f:metadata>
    <f:viewParam name="userId" value="#{myBean.selectedPlayer}" converter="pConverter"
         converterMessage="Bad Request. Unknown User" required="true"
         requiredMessage="Bad Request. Please use a link from within the system" />

</f:metadata>
<h:body>
    <p:messages id="msgs"/>        
    <h:form>
        <ul>
            <li><a href="index2.xhtml?userId=1">Harry</a></li>
            <li><a href="index2.xhtml?userId=2">Tom</a></li>
            <li><a href="index2.xhtml?userId=3">Peter</a></li>
        </ul>            
    </h:form>
    <h:form>
        <h:panelGrid columns="2" rendered="#{not empty myBean.selectedPlayer}">

            <h:outputText value="Id: #{myBean.selectedPlayer.id}"/>

            <h:outputText value="Name: #{myBean.selectedPlayer.name}"/>

        </h:panelGrid>
    </h:form>
    <h:form id="testForm">
        <h:inputText value="#{myBean.text}"/>
        <p:commandButton value="Switch" update=":msgs testForm"/>
        <h:outputText value="#{myBean.text}" rendered="#{not empty myBean.text}"/>
    </h:form>    
</h:body>

My Converter look like this

@FacesConverter(value="pConverter")
public class PConverter implements Converter {
private static final List<Player> playerList;
static{
    playerList = new ArrayList<Player>();        
    playerList.add(new Player(1, "Harry"));
    playerList.add(new Player(2, "Tom"));
    playerList.add(new Player(3, "Peter"));
}

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
    if(value == null || !value.matches("\\d+")){
        return null;
    }
    long id = Long.parseLong(value);
    for(Player p : playerList){
        if(p.getId() == id){
            return p;
        }
    }
    throw new ConverterException(new FacesMessage("Unknown userId: " + value));

}

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {
    if(!(value instanceof Player) || value == null){
        return null;
    }
    return String.valueOf(((Player)value).getId());
}
}

As I click the three link (Harry, Tom, Peter), the converter work great. It converter the id and bind the player back to my managed bean. I then type something in the text box, then click Switch, the first time it work fine, what I typed appear next to the button, but then I change what I type, and click Switch again, then error message appear Bad Request. Please use a link from within the system, which is the error message for required for f:viewParam. If I took the f:viewParam out then everything work fine. Surprisingly, if I switch from f:viewParam to o:viewParam (OmniFaces), then it work great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:10:23+00:00Added an answer on June 3, 2026 at 4:10 pm

    That’s because the <f:viewParam> runs on every single HTTP request, also on postbacks. It works in your case fine for plain GET links, because you’re passing exactly that parameter in the links. It fails in your case for POST forms, because you aren’t passing that parameter in the button. So it becomes null in the request parameter map and the required validator kicks in and hence this validation error.

    To keep the <f:viewParam required="true"> happy on POST forms as well, you basically need to retain the initial request parameter by <f:param> in the command buttons/links.

    <p:commandButton value="Switch" update=":msgs testForm">
        <f:param name="userId" value="#{param.userId}" />
    </p:commandButton>
    

    The OmniFaces <o:viewParam>, which is designed to be used in combination with view scoped beans, has an additional check in the isRequired() getter (source code here):

    @Override
    public boolean isRequired() {
        // The request parameter get lost on postbacks, however it's already present in the view scoped bean.
        // So we can safely skip the required validation on postbacks.
        return !FacesContext.getCurrentInstance().isPostback() && super.isRequired();
    }
    

    So, this skips the required validator on every postback (and additionally, it also skips setting the model value on every postback due to its stateless nature). That’s why you don’t see the validation error and you still have the proper model value (which isn’t reset on every postback).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have this software that has a webservices component. Now, the administrator of this
I have this DependencyProperty which holds an entity with a property that is a
I have this batch that needs to run that the user has to execute
Have this result set below; am trying to insert the red numbers into post_position
I have a JSF page that accepts a viewparam and sets a variable which
i have a project that is structured with a .net namespace called ViewParts. This,
I have this code in jQuery, that I want to reimplement with the prototype
I have a few pages that needs a userId to work, thus the following
I have problem firing an ajax request with Primefaces <f:metadata> <f:viewParam name=token value=#{clientBean.token}/> <f:event
I have following code for initializing bean values depending upon url parameter. <f:metadata> <f:viewParam

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.