Redirect if f: viewParam is empty

How can I make a redirect (or error) if f:viewParamempty?

<f:metadata>
    <f:viewParam name="accountId" value="#{accountMB.id}"/>
</f:metadata>

When I add required="true", nothing happens. What are the options?

+4
source share
2 answers

When I add required="true", nothing happens

You need to <h:message(s)>show the messages of persons associated with this (input) component. You probably already know how to do this for <h:inputText>. You can do the same for <f:viewParam>.

<f:metadata>
    <f:viewParam id="foo" ... required="true" />
</f:metadata>
...
<h:message for="foo" />

How can I make a redirect (or error) if f:viewParamempty?

JSF. <f:viewAction> ( , /, - /, <f:event type="preRenderView">).

<f:metadata>
    <f:viewParam value="#{bean.foo}" />
    <f:viewAction action="#{bean.checkFoo}" />
</f:metadata>

public String checkFoo() {
    if (foo == null || foo.isEmpty()) {
        return "some.xhtml"; // Redirect to that page.
    } else {
        return null; // Stay on current page.
    }
}

HTTP- , ( HTTP 400):

public void checkFoo() {
    if (foo == null || foo.isEmpty()) {
        FacesContext context = Facescontext.getCurrentInstance();
        context.getExternalContext().responseSendError(400, "Foo parameter is required");
        context.responseComplete();
    }
}

JSF OmniFaces, <o:viewParamValidationFailed> bean.

:

<f:metadata>
    <f:viewParam ... required="true">
        <o:viewParamValidationFailed sendRedirect="some.xhtml" />
    </f:viewParam>
</f:metadata>

HTTP 400 :

<f:metadata>
    <f:viewParam ... required="true">
        <o:viewParamValidationFailed sendError="400" />
    </f:viewParam>
</f:metadata>

. :

+8

( ):

@WebFilter(filterName = "MyFilter")
public class MyFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response,  FilterChain chain)
  throws IOException, ServletException {

  if (request.getParameterMap().get("accountId") == null){
     // do redirect
     return;
    }
   chain.doFilter(request, response); 
  }

}

, web.xml:

<filter>
  <filter-name>MyFilter</filter-name>
  <filter-class>my.filter.classpath.MyFilterclass</filter-class>
</filter>
<filter-mapping>
  <filter-name>MyFilter</filter-name>
  <url-pattern>/url/to/my/page.xhtml</url-pattern>
</filter-mapping>

, , .

+1

Source: https://habr.com/ru/post/1584400/


All Articles