如何为JSF中的不同PROJECT_STAGE获取不同的错误页面

问题描述 投票:0回答:2

如果项目阶段是开发阶段,我想在错误页面上显示详细的异常以及堆栈跟踪

web.xml条目-

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

否则,如果Project阶段为Production,那么我想向用户显示自定义消息。

web.xml条目-

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>

有什么方法可以实现?

jsf custom-error-pages
2个回答
0
投票

Application#getProjectStage()提供了JSF项目阶段。反过来,Application#getProjectStage()可以使用JSF应用程序。 FacesContext#getApplication()可以在EL中使用JSF上下文。

因此,这应该在错误页面上执行:

FacesContext#getApplication()

-2
投票

创建#{facesContext}并提及如下键:

<c:choose>
    <c:when test="#{facesContext.application.projectStage eq 'Development'}">
         <!-- Print stack trace here -->
    </c:when>
    <c:otherwise>
         <!-- Print custom message here -->
    </c:otherwise>
</c:choose>

还创建一个类,其中已定义所有常量,例如rest.properties

application.mode=DEV

然后创建一个类,像这样说RestConstants.java

public final class RestConstants {
    public static final String APP_MODE_DEVELOPMENT = "DEV";
    public static final String APP_MODE_PRODUCTION = "PROD";
}

然后在您的异常处理类中注入RestServiceProperty.java并按如下所示使用

@Configuration
@PropertySource(value = "classpath:rest.properties")
public class RestServiceProperty {

    @Value("${application.mode}")
    private String appMode;

    public String getAppMode() {
        return appMode;
    }

}

注意:我使用了GenericExceptionMapper,可以用Spring实现的方式相同,它为异常处理提供了RestServiceProperty。请参阅:@Provider public class GenericExceptionMapper implements ExceptionMapper<Throwable>{ @Autowired private RestServiceProperty restServiceProperty; @Override public Response toResponse(Throwable t) { if (restServiceProperty.getAppMode().equals(RestConstants.APP_MODE_DEVELOPMENT)){ //set stackTrace here //If application.mode=DEV in rest.properties this will excecute else{ //If application.mode not DEV in rest.properties this will excecute } } } }

© www.soinside.com 2019 - 2024. All rights reserved.