gpt4 book ai didi

java - Spring Web应用程序初始化器

转载 作者:行者123 更新时间:2023-12-01 10:14:41 25 4
gpt4 key购买 nike

最近在学习基于spring java的配置。我尝试用 WebConfigWebApplicationInitializer 替换 web.xml

每当我请求网址时:http://localhost:8080/spring-demo/greeting.html我收到 404 描述 请求的资源不可用。以下是我的项目详细信息。

WebConfig.java

package com.soumya.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.soumya.spring")
public class WebConfig {

}

WebAppInitializer.java

package com.soumya.spring;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
}

private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.soumya.spring.WebConfig");
return context;
}

}

Controller

package com.soumya.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping(value = "/greeting")
public String greeting(Model model) {
model.addAttribute("greeting", "Hello World!");
return "greeting.jsp";
}

}

项目结构图

Project Structure

最佳答案

首先,您的网址末尾不应有 .html 或 .jsp。网址将为http://localhost:8080/spring-demo/greeting

在您的特定 WebApplicationInitalizer 中,您的 DispatcherServlet 应映射到 /greeting Controller 而不是“*.html ”。

dispatcher.addMapping("/greeting");

此外,您的 DispatcherServlet(代码中的 WebConfig.class)没有定义 ViewResolver Bean。 ViewResolver 负责将 View 名称(如 HelloController 返回的“greeting.jsp”)映射到 View 实现(实际的“greeting.jsp”文件,可能存在于/WEB-INF/文件夹中。

我已经更改了您的 WebConfig 类以包含 ViewResolver bean-

package com.soumya.spring;

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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.soumya.spring")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {

InternalResourceViewResolver irvr = new InternalResourceViewResolver();

irvr.setPrefix("/WEB-INF/views/"); //your "greeting.jsp" file should be here
irvr.setSuffix(".jsp");
irvr.setExposeContextBeansAsAttributes(true);
return irvr;

}
}

关于java - Spring Web应用程序初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971082/

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