gpt4 book ai didi

java - Spring MVC 教程不工作

转载 作者:行者123 更新时间:2023-11-28 22:58:26 25 4
gpt4 key购买 nike

我一直在查看 stackoverflow 中的几个教程和其他帮助,但我无法解决这个恼人的问题:我在 Eclipse 中创建了一个 Spring MVC 项目,遵循 this一步一步教程,但是我仍然从 Tomcat 服务器收到资源未找到错误。

WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<!-- Processes application requests -->
<servlet>
<servlet-name>onlab</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>onlab</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

WEB-INF/onlab-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:component-scan base-package="controller" />

<mvc:annotation-driven />

</beans>

src/controller/HomeControllerJava:

@Controller
public class HomeController {

@RequestMapping(value = "/")
public String home() {
System.out.println("HomeController: Passing through...");
return "home";
}
}

WEB-INF/views/home.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>

最后是 src/controller/Configurer.java:

@Configuration
public class Configurer {
@Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

上次同样的教程对我来说效果很好,但现在我一直在从 Apache 中获取未找到的资源。

最佳答案

创建一个 java 类 MyWebAppInitializer.java 并在此文件中添加您的 Configurer.java 然后测试

公共(public)类 MyWebInitializer 扩展了 AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { Configurer.class };
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}

关于java - Spring MVC 教程不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249355/

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