2015-08-20 28 views
2

全部錯誤消息沒有映射:找到HTTP請求與URI [爪哇註釋配置]

[HTTP-NIO-8080-EXEC-3] WARN osweb.servlet.PageNotFound - 無映射找到用於與URI [/ABCD/greeting.html]在DispatcherServlet的名爲 'DispatcherServlet5'

WebAppInitializer.java

package com.naz; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.WebApplicationContext; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

public class WebAppInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     WebApplicationContext context = getContext(); 
     servletContext.addListener(new ContextLoaderListener(context)); 
     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet5", new DispatcherServlet(context)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("*.html"); 
    } 

    private AnnotationConfigWebApplicationContext getContext() { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     context.setConfigLocation("com.naz.WebConfig"); 
     return context; 
    } 

} 
HTTP請求

WebConfig.java

package com.naz; 

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.view.InternalResourceViewResolver; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.naz") 
public class WebConfig { 

    @Bean 
    public InternalResourceViewResolver jspViewResolver() { 
     InternalResourceViewResolver bean = new InternalResourceViewResolver(); 
     bean.setPrefix("/WEB-INF/jsp/"); 
     bean.setSuffix(".jsp"); 
     return bean; 
    } 
} 

HelloController.java

package com.naz.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public class HelloController { 

    @RequestMapping("/greeting") 
    public String sayhello(Model model){ 
     model.addAttribute("greeting_message", "Welcome Here"); 
     return "hello"; 
    } 


} 

的hello.jsp 位置:WEB-INF/JSP/hello.jsp中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Greeting Page</title> 
</head> 
<body> 
    <h1>${greeting_message}</h1> 

</body> 
</html> 

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

<groupId>com.naz</groupId> 
<artifactId>ABCD</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>war</packaging> 

<name>Hello2</name> 
<description>Demo project for Spring Boot</description> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.2.5.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</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-webmvc-portlet</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-integration</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-undertow</artifactId> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

回答

2

更新:我分析了新的pom.xml,我可以看到你的請求處理程序是沒有得到出於某些原因,「setConfigLocation」註冊,因爲不能找到WebConfig。

這使它適合我。在getContext函數中添加此行WebAppInitalizer.java類。

​​

引用here

我能複製相同的配置,使應用程序的工作。

我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>ABCD</groupId> 
    <artifactId>ABCD</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.1.3</version> 
     </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> 
+0

謝謝回答這個問題。 :) 你的pom.xml不適合我。我包含了我的pom.xml代碼。如果你找到任何解決方案,它會很好。 盡我所能,我的RequestMapping或ComponentScan不能正常工作。但我不知道代碼中哪一個和哪個錯誤。 @Neeraj – NAZ

+1

我分析了你的新pom.xml,我可以看到你的請求處理程序沒有被註冊,因爲「setConfigLocation」無法找到WebConfig。 這使它適合我。在WebAppInitalizer.java類的getContext函數中添加此行。 context.register(com.naz.WebConfig.class); 參考這裏: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html – Neeraj

相關問題