为了账号安全,请及时绑定邮箱和手机立即绑定

无法访问 Spring 控制器

无法访问 Spring 控制器

慕妹3146593 2022-01-12 14:46:36
我正在尝试从头开始创建一个非常基本的应用程序的 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>

希望能帮助到你。


查看完整回答
反对 回复 2022-01-12
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

从 web.xml 中删除侦听器,因为您只有一个上下文 xml。如果要加载多个上下文 xml,请添加侦听器和上下文参数。


查看完整回答
反对 回复 2022-01-12
  • 2 回答
  • 0 关注
  • 211 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号