Spring MVC 404错误http请求

PoppinDouble

我是java Spring框架的新手,正在创建简单的重定向application。但是我收到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>

dispatcher-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");
    }

}

login.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>

应该扫描除控制器以外的所有内容(请注意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>

仅扫描控制器,注意include-filteranduse-default-filters="false"属性。对那些讨厌的视图控制器使用启用@RequestMapping解析<mvc:annotation-driven /><mvc:view-controller />

有关mvc名称空间的更多信息,我建议您阅读参考指南这一部分

另一个技巧是使用无版本的xsd文件而不是已版本化的xsd文件,这将照顾到使用classpath上可用的最新版本的xsd。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章