2015-10-11 108 views
0

DispatcherServlet有問題,我認爲問題出現在xml中的一個,但在Spring中是新的,我無法識別它。PageNotFound:在DispatcherServlet中找不到具有URI的HTTP請求的映射

HotelController.java(SRC \主\的java \大全\控制器)

@Transactional 
@Controller 
public class HotelController { 

    @Autowired 
    HotelService hotels; 

    @RequestMapping(value="/hotels", method=RequestMethod.GET) 
    public String index(Model model) { 
     System.out.println("test"); 
     model.addAttribute("hotels", hotels.findAllHotels()); 
     return "allhotels"; 
    } 
} 

的web.xml(SRC \主\ web應用\ WEB-INF)

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>/WEB-INF/spring/appServlet/mvc-dispatcher-servlet.xml</param-value> 
      </init-param> 
      <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

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> 
<name>hotelbookings</name> 
<groupId>CIAI_T1</groupId> 
<artifactId>hotelbookings</artifactId> 
<version>0.1.0</version> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.0.M5</version> 
</parent> 

<properties> 
    <hibernate.version>4.3.6.Final</hibernate.version> 
    <mysql.connector.version>5.1.31</mysql.connector.version> 
    <joda-time.version>2.3</joda-time.version> 
    <testng.version>6.9.4</testng.version> 
    <mockito.version>1.10.19</mockito.version> 
    <h2.version>1.4.187</h2.version> 
    <dbunit.version>2.2</dbunit.version> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
<!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
    </dependency> 
      <dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
    </dependency> 

    <!-- Hibernate --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 

    </dependency> 

    <!-- jsr303 validation --> 
    <dependency> 
     <groupId>javax.validation</groupId> 
     <artifactId>validation-api</artifactId> 
     <version>1.1.0.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
    </dependency> 

    <!-- MySQL --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    </dependency> 

    <!-- Joda-Time -->  
    <dependency> 
     <groupId>joda-time</groupId> 
     <artifactId>joda-time</artifactId> 
    </dependency> 

    <!-- To map JodaTime with database type -->  
    <dependency> 
     <groupId>org.jadira.usertype</groupId> 
     <artifactId>usertype.core</artifactId> 
     <version>3.0.0.CR1</version> 
    </dependency> 

    <!-- Servlet+JSP+JSTL --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId>   
    </dependency> 
    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>javax.servlet.jsp-api</artifactId> 
     <version>2.3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 


    <!-- Testing dependencies --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>${testng.version}</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.mockito</groupId> 
     <artifactId>mockito-all</artifactId> 
     <version>${mockito.version}</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>dbunit</groupId> 
     <artifactId>dbunit</artifactId> 
     <version>${dbunit.version}</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-devtools</artifactId> 
    </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.4.RELEASE</version> 
       </dependency> 
      </dependencies> 
     </plugin> 
     <plugin> <!-- enable java 1.7 --> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId>   
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<repositories> 
    <repository> 
     <id>spring-releases</id> 
     <name>Spring Releases</name> 
     <url>https://repo.spring.io/libs-release</url> 
    </repository> 
    <repository> 
     <id>org.jboss.repository.releases</id> 
     <name>JBoss Maven Release Repository</name> 
     <url>https://repository.jboss.org/nexus/content/repositories/releases</url> 
    </repository> 
    <repository> 
     <id>spring-milestone</id> 
     <url>https://repo.spring.io/libs-release</url> 
    </repository> 
</repositories> 

<pluginRepositories> 
    <pluginRepository> 
     <id>spring-releases</id> 
     <url>https://repo.spring.io/libs-release</url> 
    </pluginRepository> 
    <pluginRepository> 
     <id>spring-milestone</id> 
     <url>https://repo.spring.io/libs-release</url> 
    </pluginRepository> 
</pluginRepositories> 

MVC-調度-servlet.xml中(SRC \主\ web應用\ WEB-INF)

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<context:component-scan base-package="hotelbooking.controller"/> 
<mvc:annotation-driven /> 

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp"/> 
</bean> 

</beans> 

我曾經在的觀點:「\ SRC \主\ web應用\ WEB-INF \ views「,我有一個名爲」allhotels.jsp「的文件,我試圖用」http://localhost:8080/hotels「打開頁面(儘管我不確定這是否正確)。

上面的文件名前面是它們的相對路徑。

我使用Maven的Spring引導。如果您需要我發佈其他內容,請告訴我。

+0

如果https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp是對的,也許把它放在'\ jsp'而不是'\ views'中? – zapl

+0

@zapl對不起,我忘了包含mvc-dispatcher-servlet.xml。我嘗試將文件夾名稱和xmp更改爲jsp,但它不起作用。 – Aterin

回答

1

我可以看到你已經使用了兩個不同的名字,而DispatcherServlet的web.xml中配置它

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/mvc-dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
</servlet-mapping> 

不要在你的MVC-調度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
    http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<context:component-scan base-package="hotelbooking.controller"/> 
<mvc:annotation-driven /> 

</beans> 

我下面的變化覺得它更多關於web.xml和spring配置文件問題,請嘗試使用上面修改過的並嘗試一次。

URL模式應該像下面

http://hostname:port/<webapplication-name>/<controller-path> http://localhost:8080/hotelbookings/hotels(在你的情況下,嘗試這種假設 'hotelbookings' 爲Web應用程序的名稱。)

+0

感謝您的回答,但仍然一樣。爲了確保,Web應用程序名稱是pom.xml上的artifactid /名稱? – Aterin

+0

啊對,那的確是酒店預訂。 – Aterin

+0

的確,我確實是這樣,錯誤是:'沒有爲名爲'dispatcherServlet'的DispatcherServlet中帶有URI [/ hotelbookings/hotels]的HTTP請求找到映射。「 – Aterin

1

你可能會丟失這部分在mvc-dispatcher-servlet.xml文件

<context:component-scan base-package="hotelbooking.controller" />

我想我看到了這個問題。你已經使用了值而不是路徑。嘗試使用這樣的:

@RequestMapping(path="/hotels", method=RequestMethod.GET) 
+0

感謝您的回答,但我添加了該行,但沒有幫助,我認爲如果這是由於依賴性問題導致應用程序無法啓動的問題。我確實決定離開''以供將來打樣。 – Aterin

+0

只是注意到你的編輯。我發現所有的例子都使用'value',不管我用'path'嘗試過,但它仍然不起作用。 – Aterin

相關問題