[英]Spring WebApplicationInitializer
最近,我一直在學習基於Spring Java的配置。 我試圖用WebConfig和WebApplicationInitializer替換web.xml 。
每當我請求url時: http:// localhost:8080 / spring-demo / greeting.html我得到404描述所請求的資源不可用。 以下是我的項目詳細信息。
WebConfig.java
package com.soumya.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.soumya.spring")
public class WebConfig {
}
WebAppInitializer.java
package com.soumya.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.soumya.spring.WebConfig");
return context;
}
}
控制者
package com.soumya.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value = "/greeting")
public String greeting(Model model) {
model.addAttribute("greeting", "Hello World!");
return "greeting.jsp";
}
}
項目結構圖
首先,您的網址結尾不應帶有.html或.jsp。 該網址將為http://localhost:8080/spring-demo/greeting
在特定的WebApplicationInitalizer中,應該將DispatcherServlet映射到/greeting
控制器,而不是“ * .html”。
dispatcher.addMapping("/greeting");
另外,您的DispatcherServlet(代碼中的WebConfig.class)沒有定義ViewResolver Bean。 ViewResolver負責將視圖名稱(如HelloController返回的“ greeting.jsp”)映射到視圖實現(可能存在於/ WEB-INF /文件夾中的實際“ greeting.jsp”文件)。
我已將您的WebConfig類更改為包括ViewResolver bean-
package com.soumya.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.soumya.spring")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver irvr = new InternalResourceViewResolver();
irvr.setPrefix("/WEB-INF/views/"); //your "greeting.jsp" file should be here
irvr.setSuffix(".jsp");
irvr.setExposeContextBeansAsAttributes(true);
return irvr;
}
}
帶注釋的初始化示例。
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Creates context object
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// Registers annotated configurations class
ctx.register(Configurations.class);
// Sets ContextLoaderListener to servletContext
servletContext.addListener(new ContextLoaderListener(ctx));
// Passes servlet context to context instance
ctx.setServletContext(servletContext);
//Registers dispatch servlet and passes context instance
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
//Maps URL pattern
servlet.addMapping("/");
//Sets creation priority
servlet.setLoadOnStartup(1);
//Registers security filters
FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
// Sets dispatcher types a security filters to be applied
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
}
}
如果您使用的是Spring MVC最新版本,請使用以下內容替換configlocation
from context.setConfigLocation("com.soumya.spring.WebConfig");
到context.register(WebConfig.class)。 它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.