HTTP状态404 - 在Spring MVC 4应用程序中找不到

时间:2017-07-24 10:45:48

标签: java spring spring-mvc dispatcher

我有使用Maven的NetBeans Spring MVC项目。但我收到以下错误:HTTP状态404 - 未找到。我怎样才能避免这个错误? 层次结构:

-FinalWebStore
   -Web Pages
     -WEB-INF
          +views
          web.xml
          mvc-dispatcher-servlet.xml
   -Source Packages
      +com.karans.finalwebstore.controllers
      +com.karans.finalwebstore.daoimps
      +com.karans.finalwebstore.daos
      +com.karans.finalwebstore.models
   +Dependecies
...

以下是我的文件: 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 Web MVC Application</display-name>


    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>



    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

</web-app>

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


   <bean name="/login"
        class="com.karans.finalwebstore.controllers.UserController" />


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean id="ds"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/webstore" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>


    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="ds"></property>  
    </bean>  


    <bean id="ProductDAOimpl" class="com.karans.finalwebstore.daoimps.ProductDAOimp">

        <property name="template" ref="jt"></property>
    </bean>

    <bean id="UserDAOimpl"    class="com.karans.finalwebstore.daoimps.UserDAOimp">

        <property name="template" ref="jt"></property>  
    </bean>
    <bean id="CartDAOimpl" class="com.karans.finalwebstore.daoimps.CartDAOimp">

        <property name="template" ref="jt"></property>
    </bean>
    <bean id="UserController"    class="com.karans.finalwebstore.controllers.UserController">
        <property name="userdaoimp" ref="UserDAOimpl"></property>
    </bean>



</beans>

UserController.java:。

    package com.karans.finalwebstore.controllers;
import com.karans.finalwebstore.daoimps.UserDAOimp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
    @Autowired
    UserDAOimp userdaoimp;

    public void setUserdaoimp(UserDAOimp userdaoimp) {
        this.userdaoimp = userdaoimp;
    }

    public String login(){


        return "login";
    }


}

的login.jsp:

<%-- 
    Document   : login
    Created on : Jul 15, 2017, 2:57:13 PM
    Author     : raziyeh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>LOG IN PAGE</title>
    </head>
    <body>
        <h1>Hello World Taze avvaleshe!</h1>
    </body>
</html>

我认为其他文件并不重要。

2 个答案:

答案 0 :(得分:2)

将您的登录功能更改为

@RequestMapping(value = "/")
public String login(){
    return "login";
}

同时更改以下部分中的web.xml文件:

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

您需要从mvc-dispatcher-servlet.xml

删除以下行
<bean name="/login"
        class="com.karans.finalwebstore.controllers.UserController" />

此外,请确保login.jsp文件夹中包含/WEB-INF/views/文件。

答案 1 :(得分:0)

尝试以下方法:

将Spring MVC名称空间添加到mvc-dispatch-servlet.xml文件xmlns:mvc =&#34; http://www.springframework.org/schema/mvc"。您需要更新xsi:schemaLocation以及#34; http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd&#34;

第二次添加&lt; mvc:annotation-driven /&gt;到文件。如果我没记错的话,这可以处理@RequestMapping注释。

第三,删除名称为&#34; / login&#34;声明的bean。它与在文件底部声明的UserController的类型相同。