【发布时间】:2013-08-25 09:57:11
【问题描述】:
我正在使用 maven 和基于 java 的配置创建一个简单的 hello world 应用程序。但是,我在一个简单的 href 链接上不断收到 404 错误。文件如下。
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<p>This is the Home Page</p>
<a href="hello-page.html">Access Hello</a>
</body>
</html>
LinkController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LinkController {
@RequestMapping(value = "/hello-page")
public ModelAndView showHelloPage(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello");
modelAndView.addObject("meow", "MEOW MEOW MEOW");
return modelAndView;
}
}
WebAppConfig.java
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.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration //specifies that this is a configuration class
@ComponentScan("com.springtest") //specifies which packages to scan
@EnableWebMvc //specifies that web-mvc annotations can be used
public class WebAppConfig {
@Bean
public UrlBasedViewResolver urlBasedViewResolver(){
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
urlBasedViewResolver.setSuffix(".jsp");
urlBasedViewResolver.setViewClass(JstlView.class);
return urlBasedViewResolver;
}
}
WebAppInitializer.java
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer{
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebAppConfig.class);
context.setServletContext(servletContext);
Dynamic servletDynamic = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(context));
//dispatcher servlet will handle all requests
servletDynamic.addMapping("/");
servletDynamic.setLoadOnStartup(1);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>mavenDWP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的 webapp 文件夹如下所示:
【问题讨论】:
-
您正在
WebAppInitializer中构建您的DispatcherServlet,但我没有看到该类被调用的任何地方。你有其他配置吗?尝试在onStartup中添加一条日志语句,看看它是否被调用过。 -
没有其他配置。我确实尝试了您的建议并提出了 System.out.println("calling onStartup");。它没有被调用。任何想法为什么会这样?
-
您确定
spring-webjar 在您的运行时类路径中吗?这就是应该扫描并调用此类的SpringServletContainerInitializer所在的位置。此外,您的web.xml中有错误的 XSL;两个地方都需要 3.0 版。 -
我已按照您的建议编辑了 web.xml。 spring-web.jar 在我的 Java 构建路径 -> 库 -> Maven 依赖项中的 Maven 依赖项下。这足够了还是应该以其他方式配置?
-
只要范围正确就足够了(
compile可能是你需要的)。
标签: java spring maven spring-mvc