问题
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:
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).
来源:https://stackoverflow.com/questions/10526431/fviewparam-with-converter-and-viewscoped-exception-out-when-invoke-the-second-a