0
votes

Im new to spring with crystal reports I have created my application where I used some java annotation configuration. Now I am trying to integrate with crystal reports where I have to convert xml based web.xml to java based config here is the web.xml code

<?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>

I have tried to mix it with java config but it fails, Thanks in advance

2
show pls servlet-context.xml and for "I have tried to mix it with java config but it fails" , what exception do you get? - xyz

2 Answers

0
votes

To convert your basic web.xml to java configuration, either create a class by

  • implement WebApplicationInitializer or
  • extend AbstractAnnotationConfigDispatcherServletInitializer

regarding your crystal report you can create the Bean of type CrystalReportViewerServlet using @Bean Annotation.

for more information please check - web.xml to java config and register secondary servlet in spring

0
votes

Thanks for to all who looked for a solution. I did the following

@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");
    }
}

There were also some security issues caused by spring security so i added some exceptions in security configuration.