2012-01-13 22 views
5

Tôi hoàn toàn là người mới sử dụng Java và Spring và tôi muốn học từ ví dụ.Không tìm thấy ánh xạ nào cho yêu cầu HTTP với URI [/ myproject /] trong DispatcherServlet với tên 'appServlet'

Tôi đang sử dụng một out-of-the-box cấu hình/cài đặt

  • Mac OSX
  • SpringSource Tool Suite như IDE
  • Xuân 2.8.1.RELEASE
  • vfabric-tc -server-developer-2.6.1.RELEASE

Tôi đã cố gắng tạo một dự án mới dựa trên "Spring Template Project". Sau đó, tôi đã chọn "Spring MVC Project". Dự án mẫu được tạo ra. Sau đó, mà không sửa đổi bất cứ điều gì, tôi đã cố gắng để thực hiện de "home.jsp" trang thông qua "Run As". Máy chủ Web khởi động và cuối cùng tôi đã nhận được lỗi trong bảng điều khiển Tab.

Không có ánh xạ tìm thấy cho yêu cầu HTTP URI [/ myproject /] trong DispatcherServlet với tên 'appServlet'

Và đầu ra khác này trong các trang web:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

enter image description here

đây bạn có thể thấy một hình ảnh về cách dự án của tôi được cấu trúc (tự động tạo ra cho STS):

enter image description here

Điều gì là sai?

đây bạn có thể xem nội dung của các web.xml file:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

Các gốc context.xml tập tin có nội dung này.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    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-3.0.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 

</beans> 

Và cuối cùng là servlet-context.xml có nội dung này.

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.mycompany.myapp" /> 

</beans:beans> 

Có ai có ý tưởng giải quyết không?

Trả lời

0

Mọi thứ trong WEB-INF không thể truy cập từ bên ngoài và điểm của khung MVC là gọi bộ điều khiển trước khi gửi đến khung nhìn, vì vậy hãy gọi trực tiếp JSP không nên được thực hiện.

Và có thể bạn không có bất kỳ trình điều khiển Spring nào được ánh xạ tới URL gốc, vì vậy hiển nhiên, không có gì ở URL http://localhost:8080/myproject/.

+0

@AlfonsoDominguez @duffymo Dự án được tạo tự động có tệp có tên "HomeController.java" với nội dung sau: ' gói com.mycompany.myapp; [...] OMITED PART [...] /** * Xử lý yêu cầu cho trang chủ ứng dụng. */ @Controller public class HomeController { \t \t \t/** \t * Đơn giản chỉ cần chọn xem nhà để render bằng cách trả lại tên của nó. \t */ \t @RequestMapping (value = "/", method = RequestMethod.GET) \t public String nhà (Locale locale, mô hình Model) { \t \t String str = "Thông báo máy chủ"; \t \t \t \t model.addAttribute ("serverTime", str); \t \t \t \t trả lại "nhà riêng"; \t} \t } ' Là những gì bạn nói? –

+0

yep, đó là ý của tôi, bạn có cố gắng truy cập URL 'http: // localhost: 8080/myproject/home' không? URL đó có vẻ là URL chính xác từ các tệp cấu hình của bạn. –

1

Quy ước của Spring là giả định rằng <servlet-name> trên web.xml cho số DispatcherServlet khớp với phần đầu của tệp XML ngữ cảnh Spring servlet. Đổi tên servlet-context.xml thành appServlet-context.xml và xem điều đó có hữu ích không.

+0

Thử rằng tôi gặp lỗi tương tự. Trong thực tế, trong tệp web.xml, có một dòng chỉ định tên của servlet ... Bây giờ tôi đã thay đổi cho một trong những bạn đã đề xuất (cộng với việc đổi tên tệp) ... nhưng kết quả là như nhau . \t \t \t contextConfigLocation \t \t \t /WEB-INF/spring/appServlet/appServlet-context.xml

0

Thêm bộ điều khiển vào ứng dụng của bạn trả về phiên bản ModelAndView có tên "nhà riêng". Sau đó cấu hình một số ánh xạ trình xử lý cho Bộ điều khiển đó và thử truy cập ứng dụng web của bạn bằng một URL tương tự như URL này: http://localhost:8080/myproject/home.do. Bạn có thể tìm thêm thông tin herehere.

Các vấn đề liên quan