【问题标题】:Simple Servlet mapping简单的 Servlet 映射
【发布时间】:2011-05-26 12:06:11
【问题描述】:

我正在尝试在一个小应用程序上实现 Spring。 我得到以下信息:

WARNING: No mapping found for HTTP request with URI [/audiClave/] in DispatcherServlet with name 'appServlet'

这是我的 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_3_0.xsd" 
    id="WebApp_ID" 
    version="3.0">
    <display-name>audiClave</display-name>
<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-                            class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>      

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

这里是 servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.audiClave.Service" />

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

</beans>

控制器是:

package com.audiClave.Service;

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

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

@RequestMapping(value = "/")
public String home() {
    System.out.println("HomeController: Passing through...");
    return "WEB-INF/views/home.jsp";
}
}   

http://localhost:8080/audiClave/ 返回:

HTTP Status 404
The requested resource () is not available.

我使用tomcat 7.0.12 和spring 3.0.5 在eclipse 下运行。内容似乎已正确部署到 E:\development\java.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\audiClave

当我将映射更改为 /* 时,它能够找到控制器,但是 home.jsp 不起作用,因为通配符也匹配它

当我再次从映射中取出 /* 时已解决。 谢谢

【问题讨论】:

  • 您的应用程序是否部署在“webapps/audiClave”中?

标签: java spring servlets spring-mvc


【解决方案1】:

只做return "home"(而不是整个jsp路径)。视图处理程序应配置为在该文件夹中查找具有该扩展名的视图。

【讨论】:

  • 感谢您的快速回复,但似乎没有帮助。我仍然收到 26/05/2011 10:15:08 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在 DispatcherServlet 中找不到带有 URI [/audiClave/] 的 HTTP 请求的映射,名称为 'appServlet' 我没有'根本不认为它正在寻找 home() 方法
  • 我更新到 3.0.5 但没有帮助。当我将映射更改为 /* 时,它能够找到控制器,但是 home.jsp 不起作用,因为通配符也匹配它。
  • tomcat 7.0.5 之前的版本存在问题。你到底用的是哪个tomcat?
【解决方案2】:

问题应该是这样的:
在文件[filename]-servlet.xml中请输入以下行:
&lt; mvc:resources location="/index.html" mapping="/index.html" /&gt;

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 2017-10-15
    • 1970-01-01
    • 2011-11-01
    • 2016-03-03
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多