Reputation: 31
Recently I've been learning spring java based configuration. I tried to replace the web.xml with WebConfig and WebApplicationInitializer.
Whenever I'm requesting the url: http://localhost:8080/spring-demo/greeting.html I'm getting 404 description The requested resource is not available. Below are my project details.
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 Image
Upvotes: 1
Views: 16482
Reputation: 141
First of, your url should not have .html or .jsp at the end. The url will be http://localhost:8080/spring-demo/greeting
In your specific WebApplicationInitalizer, your DispatcherServlet should be mapped to /greeting
controller instead of " *.html ".
dispatcher.addMapping("/greeting");
Also, Your DispatcherServlet (WebConfig.class in your code) does not have a ViewResolver Bean defined. The ViewResolver is in charge of mapping a view name( like 'greeting.jsp' which is returned by your HelloController) to the view implementation( the actual 'greeting.jsp' file which could be present in the /WEB-INF/ folder.
I have changed your WebConfig class to include a 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;
}
}
Upvotes: 1
Reputation: 1
If you are using spring MVC recent version , please replace configlocation with below
from context.setConfigLocation("com.soumya.spring.WebConfig");
to context.register(WebConfig.class). It should work.
Upvotes: 0
Reputation: 117
An example of initialization with comments.
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, "/*");
}
}
Upvotes: 0