AnsweredAssumed Answered

Using Java config rather than web.xml in a B2, with servlet 3 environments

Question asked by guy.heatley on Feb 2, 2016
Latest reply on Nov 2, 2016 by guy.heatley

Hello,

I have been trying to move to Java config rather than use a web.xml in a building block (as the servlet-api that Blackboard uses is version 3 and should support this)

Here's my class that extends the AbstractAnnotationConfigDispatcherServletInitializer which when used produces the strange effect of a nested version of the main page of Blackboard inside the window of the building block with some ajax error.

 

I suspect the villain is the line in red text, but I'm not sure what to do to fix it. I think the WebApplicationContext gets refreshed needlessly.

 

package uk.ac.uclan.lis.dev.bb_crs_by_yr.config;

 

import javax.servlet.ServletContext;

import javax.servlet.ServletRegistration;

 

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.servlet.DispatcherServlet;

 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import uk.ac.uclan.lis.dev.bb_crs_by_yr.mvc.MvcConfiguration;

 

public class UclanAnnotConfDispServInit extends

  AbstractAnnotationConfigDispatcherServletInitializer {

 

  @Override

  protected Class<?>[] getRootConfigClasses() {

  // TODO Auto-generated method stub

  return null;

  }

 

  @Override

  protected Class<?>[] getServletConfigClasses() {

  return new Class<?>[] { MvcConfiguration.class };

  }

 

  @Override

  protected String[] getServletMappings() {

  return new String[] { "/" };

  }

 

  @Override

  protected void registerDispatcherServlet(ServletContext servletContext) {

 

  //servletContext.setInitParameter("defaultHtmlEscape", "true");

 

  WebApplicationContext servletAppContext = createServletApplicationContext();

  // The main Spring MVC dispatcher servlet will use the MVC context.

  DispatcherServlet ds = new DispatcherServlet(servletAppContext);

  //ds.setThrowExceptionIfNoHandlerFound(true);

 

  ServletRegistration.Dynamic appServlet = servletContext.addServlet(

  "appServlet", ds);

  logger.info("Dispatcher servlet (called 'appServlet') added.");

 

  appServlet.setLoadOnStartup(1);

  appServlet.setInitParameter("debug", "true");

 

  appServlet.addMapping(getServletMappings());

 

  }

}

 

Here's the web.xml file which works fine (on my dev VM) if I add it into the project and remove the class listed above:

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

    <display-name>Spring Demo</display-name>

 

    <!-- The central dispatcher servlet for the spring side of things -->

    <servlet>

        <servlet-name>spring</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextClass</param-name>

            <param-value>

            org.springframework.web.context.support.AnnotationConfigWebApplicationContext

        </param-value>

        </init-param>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>uk.ac.uclan.lis.dev.bb_crs_by_yr.mvc.MvcConfiguration</param-value>

        </init-param>

        <init-param>

            <param-name>debug</param-name>

            <param-value>true</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>spring</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

</web-app>

 

Any ideas on how to make the Java config version behave correctly?

 

EDIT: Whilst the B2 version that uses the web.xml works OK on my dev VM it too causes the strange "nested frontpage" effect on our test Blackboard installation. If I remove the web.xml file to use the Java config this fails to work properly on any system.

 

Cheers.

Outcomes