【发布时间】:2015-10-12 05:46:27
【问题描述】:
我正在尝试使用 Spring MVC(启动项目 STS 3.7.0)构建 webapp 基本项目。 Java 1.7
WebConfig.java
package com.terafast;
import org.springframework.context.annotation.Bean;
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.terafast")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver getInternalViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
}
WebAppInitializer.java
package com.terafast;
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.terafast.WebConfig");
return context;
}
}
家庭控制器
package com.terafast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(value = "/index")
public String index(Model model) {
return "welcome";
}
}
我在 webapp/WEB-INF/view/ 路径中创建了 welcome.jsp。当我将项目作为服务器运行时,我在 http://localhost:8080/TEM/ 上一无所获。当我使用http://localhost:8080/TEM/index.html 时,我也一无所获。
18:18:29.911 [http-nio-8080-exec-4] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/TEM/index] in DispatcherServlet with name 'DispatcherServlet'
18:18:29.911 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
【问题讨论】:
-
/永远不会匹配您的映射DispatcherServlet和/index不会匹配/index.html。将您的DispatcherServlet映射到/而不是*.html。 -
我将其更改为 addMapping("/") 并尝试了 /index url。但我仍然有同样的问题。
-
您的
prefix也应该以/开头,所以/WEB-INF而不是WEB-INF。启用 DEBUG 日志记录或org.springframework.web以查看它失败的原因,它是否真的在请求的 url 或视图呈现上失败。 -
不工作!还是一样的问题。
-
请启用调试日志并发布输出...
标签: java spring spring-mvc servlets