最近,我一直在学习基于Spring java的配置。我尝试将 web.xml 替换为 WebConfig 和 WebApplicationInitializer 。
每当我请求网址时: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";
}
}
项目结构图像
答案 0 :(得分:0)
使用注释进行初始化的示例。
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, "/*");
}
}
答案 1 :(得分:0)
如果您使用的是spring MVC最新版本,请将configlocation替换为下面的
from context.setConfigLocation("com.soumya.spring.WebConfig");
到context.register(WebConfig.class)。它应该工作。
答案 2 :(得分:0)
首先,您的网址结尾不应带有.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;
}
}