Reputation: 95
I usually used xml configs but now want to move on just java config but something is wrong with my code, I dont know what to change because everything seems fine.
Im getting this error after running my spring app on glass fish:
[glassfish 5.0] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=30 _ThreadName=http-listener-1(2)] [timeMillis: 1619543716623] [levelValue: 900] [[ No mapping for GET /spring_project-1.0-SNAPSHOT/home/showHome/]]
This is my controller:
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/showHome")
public String showHome() {
return "home";
}
}
My view resolver:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.config")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
And WebAppInitializer:
public class AppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Upvotes: 0
Views: 6753
Reputation: 11
You need to specify the HTTP method in this line.
@RequestMapping("/showHome")
or just use
@GetMapping
Also, read the page below it can be useful.
https://www.baeldung.com/spring-new-requestmapping-shortcuts
Upvotes: 1