基于Spring Java的Crystal Reports配置

时间:2017-06-17 16:20:10

标签: java xml spring spring-mvc crystal-reports

我刚接触到水晶报告,我创建了我的应用程序,我使用了一些java注释配置。现在我正在尝试与水晶报告集成,我必须将基于xml的web.xml转换为基于java的配置 这是web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- crystal report in spring -->
    <context-param>
        <param-name>crystal_image_uri</param-name>
        <param-value>/crystalreportviewers</param-value>
    </context-param>
    <context-param>
        <param-name>crystal_image_use_relative</param-name>
        <param-value>webapp</param-value>
    </context-param>

    <servlet>
        <servlet-name>CrystalReportViewerHandler</servlet-name>
        <servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CrystalReportViewerHandler</servlet-name>
        <url-pattern>/CrystalReportViewerHandler</url-pattern>
        <url-pattern>/faces/CrystalReportViewerHandler</url-pattern>
    </servlet-mapping>

</web-app>

我试图将它与java配置混合但它失败了, 提前致谢

2 个答案:

答案 0 :(得分:0)

要将基本web.xml转换为java配置,请按

创建一个类
  • 实施WebApplicationInitializer或
  • 扩展AbstractAnnotationConfigDispatcherServletInitializer

关于您的水晶报告,您可以使用@Bean Annotation创建CrystalReportViewerServlet类型的Bean。

了解更多信息,请检查 - web.xml to java configregister secondary servlet in spring

答案 1 :(得分:0)

感谢所有寻求解决方案的人。我做了以下

@Configuration
@ComponentScan(basePackages = { "t.g.app" })
public class ReportsConfig implements WebApplicationInitializer{

@Override
public void onStartup(ServletContext servletContext) throws ServletException{
    servletContext.setInitParameter("crystal_image_uri","/crystalreportviewers");
    servletContext.setInitParameter("crystal_image_use_relative", "webapp");
    ServletRegistration.Dynamic crystalReportViewerHandler = servletContext
              .addServlet("CrystalReportViewerHandler", new CrystalReportViewerServlet());

    crystalReportViewerHandler.setLoadOnStartup(1);
    crystalReportViewerHandler.addMapping("/CrystalReportViewerHandler");
    }
}

还有一些由Spring安全性引起的安全问题,所以我在安全配置中添加了一些例外。