为什么@JavaConfig无法在Spring MVC中运行? [英] Why @JavaConfig not working in Spring MVC?

查看:163
本文介绍了为什么@JavaConfig无法在Spring MVC中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置没有调度程序xml和web xml的Spring MVC项目(即根本没有任何xml)。因此,我正在使用Spring的@JavaConfig技术。但是,每当我尝试在我的服务器上启动应用程序时,我的服务器都无法正常工作(没有任何异常,它会获得HTTP 404)。

这是项目结构的快照...


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...

此处代码片段:

WebConfig.java

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";
    }

}

这里是pox.xml摘录:

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>


推荐答案

您的项目结构看起来不错但我怀疑Eclipse组态。看起来您的包不是Java Source的一部分。

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

请执行以下步骤:

1。验证为项目设置的来源。转到项目属性, Java构建路径并在选项卡上,查找条目 src \ main\ava src\main\resources 。如果不存在,请单击添加文件夹以添加源文件夹,它应类似于以下内容:

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:

2. 转到部署程序集和以正确的方式验证您是否具有部署条目映射。如果找不到部署程序集,请转到 Project Facets 并转到将项目转换为分面形式的选项。在 Project Facets 中,您应该选择动态网络模块

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.

部署程序集应具有从 Maven依赖项 WEB-INF / lib <的映射/ code>, / src / main / webapp to / src / main / resources WEB-INF / classes src / main / java WEB-INF /类。如果缺少任何这些条目,您可以单击添加并选择文件夹根据需要。

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.

3. 确保使用 Web项目设置 Context Root 名称访问项目c>您的项目,如果您通过Eclipse而不是通过maven或其他工具部署应用程序。

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.

在上述情况下,您可以通过 localhost:8080 / UI 访问应用程序,其中服务器端口 8080 可能因配置而异。

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

这篇关于为什么@JavaConfig无法在Spring MVC中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆