Logan
Logan

Reputation: 2505

Unable to load html pages when using Spring annotation config

I'm working on a POC using Spring with Annotation based configuration. While working on it, I'm facing below issues:

I am not sure if I should add another dispatcher servlet and add one mapping in it. I also tried passing both the mappings in the dispatcher servlet, but it didn't work.

Maybe someone can help me with the issue.

Below is the code:

AppConfig

package com.upload.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(WebConfig.class)
@ComponentScan(basePackages= "com.upload")
public class AppConfig {
}

WebConfig

package com.upload.config;

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

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
}

ServletInitializer

public class ServletInitializer extends AbstractDispatcherServletInitializer {

@Override
protected WebApplicationContext createServletApplicationContext() {
    final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(AppConfig.class);
    context.getEnvironment().setActiveProfiles("prod");
    return context;
}

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

@Override
protected WebApplicationContext createRootApplicationContext() {
    return null;
}
}

FileUploadController

@Controller
@RequestMapping("/ws")
public class FileUploadController {

@RequestMapping(value="hello",method=RequestMethod.GET)
public String hello(){
    return "Hello";
}

}

Upvotes: 1

Views: 462

Answers (1)

Deepak
Deepak

Reputation: 1768

Use Following Approach: You need to add addResourceHandler to pick up the html page from the web-inf structure WEB-INF/views/viewer where all your pages like: js/ html /css/ images etc are present. You can remove your AppConfig .java shown above.

package com.Configuration.si.config;

@Configuration
@ComponentScan("com.Configuration.si")
@EnableWebMvc
public class Configuration extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/viewer/**","/lib/**","/js/**","/images/**","/css/**","/swf/**")
            .addResourceLocations("/WEB-INF/views/viewer/","/WEB-INF/views/lib/","/WEB-INF/views/js/","/WEB-INF/views/images/","/WEB-INF/views/css/","/WEB-INF/views/swf/")
            .setCachePeriod(315569126);
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

Also Add this class to your structure

package com.Configuration.si.config;

@Configuration
public class WebMvcConfiguration  {

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

Internal view resolver will resolve the page you need using model.setViewName("login"); in controller. Hope it will help you

Upvotes: 3

Related Questions