2012-10-18 54 views
1

你好,我是Spring的新手,并停留在我的jsp映射 - 我的页面发出404 - 页面未找到的错误。Spring MVC jsp映射

我的项目结构



    WebContent 
    + META-INF 
    - WEB-INF 
     - jsp 
     index.jsp 
     login.jsp 
     +lib 
     springMVC-servlet.xml 
     web.xml 

的web.xml




    <display-name>spring</display-name> 
     <servlet> 
      <servlet-name>springMVC</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <load-on-startup>1</load-on-startup> 
     </servlet> 

    <servlet-mapping> 
     <servlet-name>springMVC</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    </web-app> 

用SpringMVC-servlet.xml中



    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

     <context:component-scan 
      base-package="web.controller" /> 

    <!-- Enabling Spring MVC configuration through annotations --> 
    <mvc:annotation-driven /> 

     <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix" value="/WEB-INF/jsp/" /> 
      <property name="suffix" value=".jsp" /> 
     </bean> 
    </beans> 

的HomeController



    @RequestMapping(method=RequestMethod.GET, value="/") 
      public String index(){ 
        return "index"; 
      } 

      @RequestMapping(value="/login") 
      public String login() { 
       return "login"; 
      } 

我能够加载http://localhost:8080/Spring3/

中的index.jsp具有下面的代码 -



    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" 
     pageEncoding="UTF-8"%> 
    <html> 
    <head> 
    <title>Login</title> 
    </head> 
    <body> 
    <c:url value="login.jsp" var="somevar" /> 
    <h1>Click here to </h1> <a href= "${somevar}">Login</a> 
    </body> 
    </html> 

然而,当我点击链接到登录页面 - 我得到的404页。

我不确定发生了什么问题 - 任何人都可以帮助我确定我的映射出错的地方。

+0

你直接链接到JSP页面无法访问到客户端,使用Spring控制器映射。 –

回答

6

在index.jsp中,你应该写<c:url value="login" var="somevar" />具有映射@RequestMapping(value="/login")您CONTROLER

+0

非常感谢...帮助:) – user1755645