Search code examples
javaspring-bootstruts-1

SERVLET_MAPPING Suffix .do in Struts1 to Spring Boot Migration


Working on a migration from Struts1 to Springbooted Struts1, I faced action resolving issues.

Using ServletRegistrationBean, Struts1 would behave as expected (when someAction.do is invoked, it works correctly).

The problem, however, is related to the generated HTML with:

<html:form action="someAction" .../>

The HTML rendering of someAction must be suffixed with .do, but it was not.

<form action="someAction" .../>

After debugging html taglib, I discovered that the equivalent parameter in web.xml, servlet-mapping is not actually replicated in the ServletRegistrationBean, although .addUrlMappings("*.do") was specified.

In the image below, pageContext.getAttribute() retrieves '*.do' for a basic Struts1 application, but it returns null in the Springbooted application.

enter image description here

What should I do to make pageContext.getAttribute(Globals.SERVLET_KEY, PageContext.APPLICATION_SCOPE); return '*.do'?

@Bean
public ServletRegistrationBean strutsActionServlet() {
        
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
    servletRegistrationBean.setServlet(new myActionServlet());
    servletRegistrationBean.setLoadOnStartup(10);
    servletRegistrationBean.setName("StrutsServlet");
    servletRegistrationBean.addUrlMappings("*.do");

    servletRegistrationBean.addInitParameter("config", "/WEB-INF/struts-config.xml");
    return servletRegistrationBean;
}

Solution

  • This is how I solved it through ServletContextEvent:

    public void contextInitialized (ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext ();
        sc.setAttribute(Globals.SERVLET_KEY, "*.do");
    }