【问题标题】:Spring MVC with Eclipse Jetty integration 8 - No mapping found for HTTP request with URI带有 Eclipse Jetty 集成 8 的 Spring MVC - 找不到带有 URI 的 HTTP 请求的映射
【发布时间】:2018-09-27 07:22:16
【问题描述】:


我正在使用 Spring mvc 和 IDE (Rad 8.5.5) 和 Jetty Integration 版本 8 来创建一个小型 Web 应用程序。
当我尝试提交请求 http://localhost:9090/testJetty/index 时,Jetty 返回以下错误消息:WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/testJetty /WEB-INF/views/index.jsp] 在 DispatcherServlet 中,名称为“appServlet”

这是我的代码:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    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_3_0.xsd">
    <display-name>testJetty</display-name>

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

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

app-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:component-scan base-package="test"/>

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

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
        <beans:property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
    </beans:bean>
</beans:beans>

控制器:

package test;

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

@Controller
public class test {

    @RequestMapping(value = "/index")
    public String index(Model model) {
        return "index";
    }
}


以下要求不能更改: 1. 使用jdk 1.6编译的项目 2.春季版4 3. Javax-servlet-api-3.1.0

我不知道是什么问题。有任何想法吗? 谢谢。

【问题讨论】:

    标签: jsp spring-mvc embedded-jetty


    【解决方案1】:

    由于我们没有看到您的项目结构,因此可能不正确。

    但我猜是因为你的jsp文件在WEB-INF下,你需要在你的spring配置文件中添加如下代码:

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

    还将您的调度员从/* 更改为/ in web.xml

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

    【讨论】:

    • 你是对的!我忘记了上面一段 app-servlet.xml 的代码。我编辑了我的问题。
    • 我做到了,但我认为我之前至少要达到 15 个声望。
    • 你可以。接受或投票。你可以试试。谢谢
    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 2013-09-16
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2013-07-27
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多