Java 批注spring mvc-未找到映射
我是SpringMVC的新手,希望为我的应用程序进行注释配置。没有web.xml或类似的东西。我已经编写了两个我想访问的jsp页面,两个控制器类和一个应用程序类,其main方法如下所示:Java 批注spring mvc-未找到映射,java,spring,jsp,spring-mvc,spring-annotations,Java,Spring,Jsp,Spring Mvc,Spring Annotations,我是SpringMVC的新手,希望为我的应用程序进行注释配置。没有web.xml或类似的东西。我已经编写了两个我想访问的jsp页面,两个控制器类和一个应用程序类,其main方法如下所示: import javax.servlet.ServletContext; import javax.servlet.ServletRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springfra
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableWebMvc
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer{
static final Logger logger = LoggerFactory.getLogger(Application.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
控制器示例:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
static final Logger logger = LoggerFactory.getLogger(IndexController.class);
@RequestMapping(value = {"/index","/"})
@ResponseBody
public ModelAndView index()
{
return new ModelAndView("index");
}
}
最后是我编辑的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.6.RELEASE</version>
</parent>
<groupId></groupId>
<artifactId></artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name></name>
<description></description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>Application</start-class>
<java.version>1.8</java.version>
<main.basedir>${basedir}/../..</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
<!-- dependency versions -->
<spring.version>3.1.4.RELEASE</spring.version>
<apache.cxf.version>2.6.1</apache.cxf.version>
<spring.boot.version>1.1.9.RELEASE</spring.boot.version>
<spring.version>4.1.2.RELEASE</spring.version>
<javax.servlet.version>2.5</javax.servlet.version>
</properties>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
谁能在这个问题上提供帮助?我怀疑我应该用DispatcherServlet注入一个bean,但我真的不知道该如何使用它以及如何正确设置它 在webMvcConfig.java上添加以下配置
@ComponentScan({ "com.your.controller"})
它可能是两个不同包中的Application.java和IndexController.java。那么你必须这样提
@ComponentScan(base=“your packge”)
完整示例:-您的
@Controller
和@Configuration
类是否在同一个包中?首先,您的pom有缺陷,您的spring版本有问题。只需使用提供的弹簧靴即可。删除spring.version
并删除spring核心依赖项。同时删除@EnableWebMvc
注释和实现不需要的WebApplicationInitializer
。确保您的应用程序
类与控制器位于同一个包中,或者位于控制器上方的包中。我倾向于将其放在顶级包中,以便@ComponentScan
涵盖所有内容。谢谢,将所有内容移动到一个包的提示修复了我的错误。
@ComponentScan({ "com.your.controller"})