@RequestMapping无法在spring mvc中工作

时间:2013-06-24 17:47:46

标签: spring jsp spring-mvc twitter-bootstrap

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

当我尝试访问appointmentments.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>

针对home.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>

appointments.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>

1 个答案:

答案 0 :(得分:0)

您错过了ContextPath并直接引用了.jsp文件,这是错误的。

试试这个

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