运行webapp时遇到未知映射

时间:2017-08-13 15:15:22

标签: java spring spring-mvc servlets web-applications

我正试图在eclipse上使用spring maven中的控制器映射我的第一个登录页面。我将'/ login'映射到WEB-INF / jsp文件夹中的login.jsp文件。但是在调用url localhost:8080 / Project / login时,它会在此消息中显示404错误:

  

/Project/WEB-INF/jsp/hello.jsp

     

渲染视图[org.springframework.web.servlet.view.InternalResourceView:name'hello';名为“project”的DispatcherServlet中的URL [/WEB-INF/jsp/hello.jsp]]

我没有任何名为hello.jsp的文件,也没有映射到任何此类文件。我的代码如下:ProjectController.java

package com.fico;    
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller
public class ProjectController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView getLogin(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView in = new ModelAndView("login");
        in.addObject("message", "Finally page loaded");
        return in;
    }
}

web.xml文件:

<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring MVC Project</display-name>
  <servlet>
      <servlet-name>project</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>project</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>  
</web-app>

项目servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.fico" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

的login.jsp

<%@ page contentType = "text/jsp; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>

为什么要搜索hello.jsp文件?我不明白我的映射或控制器有什么错误吗?我检查文件结构,没有任何问题。我根据需要添加了所有依赖项。默认欢迎页面加载正常。如果我添加一个hello.jsp文件,它也可以工作。为什么会这样?

0 个答案:

没有答案