简体   繁体   中英

The requested resource is not available Spring 3 MVC

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:

<?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:

<?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:

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

And the index.jsp page is located under the 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?

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" />

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
    }

}

Shouldn't it be

<% 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.

Generally requested resource not found error 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM