java - Spring MVC 404错误http请求

标签 java spring spring-mvc

我是 Java Spring 框架的新手,我正在创建简单的重定向应用程序。但是我收到了 http 404 错误 代码如下

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
          http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
         <context:component-scan base-package="com.webservlet"/>
</beans>

调度器-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" >


        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
        <bean class="com.webservlet.LoginServlet">

        </bean>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="indexMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>
    <bean id="homeMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="home.htm">homeController</prop>
            </props>
        </property>
    </bean>
    <bean id="loginMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="login.htm">loginController</prop>
            </props>
        </property>
    </bean>
     <bean id="about_usMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="about_us.htm">about_usController</prop>
            </props>
        </property>
    </bean>
     <bean id="contact_usMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props> <prop key="contact_us.htm">contact_usController</prop>
            </props>
        </property>
    </bean>


    <!--
    The index controller.
    -->

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />
    <bean name="homeController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="home" />
    <bean name="loginController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="login" />


    <bean name="about_usController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="about_us" />
    <bean name="contact_usController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="contact_us" />
    <!--
        The viewResolver
    -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>

LoginServlet

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.webservlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
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.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

/**
 *
 * @author sharathsind
 */
@Controller
@EnableWebMvc


public class LoginServlet {



    @RequestMapping(value="/login.do",method = RequestMethod.POST)

public ModelAndView handleRequestInternal()  {

       return new ModelAndView("home");
    }

}

登录.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body>
          <script>
            function display(string) {
            document.getElementById("form").action = string;
            }
        </script>
        <form  method="post" id="form" onsubmit="display('login.do')">
            UserName: <input type="text" name="userName"><br>
            PassWord: <input type="password" name="passWord">
            <input type="submit" value="submit"/>

        </form>
    </body>
</html>

当我重定向到 login.do 时它没有显示。任何人都可以帮助我 提前致谢

最佳答案

您的配置有缺陷,更不用说它配置太多了。对于初学者,删除 @EnableWebMvc来自你的 LoginServlet因为它什么也没做。它应该放在 @Configuration 上基于类并在您使用 XML 的基于 Java Config 的应用程序中使用。

applicationContext.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.webservlet">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
         </context:component-scan>
</beans>

应该扫描 Controller 以外的所有内容(注意 exclude-filter )。

dispatcherServlet.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.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

         <context:component-scan base-package="com.webservlet" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
         </context:component-scan>

          <mvc:annotation-driven />

          <mvc:view-controller path="/index.html" view-name="index" />
          <mvc:view-controller path="/home.html" view-name="home" />
          <mvc:view-controller path="/login.html" view-name="login />
          <mvc:view-controller path="/about_us.html" view-name="about_us" />
          <mvc:view-controller path="/contact_us.html" view-name="contact_us" />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>

只扫描 Controller ,注意 include-filteruse-default-filters="false"属性。启用 @RequestMapping<mvc:annotation-driven /> 解析对于那些讨厌的 View Controller ,请使用 <mvc:view-controller /> .

有关 mvc 的更多信息命名空间我建议阅读 this section引用指南。

另一个技巧是使用无版本的 xsd 文件而不是版本化的文件,这将负责使用类路径上可用的最新版本的 xsd。

关于java - Spring MVC 404错误http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24603981/

相关文章:

java - 准备好的语句会明显减慢程序速度吗?

java - 违反约束时静默失败

java - 事务回滚之前的事务

java - 替换 spring/hibernate 项目中的 import.sql 文件

java - 应该包含/导入什么来识别我的 Spark -java 代码中的 "$"操作 join 函数?

java - Kryonet - 缓冲区溢出 - 小对象发送

java - 递归调用后变量值未更新

spring - 无法将自定义自动配置与 spring-data-elasticsearch 一起使用

java - 创建名称为 'serviceController' : Injection of autowired dependencies failed; 的 bean 时出错

java - Spring、MVC、Java,让单例 bean 每 5 秒执行一次操作?