0

当我输入 /localhost:8080/springapp2/ 时,我收到以下消息

 HTTP Status 404 - Servlet spring is not available

甚至根 URL 也不起作用。

这是我的文件...

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     id="WebApp_ID" version="2.5">
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
     <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<display-name>Spring MVC Sample</display-name>
</web-app>

spring-servlet.xml

 <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:annotation-config />
<context:component-scan base-package="org.nara.controllers"/>

<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>    
</beans>

控制器类

 package org.nara.controllers;

 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 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;

 @Controller
 @RequestMapping (value="/")
 public class HelloWorldControllder {

   public String HelloWorld() {
    return "hello";
}

@RequestMapping(value="/greetme", method = RequestMethod.GET)
public ModelAndView handlePOST( HttpServletRequest request, HttpServletResponse response) {
    ModelAndView mav = new ModelAndView("index.jsp");
    mav.addObject("Greeting", "Hello you");
    return mav;
 }  
}
4

1 回答 1

0

我解决了这个问题。我改变了几件事。

  • 将 URL 模式更改为/
  • 将 spring 表达式 jar 添加到类路径
  • ContextConfigLocation URL 存在问题。我更新了

现在它就像一个魅力。希望这可以帮助某人。

于 2013-01-06T04:41:00.593 回答