0

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>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
aruuuuu
  • 1,605
  • 2
  • 22
  • 32
  • If all MVC frameworks did everything exactly the same way, why would we have serveral ones? They just chose a different way of achieving a similar goal. – JB Nizet Nov 29 '15 at 22:45
  • though asking the question though I am trying to learn more about both and about the servlet spec altogether, a worthwhile exercise in my opinion – aruuuuu Nov 30 '15 at 16:12

1 Answers1

0

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)

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243