0

我正在使用 Maven,Spring 3.1.1 创建一个带有模型和控制器的简单登录页面。我刚刚创建了一个模型和控制器。但是在运行应用程序时,我在浏览器和控制台中收到404 错误,我收到以下错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告:在名称为“dispatcher”的 DispatcherServlet 中找不到具有 URI [/TEST2/] 的 HTTP 请求的映射

我已经正确检查了配置,但找不到确切的错误。

我改变了一些配置。我尝试/*输入 URL-Mapping,但我面临同样的问题。

我的 Web.XML

<?xml version="1.0" encoding="ISO-8859-1" ?>
    <web-app 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"
        version="2.4">

        <display-name>Spring MVC Application</display-name>

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

        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </context-param>

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

调度程序-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" 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.xsd">

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


   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/pages/"/>
      <property name="suffix" value=".jsp"/> 
   </bean>

</beans>

登录控制器.Java

package com.concretepage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(){
        return "redirect:pages/login.jsp";
    }

    @RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
    public String userCheck(ModelMap model, HttpServletRequest request) {
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
            model.addAttribute("message", "Successfully logged in.");

        }else{
            model.addAttribute("message", "Username or password is wrong.");
        }
        return "redirect:success.jsp";
    }

}

我的错误是什么?

4

1 回答 1

0

您尚未放置根路径的映射“/”更改此代码

    @RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
    return "redirect:pages/login.jsp";
} 

进入

    @RequestMapping(value="/", method = RequestMethod.GET)
public String login(){
    return "redirect:pages/login.jsp";
}  

并检查输出。

于 2016-05-19T05:51:50.690 回答