联接面中错误页面映射的最佳做法

时间:2017-11-15 11:53:45

标签: java spring spring-boot error-handling joinfaces

我们正在开发一个带有嵌入式tomcat,spring-boot(无mvc)和joinfaces的Web应用程序。我们没有web.xml或web-fragment.xml,因此错误页面映射有点困难。我们将错误映射实现为@Bean类中的@Configuration注释方法。 E.g:

@Bean
    public ErrorPageRegistrar errorPageRegistrar() {
        return new ErrorPageRegistrar() {
            @Override
            public void registerErrorPages(ErrorPageRegistry registry) {
                registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, errorPage));
                registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, errorPage));
            }
        };
    }

其中errorPage是指向错误文件的静态变量。遗憾的是,来自Omnifaces的类FacesExceptionFilterFullAjaxExceptionHandler不起作用(因为我们没有web.xml)。 那么这种方法真的是在joinfaces中实现错误页面映射的最佳方法,还是有更好的解决方案?

2 个答案:

答案 0 :(得分:0)

我找到了此错误处理解决方案,并且使其与URL资源相同

解决此问题的一种方法是扩展WebMvcConfigurerAdapter。但是,从Spring 5开始,不推荐使用WebMvcConfigurerAdapter。解决方案是直接使用WebMvcConfigurer界面。

创建一个实现WebMvcConfigurer的WelcomePageRedirect类。

所以我为索引,问候和错误创建了根目录

希望对您完全有用

旧方式

    import javax.faces.application.ResourceHandler;
    import javax.inject.Inject;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    @Configuration
    public class DefaultView extends WebMvcConfigurerAdapter {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("forward:/index.xhtml");
            registry.addViewController("/hello") .setViewName("forward:/pages/hello.xhtml");

            registry.addViewController("

/error") .setViewName("forward:/jsf-templates/error_not_found.xhtml");

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

        super.addViewControllers(registry);
    }


}

新方法

@Configuration
public class WelcomePageRedirect implements WebMvcConfigurer {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/")
        .setViewName("forward:/helloworld.xhtml");
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  }
}

更多信息 https://codenotfound.com/jsf-primefaces-welcome-page-redirect-example.html

答案 1 :(得分:0)

我能够使用joinfaces和spring-boot 2配置素数的错误处理。为了使它起作用,我扩展了PrimeExceptionHandlerFactory以覆盖根据抛出的异常决定错误页面在哪里的方法。在src / main / resources / META-INF / faces-config.xml上配置了此扩展的错误处理程序。这种方法还启用了“ p:ajaxExceptionHandler”组件功能。

对于我来说,任何web.xml配置都被忽略了,我认为这是因为我使用了提供Spring Boot的嵌入式tomcat。如果要部署读取的.war / .ear应用程序,则只需在web.xml中定义错误页面

这是一个很不错的技巧,如果在检测到素数时joinjoins可以配置它,那就很不错了,为了创建带有素数的JSF应用程序,需要进行错误处理。

完整的工作项目可以在以下网址找到:https://github.com/ollbap/my-primefaces-spring-boot-skeleton

请参阅:

faces-config.xml

public override void OnEndPage(PdfWriter writer, Document document)
    {


        double capitalsocial;
        CultureInfo culture;
        string specifier = "N";
        iTextSharp.text.Font pfontfooter = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.UNDEFINED.ToString(), 9f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
        iTextSharp.text.Font pfontNumbrePage = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.UNDEFINED.ToString(), 13f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.LIGHT_GRAY);

        base.OnEndPage(writer, document);
        var tableContainerFooter = new PdfPTable(new[] { 9F, 1f })
        {
            TotalWidth = 550f,
            PaddingTop = -51f,
            SpacingAfter = -50f,
            SpacingBefore = -50f,
            DefaultCell = { BorderWidth = 0}
        };

        var tableFooter = new PdfPTable(new[] { 9F })
        {
            DefaultCell = { BorderWidth = 0, HorizontalAlignment = 1},  //0=Left, 1=Centre, 2=Right
            TotalWidth = 500f,
            PaddingTop = -51f,
        };
        tableFooter.HorizontalAlignment = 1;
        tableFooter.DefaultCell.BorderWidth = 0;
        tableFooter.DefaultCell.HorizontalAlignment = 1;

        cb = writer.DirectContentUnder;
        PdfTemplate templateM = cb.CreateTemplate(50, 50);
        templates.Add(templateM);

        int pageN = writer.CurrentPageNumber;
        String pageText = pageN.ToString() + "/";
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        float len = bf.GetWidthPoint(pageText, 12);
        cb.BeginText();
        cb.SetFontAndSize(bf, 10);
        cb.SetTextMatrix(document.RightMargin + len + 530f, document.PageSize.GetBottom(document.BottomMargin));
        cb.ShowText(pageText);
        cb.EndText();
        cb.AddTemplate(templateM, document.RightMargin + len, document.PageSize.GetBottom(document.BottomMargin));

        tableFooter.AddCell(new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(-0.2F, 131F, BaseColor.LIGHT_GRAY, Element.ALIGN_CENTER, 9))));

        tableFooter.AddCell(new Paragraph(raison_sociale + " " + forme_juridique + " au capital de " + capital + "" + devise, pfontfooter));
        tableFooter.AddCell(new Paragraph("I.F: " + idfscal + " - R.C.: " + rc + " - Patente: " + patente + " - C.N.S.S: " + cnss + " - I.C.E: " + ice, pfontfooter));
        tableFooter.AddCell(new Paragraph("Tél.: " + tel1 + " - Fax: " + fax + " - Mail: " + email, pfontfooter));

        tableContainerFooter.AddCell(new PdfPCell(tableFooter) { BorderWidth = 0f, });

        tableContainerFooter.WriteSelectedRows(0, -1, 45f, document.Bottom + 55f, writer.DirectContent);

    }

    //write on close of document
    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        foreach (PdfTemplate item in templates)
        {
            item.BeginText();
            item.SetFontAndSize(bf, 10);

            item.SetTextMatrix(document.RightMargin, document.PageSize.GetBottom(document.BottomMargin));

            numberOfPages = writer.PageNumber;
            document.Add(new Paragraph("" + (writer.PageNumber)) { Alignment = Element.ALIGN_RIGHT, });
            item.EndText();
        }

    }

ExtendedPrimeExceptionHandlerFactory.java

<?xml version="1.0" encoding="windows-1252"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 2.2//EN"
  "http://java.sun.com/dtd/web-facesconfig_2_2.dtd">
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
    <name>MyApplication</name>

    <ordering>
        <before>
            <others />
        </before>
    </ordering>

    <application>
        <el-resolver>org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver</el-resolver>
    </application>
    <factory>
        <exception-handler-factory>es.test.config.ExtendedPrimeExceptionHandlerFactory</exception-handler-factory>
    </factory>
</faces-config>