Why @JavaConfig not working in Spring MVC?

2019-01-19 09:06发布

I'm trying to setup Spring MVC project without dispatcher xml and web xml(i.e. without any xml at all). Hence, I'm using @JavaConfig technique of Spring. However, whenever I'm trying to start the application on my server my server it's not working(without throwing any exception it's getting HTTP 404).
Here is the snapshot of the project structure...

enter image description here

And here the code snippets:
WebConfig.java

@EnableWebMvc
@Configuration
@ComponentScan({ "com.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final String VIEW_LOCATION = "/WEB-INF/views/";
    private static final String SUFFIX = ".jsp";

//  @Override
//  public void addResourceHandlers(ResourceHandlerRegistry registry) {
//      System.out.println("in resource");
//      registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");       
//  }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        System.out.println("in view");
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix(VIEW_LOCATION);
        viewResolver.setSuffix(SUFFIX);
        return viewResolver;
    }

}

Initializer.java

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    private static final String MAPPING_URL = "/";

    @Override
    protected Class<?>[] getRootConfigClasses() {
        System.out.println("in config");
        return new Class[] { WebConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        System.out.println("dispatcher");
        return new String[] { MAPPING_URL };
    }

}

HelloController.java

@Controller
public class HelloController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printWelcome() {
        System.out.println("in servlet");
        return "index";
    }

}

And here is the pox.xml snippet:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>
  <artifactId>UI</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>UI Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
    <finalName>UI</finalName>
  </build>
</project>

2条回答
甜甜的少女心
2楼-- · 2019-01-19 09:58

Your code is all right. I was execute them without any changes on Tomcat 8 application server. Use Maven to package war, mvn clean package.

查看更多
SAY GOODBYE
3楼-- · 2019-01-19 10:03

Your project structure looks good but I doubt about the Eclipse configuration. Looks like your package is not being a part of Java Source.

Please execute the following steps:

1. Verify Source set for project. Go to project properties, Java Build Path and on Source tab, look for the entries src\main\java and src\main\resources. If not present, click on Add Folder to add source folders and it should similar to the below:

Source Folders

2. Go to Deployment Assembly and verify that the you have deployment entry mappings in the right way. If you do not find Deployment Assembly, go to Project Facets and go for the option to convert the project to faceted form. In Project Facets, you should have Dynamic Web Module selected.

Deployment Assembly should have mappings from Maven Dependencies to WEB-INF/lib, /src/main/webapp to /, src/main/resources to WEB-INF/classes and src/main/java to WEB-INF/classes. If any of these entries are missing, you may click Add and choose Folder or Libraries as required.

Deployment Descriptor

3. Make sure that you access the project using the Context Root name mentioned in the Web Project Settings of your project, if you are deploying the application through Eclipse and not through maven or some other tool.

Web Project Settings

In the above case, you can access the application by localhost:8080/UI where the server port 8080 may vary according to configuration.

查看更多
登录 后发表回答