使用UrlBasedViewResolver和JstlView时出现404错误

时间:2013-08-25 09:57:12

标签: 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 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文件夹如下所示: my webapp folder looks like:

4 个答案:

答案 0 :(得分:0)

您正在关联hello-page.html(&lt; a href =“ hello-page.html ”&gt;),但控制器已映射到hello-page(@RequestMapping( value =“/ hello-page ”))没有.html后缀。

此外,对于您的所有链接,您应使用c:urlspring:url标记。

答案 1 :(得分:0)

如果我是您自己,我会在您的<c:url HTML标记中使用JSTL <a href="" />标记。

所以这将是:

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

此外,您是否尝试在hello-page标记内的<a>前面加上正斜杠?这也可以解决你的问题。 (删除.html,因为您的控制器已映射到hello-page)。

答案 2 :(得分:0)

我遵循了本教程:

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

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

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

因此,从项目中删除web.xml文件,然后重新运行。

答案 3 :(得分:0)

下面需要

WebAppInitializer.java 更改。

 servletDynamic.addMapping("*.html");
相关问题