【问题标题】:Spring MVC Setup not working - Path issue?Spring MVC 设置不起作用 - 路径问题?
【发布时间】:2013-01-06 03:58:04
【问题描述】:

当我输入 /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;
 }  
}

【问题讨论】:

  • 类路径中有弹簧罐吗?从 tomcat 日志中发布异常跟踪。
  • 罐子不是问题。一切都很好。我解决了。

标签: java spring jsp model-view-controller


【解决方案1】:

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

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

现在它就像一个魅力。 希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2023-02-21
    • 2012-08-04
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多