2015-12-02 83 views
0

我是新的Spring MVC javaServlet Spring MVC java - 不能訪問控制器和視圖

配置(我做了什麼)

  • 進口彈簧庫
  • 進口共記錄
  • Tomcat服務器(可以訪問本地主機:8080)

問題遇到

我可以在網絡內容訪問的index.jsp沒有問題,但是當在WEB-INF,服務器顯示訪問的hello.jspHTTP狀態404,網址在http://localhost:8080/APK_downloader/WEB-INF/jsp/hello.jspproblem image

停止

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 

    <display-name>APK downloader</display-name> 

    <servlet> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

彈簧調度-servlet.xml中(servlet配置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" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.2.3.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.3.xsd"> 

    <context:component-scan base-package="solutionView"/> 

    <bean id="HanlderMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

DBController.java(java的資源類)

package solutionView; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping(value = "/hello") 
public class DBController { 

    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public ModelAndView getModelView(){ 
     System.out.println("helllo"); // check point 
     return new ModelAndView("hello"); 
    } 

} 

的hello.jsp(視圖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> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>Hello</h1> 
<h2>${message}</h2> 
</body> 
</html> 
+4

您不能訪問WEB-INF文件直接。嘗試到訪問http://本地主機:8080/APK_downloader /你好 –

+1

請仔細閱讀本[問題](http://stackoverflow.com/questions/25904298/why -jsp-files-inside-web-inf-folder-works-but-under-a-folder-under-web),它會幫助你很多。 –

+0

@rupesh_padhye我不想用url訪問,有沒有通過控制器訪問jsp的方法? –

回答

1

請刪除此行並直接在URL中寫入/ hello字符串,它將重定向到hello.jsp頁面

@RequestMapping(value = "/hello") 
+0

感謝您的糾正,但它無法打開爲默認hello.jsp 但它有助於將來重定向url頁面。 @Controller @RequestMapping( 「/你好」) Publicpublic類DBController {} 你 –

0

您無法直接訪問WEB-INF下的文件,您必須讓servlet管理它。

按URL映射,你需要知道一個method的映射來擁有它Controller

例如映射後:

@Controller 
@RequestMapping(value = "/hello") 
public class DBController { 

    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public ModelAndView getModelView(){ 
     System.out.println("helllo"); // check point 
     return new ModelAndView("hello"); 
    } 

} 

手段getModelView()將響應以下網址:

http://localhost:8080/APK_downloader/hello/hello 

刪除dupl icated你好,你有兩個選擇,要麼從控制器刪除

@RequestMapping(value = "/hello") 

,或保持它,改變你的方法如下:

@RequestMapping(value = "", method = RequestMethod.GET) 
0

改變你的控制器類是這樣的:

@Controller 
public class DBController { 

    @RequestMapping(value = "/hello") 
    public String displayHelloPage(){ 
     return "hello"; 

     } 

} 

現在,如何調用這個URL。 localhost:8080/context_path/hello. 您必須檢查在eclipse集中的服務器中是否有上下文路徑。如果上下文路徑是/,那麼只需localhost:8080/hello。我希望這有助於,如果不是,我知道,我會刪除我的答案。

+0

讓我知道上下文路徑可以從**項目=>屬性更改=> Web項目的設置=>上下文根**感謝。 –

+0

但是我告訴你它在Servers中,因爲我使用Intellij Idea,我不記得它到底是哪裏。 –

+0

我試過這兩個網址..它不起作用。 –

1

首先,您不能直接訪問WEB-INF文件夾。所以http://localhost:8080/APK_Downloader/WEB-INF/jsp/hello.jsp將無法​​工作

第二你註釋你的處理程序與@RequestMapping(value = "/hello")以及您的控制器類。因此,對於訪問處理程序的完整的URL

http://localhost:8080/APK_Downloader/hello/hello 

如果要直接訪問對於您的上下文方法只是從控制器

刪除 @RequestMapping(value = "/hello")

,改變你的看法解析器認定中

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

你錯過了前綴的JSP部分

1

更改彈簧調度-servlet.xml中

<bean id="HanlderMapping" 
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 

<mvc:annotation-driven /> 

由於您使用的註解驅動的MVC控制器(即 @RequestMapping,@Controller)。這就是爲什麼您需要在spring-dispatcher-servlet.xml中聲明以上 聲明。