1

我对spring mvc比较陌生。我无法让页面导航正常工作。我查看了文档和其他论坛以寻求帮助,但我无法弄清楚我哪里出错了。有人可以帮忙吗..

当我尝试访问约会.jsp 时得到 404

控制器

package com.mycompany.myapp;



@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);


@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);
    return "home";
}

@RequestMapping(value = "/appointments", method = RequestMethod.GET)
public String appointments(Locale locale, Model model){
    return "appointments";
}

}

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static   resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the    /WEB-INF/views directory -->
<beans:bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.mycompany.myapp" />



</beans:beans>

主页.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link
href="//netdna.bootstrapcdn.com/bootswatch/2.3.2/amelia/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<script src="http://code.jquery.com/jquery.js"></script>
<script
    src="//netdna.bootstrapcdn.com/twitter-   bootstrap/2.3.2/js/bootstrap.min.js"></script>

<div class="navbar">
    <div class="navbar-inner">
        <div class="container">

            <a class="btn btn-navbar" data-toggle="collapse"
                data-target=".nav-collapse"> <span class="icon-bar"></span> <span
                class="icon-bar"></span> <span class="icon-bar"></span>
            </a> <a class="brand" href="#">Doctor Assist</a>

            <ul class="nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="/appointments.jsp">Appointments</a></li>
                <li><a href="#">Patient Data</a></li>

                <li class="dropdown"><a href="#" class="dropdown-toggle"
                    data-toggle="dropdown"> Account <b class="caret"></b>
                </a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Personal</a></li>
                        <li><a href="#">Link</a></li>
                    </ul></li>
            </ul>

        </div>
    </div>
</div>

<h1>work in progress</h1>

</body>
</html>

约会.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">
</head>
<body>
</body>
</html>
4

1 回答 1

0

您缺少ContextPath并直接引用该.jsp文件,这是错误的。

试试这个

<li><a href="${pageContext.request.contextPath}/appointments">Appointments</a></li>

于 2013-06-24T18:50:14.557 回答