【问题标题】:Why is my JSP view not being resolved?为什么我的 JSP 视图没有被解析?
【发布时间】:2011-07-11 15:34:56
【问题描述】:

我的 dispatcher-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"
        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-3.0.xsd">
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.example" />

    <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView"/>  

</beans>

这是我的控制器

package com.example.spring;

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

@Controller
public class HelloController {

        @RequestMapping("/form.htm")
        public ModelAndView hello(Model model) {
                return new ModelAndView("index");
        }
}

当我在调试应用程序时,我在控制台中看到了这一点。

WARNING: No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcher'

控制器被击中,并返回视图,但无法解析。

【问题讨论】:

  • 你能从 web.xml 中发布你的调度程序 servlet 的映射配置吗
  • 你能发布项目的结构以及你的 web.xml 吗?
  • @Javi:相关,但我的 servlet 配置已设置为将 /*.htm 映射到我的调度程序 servlet,而不是 /*

标签: java model-view-controller spring spring-mvc


【解决方案1】:

听起来您的web.xml 中可能缺少以下内容

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/views/*</url-pattern>
</servlet-mapping>

正如@Javi 已经评论的那样,这是already answered here

【讨论】:

  • 我无法从 Spring 的文档中推断出这一点。你有参考吗?
  • 我不确定您的 web.xml 是什么样子,但假设您有一个 / 的 url 模式被映射到 DispatcherServlet。有一个 previous answer 解释了 servlet 映射。基本上我认为 JSP 是通过 DispatcherServlet 路由的,因为没有显式映射。上面的代码添加了将/WEB-INF/views/ 下的所有内容路由到JspServlet 的映射。我想我得到了原始信息here
【解决方案2】:

将 index.jsp 添加到您的 /WEB-INF/views/ 目录中。

【讨论】:

  • @stevebot - 一切似乎都很好,请确保文件已部署到服务器,并且不仅在您的开发环境中。
猜你喜欢
  • 2011-08-19
  • 1970-01-01
  • 2020-04-06
  • 2021-03-21
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2010-11-14
相关资源
最近更新 更多