gpt4 book ai didi

java - Spring MVC 基于注释的配置不起作用

转载 作者:行者123 更新时间:2023-11-30 03:04:51 25 4
gpt4 key购买 nike

我尝试在 this tutorial 的帮助下创建一个简单的 Spring MVC 项目但是当我部署时,它显示 404 服务器错误。由于我是 spring MVC 的初学者,尽管浏览了这里和其他地方的所有答案,我仍无法找出项目中的问题。我将非常感谢您的帮助在弄清楚。

内容如下:

  • POM.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>deevigeweb</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>deevigeweb Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>

    </dependencies>
    <build>
    <finalName>deevigeweb</finalName>
    </build>
    </project>
  • Servlet 初始化程序(DeevigeServletInitializer.java)

    package com.deevigeweb.config;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class DeevigeServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

    }
  • Web MVC 配置(DeevigeWebMVCConfig.java)

    package com.deevigeweb.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;

    @EnableWebMvc
    @Configuration
    @ComponentScan({"com.deevigeweb"})
    public class DeevigeWebMVCConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

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

    }
  • Controller (SearchViewController.java)

    package com.deevigeweb.controllers.webapp;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    public class SearchViewController {
    @RequestMapping(value="/",method=RequestMethod.GET)
    public ModelAndView doLanding(){
    return new ModelAndView("searchview");
    }
    }
  • JSP(searchview.jsp)

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Welcome</title>
    </head>
    <body>
    <h1>Welcome</h1>
    </body>
    </html>
  • 目录结构 project structure

最佳答案

也许添加尾部斜杠对您有用:

resolver.setPrefix("/WEB-INF/views/");

还在您的 SearchViewController 类上添加 @Controller 注释

关于java - Spring MVC 基于注释的配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045687/

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