我正在嘗試從頭開始創建一個非?;镜膽贸绦虻?Spring RESTful API,但我無法訪問控制器。我可以訪問 JSP 文件但不能訪問控制器。我也嘗試過注釋,@RestController但它沒有用。我在Tomcat 8上運行。錯誤是:源服務器沒有找到目標資源的當前表示或不愿意透露存在的表示。WEB.xml <?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"> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>調度程序-servlet.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.controller /> <mvc:default-servlet-handler /></beans>
2 回答

墨色風雨
TA貢獻1853條經驗 獲得超6個贊
如果要單獨創建應用程序上下文,則應提供上下文參數和值作為 context.xml 文件的位置。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
對于您的錯誤,控制器無法訪問,可能是由于:-
<context:component-scan
base-package="com.controller />
檢查您是否編寫了正確的基本包名稱或嘗試使用
<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
希望能幫助到你。
添加回答
舉報
0/150
提交
取消