繁体   English   中英

Spring WebApplicationInitializer

[英]Spring WebApplicationInitializer

最近,我一直在学习基于Spring Java的配置。 我试图用WebConfigWebApplicationInitializer替换web.xml

每当我请求url时: 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;
    }

}

控制者

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";
    }

}

项目结构图

项目结构

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

在特定的WebApplicationInitalizer中,应该将DispatcherServlet映射到/greeting控制器,而不是“ * .html”。

dispatcher.addMapping("/greeting");

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

我已将您的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;

    }
}

带注释的初始化示例。

    public class Initializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Creates context object
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

        // Registers annotated configurations class
        ctx.register(Configurations.class);

        // Sets ContextLoaderListener to servletContext
        servletContext.addListener(new ContextLoaderListener(ctx));

        // Passes servlet context to context instance
        ctx.setServletContext(servletContext);

        //Registers dispatch servlet and passes context instance
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));

        //Maps URL pattern
        servlet.addMapping("/");

        //Sets creation priority
        servlet.setLoadOnStartup(1);

        //Registers security filters
        FilterRegistration.Dynamic security = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());

        // Sets dispatcher types a security filters to be applied
        EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
        security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
    }
}

如果您使用的是Spring MVC最新版本,请使用以下内容替换configlocation

   from context.setConfigLocation("com.soumya.spring.WebConfig");

到context.register(WebConfig.class)。 它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM