项目描述
一个简单的Spring MVC项目,关键代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringApplicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/m/*</url-pattern>
</servlet-mapping>
</web-app>
SpringApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.mycompany"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain; charset=utf-8</value>
</list>
</property>
</bean>
<bean class="com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:view-resolvers>
<mvc:content-negotiation>
<mvc:default-views>
<bean class="com.alibaba.fastjson2.support.spring.webservlet.view.FastJsonJsonView"/>
</mvc:default-views>
</mvc:content-negotiation>
<ref bean="thymeleafViewResolver"/>
</mvc:view-resolvers>
</beans>
ThymeleafConfig
@Configuration
open class ThymeleafConfig {
@Bean
open fun springResourceTemplateResolver(): SpringResourceTemplateResolver =
SpringResourceTemplateResolver().apply {
templateMode = TemplateMode.HTML
isCacheable = true
order = 1
characterEncoding = "UTF-8"
prefix = "/WEB-INF/templates/"
suffix = ".html"
}
@Bean
open fun springTemplateEngine(): SpringTemplateEngine =
SpringTemplateEngine().apply {
setTemplateResolver(springResourceTemplateResolver())
}
@Bean
open fun thymeleafViewResolver(): ThymeleafViewResolver =
ThymeleafViewResolver().apply {
order = 1
characterEncoding = "UTF-8"
templateEngine = springTemplateEngine()
}
}
PathPatternParserConfig
@Configuration
@EnableWebMvc
open class PathPatternParserConfig : WebMvcConfigurer {
override fun configurePathMatch(configurer: PathMatchConfigurer) {
configurer
.setPatternParser(PathPatternParser())
}
}
UrlController
@Controller
@RequestMapping("/m")
class UrlController {
@GetMapping("")
fun index() = "index"
@GetMapping("/hello")
fun hello(): String = "hello"
}
操作环境
apache-tomcat-9.0.85
Java 17
Spring 5.3.34
问题描述
访问:http://localhost:8080/m 成功
访问:http://localhost:8080/m/hello 失败, 如下图
请问问题出在哪里?