gpt4 book ai didi

java - Spring MVC 没有检测到我的 Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:56 25 4
gpt4 key购买 nike

我有一个包含以下文件的 Spring MVC 项目:

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appconfig-root.xml</param-value>
</context-param>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

还有2个配置文件:

/src/main/webapp/WEB-INF/appconfig-root.xml

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

<import resource="appconfig-mvc.xml"/>

<context:component-scan base-package="yc.servlets"/>

<!--<context:property-placeholder location="classpath:application.properties"/>-->
</beans>

/src/main/webapp/WEB-INF/appconfig-mvc.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="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.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven/>

<mvc:resources mapping="/resources/**" location="/resources/"/>

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

<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

以及包含我的 Controller 的类:

package yc.servlets;

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

@Controller
public class TestPage {


@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
public String hello() {
return "hello";
}
}

我认为这足以让 spring mvc 正常工作,但是当我在浏览器中键入 localhost:8888 时,我收到了 404 错误。

localhost:8888/hello_there 也给出了 404 错误。

最佳答案

您好,您正在 Controller 中返回“hello”,它会找到 hello.jsp如果找不到它会抛出 404 http 错误

当您配置 InternalViewResolver 时:

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

<property name="suffix">
<value>.jsp</value>
</property>
</bean>

它试图在/WEB-INF/jsp/hello.jsp 中查找,所以当您在 Controller 中返回字符串“hello”时:

@RequestMapping(value = "/hello_there", method = RequestMethod.GET)
public String hello() {
return "hello";
}

它会为“hello”添加一个后缀“.jsp”,它会尝试寻找“hello.jsp”,因为找不到它会抛出 404

您可以将您的@Controller 更改为@RestController,它隐含地添加@ResponseBody 注释并查看它是否在响应中返回“hello”

此外,如果这对您没有帮助,您可以更改:

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

到这个:

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

如果它现在仍然有效,请尝试下一步:

试试这样的网址:

localhost:8080/yourApplicationName/hello_there

默认 conext-path 在这里是你的应用程序名称如果你去 TOMCAT_HOME/webapps/

你会发现根目录下没有你的应用

这就是为什么 localhost:8080/... 找不到它的原因

您可以通过两种方式解决此问题:

1.Find your application deployed and copy to root folder in 
TOMCAT_HOME/webapps/ROOT
2.See the name of your application in TOMCAT_HOME/webapps/
and call url : localhost:8080/yourAppName/hello_world

谢谢

关于java - Spring MVC 没有检测到我的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com