Spring MVC请求的资源不可用

时间:2017-02-14 16:54:41

标签: spring maven tomcat

我有一个带有单个控制器类(MainController.class)的war项目,它带有一个公共函数index(),它返回一个字符串,这是我最后一次检查你要转发到的视图的名称 - 以及一个单独的jsp命名" index.jsp"。视图位于/ 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>

我的配置xml文件:

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

出于某种原因,当我转到&#34; http://localhost:8080/Ushering/&#34;:404 - &#34时,我的浏览器出现错误:请求的资源不可用。&#34;

我无法在日志中找到任何内容(但必要时可以发布)告诉我哪些文件或资源无法使用。它似乎能够找到控制器,因为在一个日志文件中它提到了控制器的请求映射。它可能是什么?

编辑:WAR文件是在pom文件中定义的Ushering,也是在web.xml中定义的项目名称,所以我认为上下文也是Ushering以及Spring应用程序中的任何东西如果我将请求映射设置为/ Hello,那么URL应该是localhost:8080 / Ushering / Hello。

编辑:我的Web.xml文件

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

1 个答案:

答案 0 :(得分:1)

如果查看MainController类,只有一个请求处理方法返回&#34; index&#34;。它被映射到&#34; /&#34;这意味着当您键入 localhost:8080 时,它会为您提供索引文件。因此,不要输入 localhost:8080 / Ushering ,而是输入 localhost:8080 。它会工作。但是如果要输入 localhost:8080 / Ushering ,请将控制器文件中的请求映射更改为以下内容。

@RequestMapping(值=&#34; /迎来&#34)

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

它会返回索引文件。