0

I'm creating a simple hello world application using maven and java-based configuration. However, I keep on getting a 404 error on a simple href link. The files are below.

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>

my webapp folder looks like: my webapp folder looks like:

4

4 回答 4

0

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

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

于 2013-08-25T10:04:54.327 回答
0

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

所以这将是:

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

hello-page另外,您是否尝试过在<a>标签前添加正斜杠?那也可以解决你的问题。(删除.html您的 Controller 映射到hello-page)。

于 2013-08-25T10:05:04.173 回答
0

我遵循了本教程:

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

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

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

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

于 2013-09-03T11:59:59.560 回答
0

WebAppInitializer.java需要以下更改。

 servletDynamic.addMapping("*.html");
于 2016-05-14T02:22:59.880 回答