简体   繁体   English

Spring MVC应用程序中的404错误

[英]404 error in Spring MVC application

I have created a simple spring MVC application without using any build tools like maven or ant in eclipse EE IDE 我创建了一个简单的spring MVC应用程序,而未在Eclipse EE IDE中使用任何构建工具(如maven或ant)

The application consist of only one controller class, 2 XML's ( web.xml and spring-servlet.xml ) and a jsp page( hellopage.jsp ) 该应用程序仅包含一个控制器类,2个XML类( web.xmlspring-servlet.xml )和一个jsp页面( hellopage.jsp )。

I am using tomcat and 6.0 eclipse galileo. 我正在使用tomcat和6.0 eclipse galileo。

In my spring-servlet.xml file I have mentioned <servlet-name> as "Spring" and <servlet-class> as org.springframework.web.servlet.DispatcherServlet , <url-pattern> as *.html , whereas in my web.xml file welcome file is index.jsp which has a link( <a href="hello.html">click</a> ) to hello.html . 在我的spring-servlet.xml文件中,我曾提到<servlet-name>为“ Spring”和<servlet-class>org.springframework.web.servlet.DispatcherServlet<url-pattern>*.html ,而在我的web.xml文件欢迎文件是index.jsp ,该文件具有指向hello.html的链接( <a href="hello.html">click</a> )。

My controller class has request mapping as ( "/hello" ), when in the browser after deploying the war file of my application I hit the URL localhost:8080/projectname the index.jsp page pops up with a link "click" , but after clicking on this link I get a 404 error mentioning " servlet spring is not available " means the dispatcher servlet which I mentioned in spring-servlet.xml file, please can anyone help? 我的控制器类的请求映射为( "/hello" ),当在浏览器中部署了我的应用程序的war文件后,我点击URL localhost:8080/projectnameindex.jsp页面以链接"click"弹出,但是单击此链接后,我收到一个404错误,提示“ servlet弹簧不可用 ”,这意味着我在spring-servlet.xml文件中提到的调度程序servlet,任何人都可以帮忙吗?

Here is the code: 这是代码:

web.xml: 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">
    <display-name>SpringMVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

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

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

HelloWorldController.java: HelloWorldController.java:

package com.samar.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")   
    public ModelAndView helloworld()
    {
        String message ="Hello spring MVC...!!";

        return new ModelAndView("hellopage","message",message);
    }

}

spring-servlet.xml: 为spring-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation ="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.samar.controllers"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <a href="/hello.html">Click</a>
</body>
</html>

hellopage.jsp: hellopage.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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    Message is ${message}
</body>
</html>

You have to tell Spring to read your @Controller and other annotations. 您必须告诉Spring读取您的@Controller和其他注释。

This can be done by adding 这可以通过添加

<context:annotation-config/>

to your spring-servlet.xml 到您的spring-servlet.xml

"servlet spring not available" means your app didn't start up correctly. “ Servlet弹簧不可用”表示您的应用未正确启动。

Check your tomcat log for details. 检查您的tomcat日志以获取详细信息。

This may have info you need: http://forum.spring.io/forum/spring-projects/web/41340-beginner-s-problem-servlet-is-not-available 这可能包含您需要的信息: http : //forum.spring.io/forum/spring-projects/web/41340-beginner-s-problem-servlet-is-not-available

谢谢大家,终于解决了实际问题,除了在项目的构建路径中添加“ spring core”和“ spring web”罐子之外,我还必须将它们添加到WEB-INF下的lib文件夹中,并且现在可以使用了,这就是为什么Tomcat无法找到调度程序servlet从而导致404错误的原因。我希望答案能帮助像我这样的初学者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM