Java 404使用UrlBasedViewResolver和JstlView时出错
我正在使用maven和基于java的配置创建一个简单的hello world应用程序。然而,我在一个简单的href链接上不断收到404错误。文件如下 index.jspJava 404使用UrlBasedViewResolver和JstlView时出错,java,spring,maven,spring-mvc,Java,Spring,Maven,Spring Mvc,我正在使用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 Transition
<%@ 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>
WebAppConfig.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;
}
}
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;
}
}
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);
}
}
WebAppInitializer.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;
}
}
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;
}
}
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>
马文德普
index.jsp
我的webapp文件夹看起来像:
您正在链接到
hello page.html
(或标记)。如果我是您,我会使用JSTL我遵循了本教程:
在研究之后,我发现任何web服务器
都会首先在web-INF
文件夹中查找web.xml
,因为它是web应用程序的默认部署描述符
在这种情况下,如果web.xml
已经存在,并且您正在定义自己的WebApplicationInitializer
,则web服务器将无法使用它,因为它已经找到了一个
因此,请从项目中删除web.xml
文件,然后再次运行。WebAppInitializer.java下面需要更改
servletDynamic.addMapping("*.html");
尝试了c:url并使用正斜杠,仍然不起作用我很抱歉如果不是这样,那么我必须在您的Spring XML配置中;这更可能是问题所在。请向我们显示您的XML配置。web应用使用基于java的配置,因此此Oneried@RequestMapping(value=“/hello page.html”)不存在XML配置文件,但404错误仍然存在,DispatcherServlet的映射是什么?它映射到.html了吗?DispatcherServlet映射被映射到处理所有请求,它在WebAppInitializer.java中。您正在WebAppInitializer
中构建您的DispatcherServlet
,但我没有看到任何地方调用该类。您知道吗你有其他配置吗?试着在启动时将log语句放入onStartup
以查看是否调用过它。没有其他配置。我确实尝试了你的建议,并将System.out.println(“启动时调用”)放入;它没有被调用。你知道为什么会这样吗?你确定SpringWeb
jar在你的运行时类路径上吗?这就是SpringServletContainerInitializer
的位置,它应该扫描并调用这个类。另外,你的web.xml
中有错误的XSL;这两个版本都需要3.0版本放置。我已经按照您的建议编辑了web.xml。spring-web.jar在我的Java构建路径->库->Maven依赖项中位于Maven依赖项下。这足够了吗?还是应该以另一种方式进行配置?只要范围正确就足够了(compile
可能是您需要的)。