在学习Spring时,我试图运行Spring in Action的代码示例,但它向我展示了tomcat日志中提到的以下错误:
11-Aug-2016 12:15:57.561 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/RestTester-0.0.1-SNAPSHOT/] in DispatcherServlet with name 'dispatcher'
我正在尝试仅借助基于java的配置来配置spring。相同的代码如下所述:
TestProjectInitializer.java
package test.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class TestProjectInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[]{"/"};
}
}
WebConfig.java
package test.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("test.webconf")
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
{
configurer.enable();
}
}
根配置.java
package test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={"test"}, excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {
}
HomeController.java
package test.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
/*@RequestMapping("/")*/
public class HomeController
{
@RequestMapping(value="/",method=RequestMethod.GET)
public String home()
{
return "home";
}
}
pom.xml
<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>
<groupId>test</groupId>
<artifactId>RestTester</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
目录结构
请帮助我了解我做错了什么。
Webmvc上的基础包名称错误,应为test.controller。
我写了一个spring boot项目。它有三个文件。 appconfig.java HelloController.java 当我尝试运行它时,它出现了错误“没有为名为'DispatcherServlet中URI[/springc1_01/]的HTTP请求找到映射”。这是因为服务器没有找到控制器还是其他原因?THX.
这是我的网站。xml 我的servlet上下文。xml 最后是Handler类。这是在com下。比如Spring。控制器。恳求 但是,我们将转到 它返回404错误。 这是我的项目结构
我想配置文件中缺少一些东西,但我看不到。 当我放入浏览器-->localhost:8080/procura/notifications-->我在控制台中得到的是-->org.springframework.web.servlet.DispatcherServlet noHandlerFound警告:在名为“DispatcherServlet”的DispatcherServlet中没有找到URI[/
我正在尝试学习Spring Security性,第一个代码示例是在运行URL“http://localhost:8080/spring-security-helloworld-xml/welcome”并使用jetty插件作为服务器时出现这样的错误。 错误:org.springframework.web.servlet.pageNotFound noHandlerFound警告:在名为“mvc-di
很抱歉再次问这种问题,但我无法通过查看其他线程和Spring doc来解决我的问题。 我正在使用maven的3.1.0.RELEASE,并尝试使用注释和java配置。 以下是我的web.xml: 这是我的档案web-application-config.xml. 我有两个类。第一个配置视图解析器 第二个定义我的控制器: 根据我的配置,我想一切都应该指向我的home()函数。然而,事实并非如此,以下
Dispatcher servlet(servlet-context.xml) http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xs