【问题标题】:404 error when using UrlBasedViewResolver and JstlView使用 UrlBasedViewResolver 和 JstlView 时出现 404 错误
【发布时间】: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-web jar 在您的运行时类路径中吗?这就是应该扫描并调用此类的SpringServletContainerInitializer 所在的位置。此外,您的 web.xml 中有错误的 XSL;两个地方都需要 3.0 版。
  • 我已按照您的建议编辑了 web.xml。 spring-web.jar 在我的 Java 构建路径 -> 库 -> Maven 依赖项中的 Maven 依赖项下。这足够了还是应该以其他方式配置?
  • 只要范围正确就足够了(compile 可能是你需要的)。

标签: java spring maven spring-mvc


【解决方案1】:

如果我是你自己,我会在你的&lt;a href="" /&gt; HTML 标记中使用 JSTL &lt;c:url 标记。

所以这将是:

<a href="<c:url value="hello-page"/>">Access Hello</a>

此外,您是否尝试过在&lt;a&gt; 标签内的hello-page 前面加上一个正斜杠?那也可以解决你的问题。 (删除.html,因为您的控制器映射到hello-page)。

【讨论】:

  • 试过 c:url 并加上正斜杠,恐怕还是不行
  • 如果不是,那么我必须在你的 Spring XML 配置中;这更有可能是问题所在。请向我们展示您的 XML 配置。
  • web 应用程序使用基于 java 的配置,因此不存在此配置文件的 XML 配置文件
【解决方案2】:

您正在链接到 hello-page.html (),但控制器映射到 hello-page (@RequestMapping(value = "/hello-page")) 没有.html 后缀。

此外,对于所有链接,您应该使用c:url 或spring:url 标签。

【讨论】:

  • 试过@RequestMapping(value="/hello-page.html"),但404错误依旧存在
  • DispatcherServlet 的映射是什么?是否映射到 .html?
  • dispatcherservlet映射被映射来处理所有请求,它在WebAppInitializer.java中
【解决方案3】:

我遵循了这个教程:

http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

并且面临同样的问题。经过研究,我发现任何web-server 都会首先尝试在WEB-INF 文件夹中找到web.xml,因为它是Web 应用程序的默认部署描述符。

在这种情况下,如果web.xml 已经存在并且您正在定义自己的WebApplicationInitializer,则web-server 将无法使用它,因为它已经找到了一个。

所以从项目中删除web.xml文件,然后再次运行。

【讨论】:

    【解决方案4】:

    WebAppInitializer.java 需要以下更改。

     servletDynamic.addMapping("*.html");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-17
      • 2021-08-05
      • 2014-10-26
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      相关资源
      最近更新 更多