与查询参数关联的转换/验证失败时执行重定向

Tin*_*iny 5 jsf primefaces jsf-2.2

以下是一个简单的用例<f:viewAction>.

<f:metadata>
    <f:viewParam name="id" value="#{testManagedBean.id}" maxlength="20"/>
    <f:viewAction action="#{testManagedBean.viewAction}"/>
</f:metadata>
Run Code Online (Sandbox Code Playgroud)

涉及的托管bean.

@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id; //Getter and setter.

    public void viewAction() {
        System.out.println("viewAction() called : " + id);
    }
}
Run Code Online (Sandbox Code Playgroud)

该参数id通过URL传递.当xxx通过相关URL传递非数字值并且未调用viewAction()与侦听器关联的方法时,存在转换错误<f:viewAction>.

的值idnull在这种情况下.我想重定向到另一个页面,当id不能转换为所需的目标类型(如本例中)或未id根据指定的验证标准进行验证,以避免可能在LazyDataModel#load()PrimeFaces或某处的方法中抛出的潜在异常只要在相应的托管bean中尝试访问这些参数,就会在关联的托管bean中执行else.为此,viewAction()应该调用该方法.

如何处理?我应该用吗?

<f:event type="preRenderView">
Run Code Online (Sandbox Code Playgroud)

<f:viewAction>?一起

Bal*_*usC 8

这是指定的行为.当PROCESS_VALIDATIONS阶段以验证失败结束时,将跳过UPDATE_MODEL_VALUESINVOKE_APPLICATION阶段.与"常规"形式完全一样<h:form>.把它想象<f:viewParam>成a <h:inputText>和a <f:viewAction>作为a <h:commandButton>,它会变得更加清晰.

对于您的特定要求,在转换/验证失败时执行重定向,至少有3个解决方案:

  1. 当你发现时,添加一个<f:event listener>.我宁愿挂钩postValidate事件而不是更好的自我可记录性.

    <f:metadata>
        <f:viewParam name="id" value="#{bean.id}" maxlength="20" />
        <f:event type="postValidate" listener="#{bean.redirectIfNecessary}" />
        <f:viewAction action="#{bean.viewAction}" />
    </f:metadata>
    
    Run Code Online (Sandbox Code Playgroud)
    public void redirectIfNecessary() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();
    
        if (!context.isPostback() && context.isValidationFailed()) {
            context.getExternalContext().redirect("some.xhtml");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    检查FacesContext#isPostback()可防止在同一视图(如果有)中对"常规"表单的验证失败执行重定向.


  2. 扩展内置函数,LongConverter从而执行重定向getAsObject()(验证器是不可或缺的,因为默认转换器Long已经在非数字输入上失败;如果转换器失败,验证器永远不会被触发).然而,这是糟糕的设计(紧耦合).

    <f:metadata>
        <f:viewParam name="id" value="#{bean.id}" converter="idConverter" />
        <f:viewAction action="#{bean.viewAction}" />
    </f:metadata>
    
    Run Code Online (Sandbox Code Playgroud)
    @FacesConverter("idConverter")
    public class IdConverter extends LongConverter {
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || !value.matches("[0-9]{1,20}")) {
                try {
                    context.getExternalContext().redirect("some.xhtml");
                    return null;
                }
                catch (IOException e) {
                    throw new FacesException(e);
                }
            }
            else {
                return super.getAsObject(context, component, value);
            }
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

    如果需要,您可以在<f:attribute>内部<f:viewParam>使用"传递"参数到转换器.

    <f:viewParam name="id" value="#{bean.id}" converter="idConverter">
        <f:attribute name="redirect" value="some.xhtml" />
    </f:viewParam>
    
    Run Code Online (Sandbox Code Playgroud)
    String redirect = (String) component.getAttributes().get("redirect");
    context.getExternalContext().redirect(redirect);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建一个自定义标记处理程序,它与基本相同,<f:event listener>但不需要额外的辅助bean方法.

    <html ... xmlns:my="http://example.com/ui">
    
    <f:metadata>
        <f:viewParam name="id" value="#{bean.id}" maxlength="20" />
        <my:viewParamValidationFailed redirect="some.xhtml" />
        <f:viewAction action="#{bean.viewAction}" />
    </f:metadata>
    
    Run Code Online (Sandbox Code Playgroud)

    com.example.taghandler.ViewParamValidationFailed

    public class ViewParamValidationFailed extends TagHandler implements ComponentSystemEventListener {
    
        private String redirect;
    
        public ViewParamValidationFailed(TagConfig config) {
            super(config);
            redirect = getRequiredAttribute("redirect").getValue();
        }
    
        @Override
        public void apply(FaceletContext context, UIComponent parent) throws IOException {
            if (parent instanceof UIViewRoot && !context.getFacesContext().isPostback()) {
                ((UIViewRoot) parent).subscribeToEvent(PostValidateEvent.class, this);
            }
        }
    
        @Override
        public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
            FacesContext context = FacesContext.getCurrentInstance();
    
            if (context.isValidationFailed()) {
                try {
                    context.getExternalContext().redirect(redirect);
                }
                catch (IOException e) {
                    throw new AbortProcessingException(e);
                }
            }
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

    /WEB-INF/my.taglib.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <facelet-taglib
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
        version="2.0"
    >
        <namespace>http://example.com/ui</namespace>
    
        <tag>
            <tag-name>viewParamValidationFailed</tag-name>
            <handler-class>com.example.taghandler.ViewParamValidationFailed</handler-class>
        </tag>  
    </facelet-taglib>
    
    Run Code Online (Sandbox Code Playgroud)

    /WEB-INF/web.xml

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/my.taglib.xml</param-value>
    </context-param>
    
    Run Code Online (Sandbox Code Playgroud)

    确实,这是一些代码,但它最终是干净和可重复使用的<my:viewParamValidationFailed>标签,实际上非常适合新的OmniFaces功能.