【发布时间】:2019-10-22 21:03:08
【问题描述】:
我正在尝试启动并运行带有 Tomcat 9.0.20 和 IntelliJ 2019.1 的 Hello World Spring MVC 4.3.18,以继续我的 Udemy 教程,不幸的是它使用了 Eclipse。如何通过控制器显示页面?
我检查了 web.xml、dispatcher-servlet.xml 和 applicationContext.xml 是否存在配置错误。 我检查了我的项目是否有错误,IntelliJ 说没有错误。 我从 GitHub 下载了一个正在运行的 IntelliJ 项目:https://github.com/vivlai/SpringMVC(将其更改为 java 12 和 tomcat 9)并成功运行。 我比较了所有文件、目录、配置,唯一的区别是使用的 spring 版本(4.3.10)和一些 xml 版本。
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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="spring.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
HomeController.java
package spring.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showPage() {
return "index";
}
}
所以部署成功,它注册了我的映射请求,并且 InternalResourceViewResolver 指向 WEB-INF 中的视图文件夹,它应该准备好一切了吗?
所以我希望在启动服务器时显示 index.jsp,但我得到了 404 附加说明:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。 任何帮助表示赞赏!
【问题讨论】:
标签: java spring spring-mvc intellij-idea tomcat9