Why web.xml DD for Springsource Bootstrap uses a DispatcherServlet and struts2 is using a Filter?

87 views Asked by At

I would like to know why the web.xml for Struts2 and Spring MVC are different.

Both frameworks use "front controller" MVC pattern in my understanding, but Struts2 uses a Filter and Spring MVC uses a Direct declaration of a servlet in the web.xml?

Doesn't Struts use servlets as well ? If it does then how is it that that servlet is not declared in the web.xml as it is for Spring ?

Spring MVC:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Struts2:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
1

There are 1 answers

0
Andrea Ligios On

Doesn't Struts use servlets as well ? If it does then how is it that that servlet is not declared in the web.xml as it is for Spring ?

Struts2 uses :

  • Actions instead of Servlets (they're ThreadLocal, hence thread-safe and created once per request) as controllers, to handle the presentation layer;
  • Interceptors to handle ortogonal, cross-cutting concerns (sorry,wait for undeletion), such as logging, transaction management, parameters parsing, validation etc...;
  • StrutsPrepareAndExecuteFilter to handle the dispatching process.

So even if the usage of Servlet is possible (through the declaration of an excluded pattern) in a Struts2 application... no, Struts2 doesn't use Servlets at all.

Struts2 also puts values in the ValueStack instead of pushing them in the request, and this makes it the only mainstream Pull-MVC framework (while Spring MVC, Struts1 and others are Push-MVC frameworks)