DispatcherServlet and web.xml in Spring Download

I'm currently trying to move my project from Java EE to a Spring Boot project. However, I was stuck and confused from the outside with the dispatch servlet and web.xml, and it looks like web.xml is no longer being read by the project. The current project is running on tomcat 7.

In my web.xml file I have a lot of servlet , servlet-mapping , filter and filter mapping , and I really don't understand how to do the mapping in the dispatcher.

I attached a sample of my web.xml below, and version 2.5.

 <?xml version="1.0" encoding="UTF-8"?> <web-app metadata-complete="true" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>displayName</display-name> <description>description</description> <resource-ref> ... </resource-ref> <filter> <filter-name>Some Filter Name</filter-name> <filter-class>Some Filter Class</filter-class> <init-param> <param-name>Some Param Name</param-name> <param-value>Some Value</param-value> </init-param> </filter> <filter-mapping> <filter-name>Some Filter Name</filter-name> <url-pattern>Some url-pattern</url-pattern> </filter-mapping> <context-param> <param-name>Some Param Name</param-name> <param-value>Some Param Value</param-value> </context-param> <servlet> <servlet-name>Some Servlet Name</servlet-name> <servlet-class>Some Servlet Class</servlet-class> </servlet> <servlet-mapping> <servlet-name>Some Servlet Name</servlet-name> <url-pattern>Some Url Pattern</url-pattern> </servlet-mapping> </web-app> 

QNS:

  • Do I have to convert all the materials to my web.xml to rely on the Spring dispatcher, if so, how can I achieve this?
  • Is the path for the Spring boot project web.xml away from web.xml ?

Can someone direct me here? Thanks!!

+22
java spring spring-boot tomcat7
source share
4 answers
  • Yes, spring loading is no longer included in the xml configuration and automatically configures the equivalent of the dispatcher servlet. You can click the following link to learn how to register filters: How to add a filter class in spring Loading?
  • If you are using maven and not gradle, the only XML in your spring loading project should be pom.xml . The spring boot path moves your entire xml, web.xml, etc. configuration In spring boot auto configuration + your java configuration.

Spring loading works very well when you do everything in your java configuration and follow its principles. In my experience, when you start merging the XML configuration and the deprecated spring, it starts to break the automatic configuration process, and it is much better to try as much as possible to fit the new spring loading practices.

+19
source share
  1. You can save your web.xml , but you need to add it

     <listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> 

    in web.xml . And, the required dependency is pom.xml .

  2. All listener classes, filters are converted to a Java class. This class will be @Configuration.

  3. If you have an interceptor, you can move it to the configuration class.

+4
source share

Spring-boot prefers annotations over xml- based configurations, so in your case, instead of using web.xml to configure servlet, servlet-mapping, filter and filter mapping you can use the automatic creation of annotation-based bean components to register bean components. To do this, you need to:

  • Convert XML based mappings to annotation mappings
  • Create bean components using @Bean annotations @Bean that spring-boot automatically @Bean them when scanning components.

For reference: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html.

  • To register filters and add filtering components, you can create a class, annotate it with the @Configuration or @Component and create a @Component bean to register the filter. You can also create filter beans yourself using the @Bean annotation.

For example, the equivalent of the following xml filter

 <filter> <filter-name>SomeUrlFilter</filter-name> <filter-class>com.company.SomeUrlFilter</filter-class> </filter> <filter-mapping> <filter-name>SomeUrlFilter</filter-name> <url-pattern>/url/*</url-pattern> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </filter-mapping> 

An equivalent annotation based on would be:

 @Bean public FilterRegistrationBean someUrlFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(someUrlFilter()); registration.addUrlPatterns("/url/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("Filter"); registration.setOrder(1); return registration; } @Bean(name = "someUrlFilter") public Filter someUrlFilter() { return new SomeUrlFilter(); } 
  • Springboot still allows us to use xml-based configurations, for example, if you want to use web.xml . For example:

web.xml

 <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

and in another dispatcher.xml file you can create beans as:

 <beans ...> <context:component-scan base-package="com.demo"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> 

Note that Spring web.xml will usually be web.xml in src/main/webapp/WEB-INF .

You can contact: https://www.baeldung.com/register-servlet

0
source share

Spent quite a bit of time sharing three things that I remember so that everything works from the command line.

  1. Most important: JSPs should only be stored in the / src / main / resources / META-INF / resources folder. more info here
  2. Make sure that maven takes into account your / src / main / resources folders when building jar, otherwise add these lines to your pom.xml

  <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> 
  1. In your MVC configuration file

    @Configuration public class MvcConfig extends WebMvcConfigurerAdapter {@Bean public InternalResourceViewResolver viewResolver () {InternalResourceViewResolver resolver = new InternalResourceViewResolver (); resolver.setPrefix( "//"); resolver.setSuffix( "JSP."); ;

0
source share

All Articles