简体   繁体   中英

Spring MVC The requested resource is not available

I have a war project with a single controller class (MainController.class) with a public function index() that returns a string that was the last time I checked the name of the view you were forwarding onto - and a single jsp named "index.jsp". The views are in /main/webapp/WEB-INF/views/jsp/. MainController.java:

@Controller
public class MainController
{
    @Autowired
    private ApplicationContextProvider mObjApplicationContextProvider;

    @Autowired
    private ScheduleService mObjScheduleService;

    protected ApplicationContext getApplicationContext()
    {
        return(getApplicationContextProvider().getApplicationContext());
    }

    protected ApplicationContextProvider getApplicationContextProvider()
    {
        return(mObjApplicationContextProvider);
    }

    protected Object getBean(final Class<?> clsBean)
    {
        return(getApplicationContext().getBean(clsBean));
    }

    protected Object getBean(final String sBeanName)
    {
        return(getApplicationContext().getBean(sBeanName));
    }

    protected ScheduleService getScheduleService()
    {
        return(mObjScheduleService);
    }

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

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        Hello World!
    </body>
</html>

My configuration xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="net.draconia.church" />

    <bean class="net.draconia.ApplicationContextProvider" />

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:sql9.freemysqlhosting.net/sql9158326" />
        <property name="username" value="sql9158326" />
        <property name="password" value="MqX7sbacgB" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

</beans>

For some reason I get the error in my browser when I go to " http://localhost:8080/Ushering/ ": 404 - "The requested resource is not available."

I can't find anything in the logs (but can post if necessary) to tell me WHAT file or resource is unable to be available. It seems to be able to find the controller because in the one log file it mentions the Request Mapping for the controller. What could it be?

Edit: The WAR file is Ushering as is defined in the pom file and also in the web.xml as the name of the project so I would think the context would be Ushering as well and anything in the Spring application side of things would be after /Ushering so if I set the requestmapping to /Hello then the URL should be localhost:8080/Ushering/Hello.

Edit: My Web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>CrunchifySpringMVCTutorial</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Ushering</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/Ushering-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Ushering</servlet-name>
        <url-pattern>/s</url-pattern>
    </servlet-mapping>
</web-app>

If you look at your MainController class there is only one request handling method which returns "index". It is mapped to "/" meaning that when you type localhost:8080 it gives you index file in return. Therefore instead of typing localhost:8080/Ushering enter localhost:8080 . It would work. But if you want to enter localhost:8080/Ushering then change the request mapping in controller file to following.

@RequestMapping(value="/Ushering")

public String index()
{
    return("index");
}

It would return you the index file.

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