1

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

这是我的 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>

这是我的 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>

这是redirect.jsp:

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

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

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

4

4 回答 4

4

Resources which are inside the WEB-INF folder 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.

For example, you have the following 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.

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:

<context:component-scan
    base-package="package name of the controller" />
于 2012-05-29T13:58:27.887 回答
3

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.

@Controller
public class IndexController {

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

}
于 2012-05-29T14:08:47.080 回答
1

难道不应该

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

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

于 2012-05-29T13:41:50.420 回答
0

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

于 2012-05-29T13:40:21.640 回答