Sping mvc app - 是什么导致找不到请求映射?

时间:2017-11-17 18:08:19

标签: java spring jsp spring-mvc

WARNING: No mapping found for HTTP request with URI [/BigData/hello] in DispatcherServlet with name 'HelloWeb'

上面的错误。我遇到这个错误的请求映射无法找到。我已经加载了index.jsp页面,但是当我单击表单提交它时,找不到正确的地方来映射下一个加载的视图。

的index.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>
<link href="/resources/css/layout.css" rel="stylesheet">
<script src="/webjars/jquery/3.2.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Big Data</title>
</head>

<body>
    <h3>Hint: user and password is "admin", no quotes.</h3>
    <form action="hello" method="post">
        Name:<input type="text" name="name" /><br /> Password:<input
            type="password" name="password" /><br /> <input type="submit"
            value="login" />
    </form>
    </div>
</body>
</html>

这是控制器     包com.bigdata.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller  
public class HelloWorldController {

    @RequestMapping("/hello")  
    public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse res) {  
        String name=request.getParameter("name");  
        String password=request.getParameter("password");  

        if(password.equals("admin")&&name.equals("admin")){  
            String message = "Welcome "+ name;  
            return new ModelAndView("homepage", "message", message);  
        }  
        else{  
            return new ModelAndView("errorpage", "message","Sorry, username or password error");  
        }  

    }  
}  

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

 <servlet>  
    <servlet-name>HelloWeb</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>HelloWeb</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

这是HelloWeb-servlet.xm

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    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.bigdata.controllers"/>
    <context:annotation-config/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:resources mapping="/webjars/**" location="/webjars/"/>    
    <mvc:resources mapping="/resources/**" location="/resources/**" />  
</beans>  

不确定我错过了什么但是当我启动应用程序时,我得到了表单和主页,但是当我输入时,我得到404没有找到映射。

1 个答案:

答案 0 :(得分:0)

我没有看到您的配置有任何问题。创建了一个结构如下的示例项目。

enter image description here

在HelloWorldController中添加了一个示例方法来显示索引页

@RequestMapping("/index")
public String index(){
  return "index";
}

示例homepage.jsp

...
<body>
My Home
</body>
</html>

似乎有效。

enter image description here

输出:

enter image description here