Programming

무엇을 할 수 있습니까

procodes 2020. 6. 15. 22:21
반응형

무엇을 할 수 있습니까 , 에 사용됩니까?


누구든지 우리가 일반적으로 또는 실제 예제 에서이 코드 조각을 어떻게 사용할 수 있는지 명확히 할 수 있습니까?

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

GET 매개 변수 처리

<f:viewParam>GET 매개 변수의 설정, 변환 및 검증을 관리합니다. 그것은 <h:inputText>이지만 GET 매개 변수와 같습니다.

다음 예

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

기본적으로 다음을 수행합니다.

  • name으로 요청 매개 변수 값을 가져 오십시오 id.
  • 필요한 경우 변환하고 유효성을 검사하십시오 ( required, validatorconverter속성을 사용 하고 같이 a <f:converter>중첩 할 수 <f:validator>있음 <h:inputText>)
  • 변환 및 유효성 검증에 성공한 #{bean.id}경우 값으로 표시되는 Bean 특성으로 설정 하거나 value속성이없는 경우 이름에서 요청 속성으로 설정 하여보기에서 id사용 가능하게 #{id}하십시오.

따라서 페이지를 열면 뷰가 렌더링되기 직전에 foo.xhtml?id=10매개 변수 값 10이이 방식으로 Bean에 설정됩니다.

유효성 검사와 관련하여 다음 예제에서는 매개 변수를 required="true"10으로 설정하고 10과 20 사이의 값만 허용합니다. 유효성 검사에 실패하면 메시지가 표시됩니다.

<f:metadata>
    <f:viewParam id="id" name="id" value="#{bean.id}" required="true">
        <f:validateLongRange minimum="10" maximum="20" />
    </f:viewParam>
</f:metadata>
<h:message for="id" />

GET 매개 변수에 대한 비즈니스 조치 수행

<f:viewAction>이것을 위해 사용할 수 있습니다 .

<f:metadata>
    <f:viewParam id="id" name="id" value="#{bean.id}" required="true">
        <f:validateLongRange minimum="10" maximum="20" />
    </f:viewParam>
    <f:viewAction action="#{bean.onload}" />
</f:metadata>
<h:message for="id" />

public void onload() {
    // ...
}

<f:viewAction>JSF 2.2합니다 (이 때문에 새로운 <f:viewParam>이미 JSF 2.0부터 존재). 업그레이드 할 수 없다면 가장 좋은 방법은 <f:event>대신 사용하는 것입니다.

<f:event type="preRenderView" listener="#{bean.onload}" />

그러나 이것은 모든 요청에서 호출됩니다 . 요청이 포스트 백이 아닌지 명시 적으로 확인해야합니다.

public void onload() {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // ...
    }
}

"전환 / 검증 실패"사례도 건너 뛰려면 다음과 같이하십시오.

public void onload() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
        // ...
    }
}

Using <f:event> this way is in essence a workaround/hack, that's exactly why the <f:viewAction> was introduced in JSF 2.2.


Pass view parameters to next view

You can "pass-through" the view parameters in navigation links by setting includeViewParams attribute to true or by adding includeViewParams=true request parameter.

<h:link outcome="next" includeViewParams="true">
<!-- Or -->
<h:link outcome="next?includeViewParams=true">

which generates with the above <f:metadata> example basically the following link

<a href="next.xhtml?id=10">

with the original parameter value.

This approach only requires that next.xhtml has also a <f:viewParam> on the very same parameter, otherwise it won't be passed through.


Use GET forms in JSF

The <f:viewParam> can also be used in combination with "plain HTML" GET forms.

<f:metadata>
    <f:viewParam id="query" name="query" value="#{bean.query}" />
    <f:viewAction action="#{bean.search}" />
</f:metadata>
...
<form>
    <label for="query">Query</label>
    <input type="text" name="query" value="#{empty bean.query ? param.query : bean.query}" />
    <input type="submit" value="Search" />
    <h:message for="query" />
</form>
...
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
     ...
</h:dataTable>

With basically this @RequestScoped bean:

private String query;
private List<Result> results;

public void search() {
    results = service.search(query);
}

Note that the <h:message> is for the <f:viewParam>, not the plain HTML <input type="text">! Also note that the input value displays #{param.query} when #{bean.query} is empty, because the submitted value would otherwise not show up at all when there's a validation or conversion error. Please note that this construct is invalid for JSF input components (it is doing that "under the covers" already).


See also:

참고URL : https://stackoverflow.com/questions/6377798/what-can-fmetadata-fviewparam-and-fviewaction-be-used-for

반응형