[英]DispatcherServlet and web.xml in Spring Boot
I'm currently trying to move my project from Java EE to Spring Boot project.我目前正在尝试将我的项目从 Java EE 移动到 Spring Boot 项目。 However, i've been stucked and confused on the part with dispatcher servlet and web.xml and it seems like web.xml is no longer being read by the project anymore.
但是,我对调度程序 servlet 和 web.xml 的部分感到困惑和困惑,似乎 web.xml 不再被项目读取。 The current project is running on tomcat 7.
当前项目在 tomcat 7 上运行。
In my web.xml
file, I have lots of servlet
, servlet-mapping
, filter
and filter mapping
and I don't really understand how to do the mapping in the dispatcher.在我的
web.xml
文件中,我有很多servlet
、 servlet-mapping
、 filter
和filter mapping
,我真的不明白如何在调度程序中进行映射。
I've attached a sample of my web.xml
below and the version is 2.5.我在下面附上了我的
web.xml
示例,版本为 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:问:
web.xml
to rely on the spring dispatcher, if yes how can I achieve that?web.xml
所有内容转换为依赖 spring 调度程序,如果是,我该如何实现?web.xml
the way to go for spring boot project?web.xml
是 Spring Boot 项目的发展方向吗? Can anyone please guide me along here?有人可以在这里指导我吗? Thanks!!
谢谢!!
pom.xml
.pom.xml
。 The way to go with spring boot is moving all your xml configuration, web.xml etc to spring boot's auto configuration + your java configuration. Spring boot works very good when you do everything in java configuration and follow its principals.当您在 Java 配置中完成所有操作并遵循其原则时,Spring Boot 会非常有效。 From my experience with it, when you start merging XML configuration and the legacy spring it starts breaking the auto configuration process and its much better to try as much as you can to comply with the new spring boot best practices.
根据我的经验,当您开始合并 XML 配置和遗留 spring 时,它开始破坏自动配置过程,最好尽可能多地尝试遵守新的 spring 引导最佳实践。
You can keep your web.xml
, but it needs to add您可以保留
web.xml
,但需要添加
<listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener>
in web.xml
.在
web.xml
。 And, required dependency of pom.xml
.并且,需要依赖
pom.xml
。
All listener classes, filters converts in Java class.所有侦听器类、过滤器都在 Java 类中转换。 This class would be
@Configuration.
这个类将是
@Configuration.
If you have an interceptor, that can be moved to configuration class.如果您有拦截器,则可以将其移至配置类。
Spring-boot prefer annotations over xml based configurations, so in your case instead of using web.xml
to configure the servlet, servlet-mapping, filter
and filter mapping
, you can use annotation based automatic bean creations to register beans.For that you need to : Spring-boot 更喜欢基于xml的配置的注释,所以在你的情况下,而不是使用
web.xml
来配置servlet, servlet-mapping, filter
和filter mapping
,你可以使用基于注释的自动 bean 创建来注册 bean。为此你需要到 :
@Bean
annotations so that spring-boot will automatically take them up during component scan.@Bean
注释创建 bean,以便 spring-boot 在组件扫描期间自动占用它们。 For reference: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html供参考: https : //docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html
@Configuration
or @Component
annotation and create bean of FilterRegistrationBean
to register the filter.You can also create the beans of filter itself there by using @Bean annotation.@Configuration
或@Component
批注对其进行批注,并创建FilterRegistrationBean
bean 以注册过滤器。您还可以使用 @Bean 批注在那里创建过滤器本身的 bean。 For example, the equivalent of the following xml based filter例如,相当于以下基于 xml 的过滤器
<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>
The equivalent annotation based will 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();
}
web.xml
.For example : web.xml
。例如: Web.xml网页.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 file dispatcher.xml you can create beans as :在另一个文件dispatcher.xml 中,您可以将 bean 创建为:
<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 live in src/main/webapp/WEB-INF
.请注意,Spring
web.xml
通常位于src/main/webapp/WEB-INF
。
You can refer : https://www.baeldung.com/register-servlet您可以参考: https : //www.baeldung.com/register-servlet
Spent quite some time so sharing three things I remember to make it working from command line.花了相当多的时间,所以我记得分享三件事,让它从命令行工作。
<resources> <resource> <directory>src/main/resources</directory> </resource> </resources>
In your mvc config file在你的 mvc 配置文件中
@Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/Representation/"); resolver.setSuffix(".jsp"); return resolver; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.