2017-02-14 792 views
0

我有一個帶有公共函數索引()的單個控制器類(MainController.class)的戰爭項目,該函數返回一個字符串,該字符串是我最後一次檢查視圖的名稱正在轉發 - 和一個名爲「index.jsp」的jsp。這些視圖位於/ main/webapp/WEB-INF/views/jsp /中。 MainController.java:Spring MVC請求的資源不可用

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

出於某種原因,我得到了我的瀏覽器中的錯誤,當我去「http://localhost:8080/Ushering/ 「:404 - 」請求的資源不可用。「

我在日誌中找不到任何東西(但可以發佈,如有必要)告訴我什麼文件或資源無法使用。它似乎能夠找到控制器,因爲它在一個日誌文件中提到了控制器的請求映射。會是什麼呢?

編輯:WAR文件按照在pom文件中定義的方式啓動,也在web.xml中作爲項目的名稱,所以我會認爲上下文將是啓動以及任何在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> 
+0

你能發佈你的web.xml文件嗎? – dennislloydjr

+0

將其添加爲編輯 –

+0

我明白了!當我將web.xml -/s貼在底部附近的url-pattern中時,我注意到了一個錯字。它應該是/且只有/。我刪除了's',乾淨的編譯戰爭:戰爭編輯它,它出現了,因爲我期待:-D謝謝你在正確的方向戳我! –

回答

0

如果你看看你的MainController類只有一個請求處理方法,它返回「指數」。它被映射到「/」,這意味着當你鍵入localhost:8080它會給你索引文件作爲回報。因此,不要打字本地主機:8080 /使用輸入localhost:8080。它會工作。但是,如果您想要輸入localhost:8080 /引導然後將控制器文件中的請求映射更改爲以下內容。

@RequestMapping(值= 「/迎來」)

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

它會回報你的索引文件。

+0

我不明白這是怎麼回事 - 戰爭是Ushering.war,當我將war文件複製到webapps時,它會在Tomcat的WebApps文件夾下創建一個上下文啓動文件夾。如果我輸入http:// localhost:8080,我會看到「如果你看到這個,你已經正式安裝了Tomcat!恭喜!」頁。 –