Reputation: 678
I have a Spring MVC application which I I deploy on a tomcat server. It worked fine, until I was told to use an annotation configuration, rather than a web.xml file.
I've added a logger to my intialiser, and it doesn't even seem to be starting, and the pages are now just giving me 404 errors.
This is my initialiser class
package com.demo.web.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class ImpInitialiser implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
}
And here is my Config class.
package com.demo.web.config;
import org.springframework.context.annotation.Bean;
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
@ComponentScan(basePackages = "com.demo.web")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
[Edit] I've simplified the code from the original post, just to try and get any example working.
Is there anything obvious I'm missing here?
Upvotes: 1
Views: 1850
Reputation: 16271
One:
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(DispatcherConfig.class);
I think is wrong, there you must register the server beans, not the web beans
The following is correct
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
Because DispatcherConfig
represent your Web infrastructure configuration.
You are registering DispatcherConfig.class
twice and I think is not correct.
Has no sense, first time I see that approach.
Two:
@Bean
@Resource(name = "jdbc/testDB")
public DataSource dataSourceLookup() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/testDB");
return dataSource;
}
Declare a DataSource
in the web side is a bad practice.
Three:
Post your @Controller
class.
Four:
I think is not neccessary
registry.addViewController("/grouporroles").setViewName("grouporroles");
because you are scanning the @Controller
s through @ComponentScan("com.demo.web.controller")
Five
Change:
@Bean
@Resource(name = "jdbc/testDB")
public DataSource dataSourceLookup() {
to
@Bean(name = "jdbc/testDB")
public DataSource dataSourceLookup() {
Upvotes: 1