java 请求的资源不可用 Spring 3 MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10800302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:37:35  来源:igfitidea点击:

The requested resource is not available Spring 3 MVC

javaspringspring-mvc

提问by Paulius Matulionis

I am getting the requested resource () is not available displayed in the browser and I can not understand what I am doing wrong.

我在浏览器中显示请求的资源 () 不可用,我无法理解我做错了什么。

This is my applicationContext-dispatcher.xml:

这是我的 applicationContext-dispatcher.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

</beans>

This is my web.xml:

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext-main.xml
            /WEB-INF/applicationContext-hibernate.xml
            /WEB-INF/applicationContext-jms.xml
            /WEB-INF/applicationContext-i18n.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext-dispatcher.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>

</web-app>

This is the redirect.jsp:

这是redirect.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("index.htm"); %>

And the index.jsp page is located under the WEB-INF/jsp.

index.jsp 页面位于 WEB-INF/jsp 下。

When open the browser I see the 404 error code generated from tomcat ant the message that resource is not available. I don't understand what is wrong. Any ideas?

打开浏览器时,我看到 404 错误代码是由 tomcat ant 生成的资源不可用的消息。我不明白出了什么问题。有任何想法吗?

回答by Priyank Doshi

Resources which are inside the WEB-INFfolder aren't accessible to the outside world (JSP file in your case). Means that you can not simply hit the URL and see the JSP file.

外部世界无法访问WEB-INF文件夹内的资源(在您的情况下为 JSP 文件)。意味着您不能简单地点击 URL 并查看 JSP 文件。

For example, you have the following JSP,

例如,您有以下 JSP,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

then you can not simply hit the browser using:

那么你不能简单地使用以下方法点击浏览器:

http://localhost:8080/project-name/WEb-INF/nameofpage.jsp

It will throw the same error. Though you can programmatically access those resources inside WEB-INF folder.

它会抛出同样的错误。虽然您可以以编程方式访问 WEB-INF 文件夹中的这些资源。

Write the following controller:

编写以下控制器:

package your.apckage.name;

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

@Controller
public class SampleController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String sample() {
        return "index";
    }
}

And add the following line in spring -config file:

并在 spring -config 文件中添加以下行:

<context:component-scan
    base-package="package name of the controller" />

回答by pap

So, have you defined any controller mapping for index.htm? Spring won't "automatically" forward the request to your index.jsp view, you need to define a controller to handle the index.htm url.

那么,您是否为 index.htm 定义了任何控制器映射?Spring 不会“自动”将请求转发到您的 index.jsp 视图,您需要定义一个控制器来处理 index.htm url。

@Controller
public class IndexController {

    @RequestMapping("/index.htm")
    public String handleIndexGet() {
        return "index"; // forward to view index.jsp
    }

}

回答by adarshr

Shouldn't it be

不应该是

<% response.sendRedirect("index.jsp"); %>

Also, since you're doing a redirect, the index.jsphas to be present outside WEB-INF to be accessible to your browser.

此外,由于您正在执行重定向,因此index.jsp必须存在于 WEB-INF 之外才能被您的浏览器访问。

回答by Priyank Doshi

Generally requested resource not founderror comes when there is a problem in URL mapping. When the requested URL does not match to any of the URL patterns, it throws this exception.

URL 映射有问题时,通常会出现请求的资源未找到错误。当请求的 URL 与任何 URL 模式不匹配时,它会抛出此异常。