2012-10-18 183 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