2014-09-29 33 views
0

我正在使用Spring MVC創建一個小型Web應用程序。當我嘗試點擊一個似乎正確映射到我的控制器中的URL時,我得到一個404。我嘗試點擊/ projectname/test-,這是當我嘗試這樣做時在日誌中顯示的內容:Spring MVC - 在名爲'appServlet'的DispatcherServlet中找不到具有URI [/ demo/test]的HTTP請求的映射

警告:org.springframework.web.servlet.PageNotFound - 沒有在DispatcherServlet的發現HTTP請求的URI與[/演示/測試]名爲「appServlet」

我見過很多類似的問題被詢問的映射,但是沒有任何建議的修補程序適用於我。我使用的是Spring MVC,而服務器似乎無法解析通過瀏覽器進入的URL。奇怪的是「/ projectname」很好用,但「/ projectname/test」似乎根本不起作用。 JSP中都在同一個目錄中(src /主/ web應用/ WEB-INF /視圖/)具有下列名稱:

針對home.jsp,test.jsp的,userInfo.jsp

這裏是我的代碼:

的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" 
 
\t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
 
\t <modelVersion>4.0.0</modelVersion> 
 
\t <groupId>com.springapp</groupId> 
 
\t <artifactId>demo</artifactId> 
 
\t <name>demo</name> 
 
\t <packaging>war</packaging> 
 
\t <version>1.0.0-BUILD-SNAPSHOT</version> 
 
\t <properties> 
 
\t \t <java-version>1.6</java-version> 
 
\t \t <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 
 
\t \t <org.aspectj-version>1.6.10</org.aspectj-version> 
 
\t \t <org.slf4j-version>1.6.6</org.slf4j-version> 
 
\t </properties> 
 
\t <dependencies> 
 
\t \t <!-- Spring --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.springframework</groupId> 
 
\t \t \t <artifactId>spring-context</artifactId> 
 
\t \t \t <version>${org.springframework-version}</version> 
 
\t \t \t <exclusions> 
 
\t \t \t \t <!-- Exclude Commons Logging in favor of SLF4j --> 
 
\t \t \t \t <exclusion> 
 
\t \t \t \t \t <groupId>commons-logging</groupId> 
 
\t \t \t \t \t <artifactId>commons-logging</artifactId> 
 
\t \t \t \t </exclusion> 
 
\t \t \t </exclusions> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.springframework</groupId> 
 
\t \t \t <artifactId>spring-webmvc</artifactId> 
 
\t \t \t <version>${org.springframework-version}</version> 
 
\t \t </dependency> 
 
\t \t \t \t 
 
\t \t <!-- AspectJ --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.aspectj</groupId> 
 
\t \t \t <artifactId>aspectjrt</artifactId> 
 
\t \t \t <version>${org.aspectj-version}</version> 
 
\t \t </dependency> \t 
 
\t \t 
 
\t \t <!-- Logging --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.slf4j</groupId> 
 
\t \t \t <artifactId>slf4j-api</artifactId> 
 
\t \t \t <version>${org.slf4j-version}</version> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.slf4j</groupId> 
 
\t \t \t <artifactId>jcl-over-slf4j</artifactId> 
 
\t \t \t <version>${org.slf4j-version}</version> 
 
\t \t \t <scope>runtime</scope> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.slf4j</groupId> 
 
\t \t \t <artifactId>slf4j-log4j12</artifactId> 
 
\t \t \t <version>${org.slf4j-version}</version> 
 
\t \t \t <scope>runtime</scope> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>log4j</groupId> 
 
\t \t \t <artifactId>log4j</artifactId> 
 
\t \t \t <version>1.2.15</version> 
 
\t \t \t <exclusions> 
 
\t \t \t \t <exclusion> 
 
\t \t \t \t \t <groupId>javax.mail</groupId> 
 
\t \t \t \t \t <artifactId>mail</artifactId> 
 
\t \t \t \t </exclusion> 
 
\t \t \t \t <exclusion> 
 
\t \t \t \t \t <groupId>javax.jms</groupId> 
 
\t \t \t \t \t <artifactId>jms</artifactId> 
 
\t \t \t \t </exclusion> 
 
\t \t \t \t <exclusion> 
 
\t \t \t \t \t <groupId>com.sun.jdmk</groupId> 
 
\t \t \t \t \t <artifactId>jmxtools</artifactId> 
 
\t \t \t \t </exclusion> 
 
\t \t \t \t <exclusion> 
 
\t \t \t \t \t <groupId>com.sun.jmx</groupId> 
 
\t \t \t \t \t <artifactId>jmxri</artifactId> 
 
\t \t \t \t </exclusion> 
 
\t \t \t </exclusions> 
 
\t \t \t <scope>runtime</scope> 
 
\t \t </dependency> 
 

 
\t \t <!-- @Inject --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>javax.inject</groupId> 
 
\t \t \t <artifactId>javax.inject</artifactId> 
 
\t \t \t <version>1</version> 
 
\t \t </dependency> 
 
\t \t \t \t 
 
\t \t <!-- Servlet --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>javax.servlet</groupId> 
 
\t \t \t <artifactId>servlet-api</artifactId> 
 
\t \t \t <version>2.5</version> 
 
\t \t \t <scope>provided</scope> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>javax.servlet.jsp</groupId> 
 
\t \t \t <artifactId>jsp-api</artifactId> 
 
\t \t \t <version>2.1</version> 
 
\t \t \t <scope>provided</scope> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>javax.servlet</groupId> 
 
\t \t \t <artifactId>jstl</artifactId> 
 
\t \t \t <version>1.2</version> 
 
\t \t </dependency> 
 
\t 
 
\t \t <!-- Test --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>junit</groupId> 
 
\t \t \t <artifactId>junit</artifactId> 
 
\t \t \t <version>4.7</version> 
 
\t \t \t <scope>test</scope> 
 
\t \t </dependency>   
 
\t </dependencies> 
 
    <build> 
 
     <plugins> 
 
      <plugin> 
 
       <artifactId>maven-eclipse-plugin</artifactId> 
 
       <version>2.9</version> 
 
       <configuration> 
 
        <additionalProjectnatures> 
 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
 
        </additionalProjectnatures> 
 
        <additionalBuildcommands> 
 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
 
        </additionalBuildcommands> 
 
        <downloadSources>true</downloadSources> 
 
        <downloadJavadocs>true</downloadJavadocs> 
 
       </configuration> 
 
      </plugin> 
 
      <plugin> 
 
       <groupId>org.apache.maven.plugins</groupId> 
 
       <artifactId>maven-compiler-plugin</artifactId> 
 
       <version>2.5.1</version> 
 
       <configuration> 
 
        <source>1.6</source> 
 
        <target>1.6</target> 
 
        <compilerArgument>-Xlint:all</compilerArgument> 
 
        <showWarnings>true</showWarnings> 
 
        <showDeprecation>true</showDeprecation> 
 
       </configuration> 
 
      </plugin> 
 
      <plugin> 
 
       <groupId>org.codehaus.mojo</groupId> 
 
       <artifactId>exec-maven-plugin</artifactId> 
 
       <version>1.2.1</version> 
 
       <configuration> 
 
        <mainClass>org.test.int1.Main</mainClass> 
 
       </configuration> 
 
      </plugin> 
 
     </plugins> 
 
    </build> 
 
</project>

的web.xml:

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

 
\t <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
 
\t <context-param> 
 
\t \t <param-name>contextConfigLocation</param-name> 
 
\t \t <param-value>/WEB-INF/spring/root-context.xml</param-value> 
 
\t </context-param> 
 
\t 
 
\t <!-- Creates the Spring Container shared by all Servlets and Filters --> 
 
\t <listener> 
 
\t \t <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 
\t </listener> 
 

 
\t <!-- Processes application requests --> 
 
\t <servlet> 
 
\t \t <servlet-name>appServlet</servlet-name> 
 
\t \t <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 
\t \t <init-param> 
 
\t \t \t <param-name>contextConfigLocation</param-name> 
 
\t \t \t <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
 
\t \t </init-param> 
 
\t \t <load-on-startup>1</load-on-startup> 
 
\t </servlet> 
 
\t \t 
 
\t <servlet-mapping> 
 
\t \t <servlet-name>appServlet</servlet-name> 
 
\t \t <url-pattern>/</url-pattern> 
 
\t </servlet-mapping> 
 

 
</web-app>

servlet的context.xml中:

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

 
\t <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 
 
\t 
 
\t <!-- Enables the Spring MVC @Controller programming model --> 
 
\t <annotation-driven/> 
 

 
\t <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
 
\t <resources mapping="/resources/**" location="/resources/" /> 
 

 
\t <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
 
\t <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
 
\t \t <beans:property name="prefix" value="/WEB-INF/views/" /> 
 
\t \t <beans:property name="suffix" value=".jsp" /> 
 
\t </beans:bean> 
 
\t 
 
\t <context:component-scan base-package="com.springapp.demo" /> 
 
\t 
 
\t 
 
\t 
 
</beans:beans>

HomeController.java:

package com.springapp.demo; 
 

 
import java.util.Locale; 
 
import org.slf4j.Logger; 
 
import org.slf4j.LoggerFactory; 
 
import org.springframework.stereotype.Controller; 
 
import org.springframework.ui.Model; 
 
import org.springframework.web.bind.annotation.RequestMapping; 
 
import org.springframework.web.bind.annotation.RequestMethod; 
 
import org.springframework.web.bind.annotation.RequestParam; 
 

 
/** 
 
* Handles requests for the application home page. 
 
*/ 
 
@RequestMapping(value = "/") 
 
@Controller 
 
public class HomeController { 
 
\t 
 
\t private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 
 
\t 
 
\t /** 
 
\t * Simply selects the home view to render by returning its name. 
 
\t */ 
 
\t @RequestMapping(value = "/", method = RequestMethod.GET) 
 
\t public String home(Locale locale, Model model) { 
 
\t \t logger.info("Welcome home! The client locale is {}.", locale); 
 
\t \t 
 
\t \t return "home"; 
 
\t } 
 
\t 
 
\t @RequestMapping(value = "/user", method = RequestMethod.POST) 
 
\t public String userInfo(Model model, @RequestParam(value="userName")String userName, @RequestParam(value="gender")String gender) { 
 
\t \t \t \t 
 
\t \t model.addAttribute("userName", userName); 
 
\t \t model.addAttribute("gender", gender); 
 
\t \t 
 
\t \t return "userInfo"; 
 
\t } 
 
\t @RequestMapping(value = "/test", method = RequestMethod.GET) 
 
\t public String test(Model model) { 
 
\t \t 
 
\t \t return "test"; 
 
\t } 
 
\t 
 
}

任何想法?我需要提供更多信息嗎?謝謝!

+0

我的推薦。遠離XML-s。如果你想要一個小型的Web應用程序,請考慮Spring Boot。 – Vaelyr 2014-09-29 18:39:56

回答

0

我認爲你所看到的行爲是預期的行爲之一。您的HomeController類不具有/demo URI的映射,只有/user,/test/

UPD:我已經看到了問題的更新後,我的結論是,你是指demo作爲上下文描述符,可能要求它像

http://localhost:8080/demo/test

如果我的假設是正確的,最可能的原因問題是您在瀏覽器中使用的上下文描述符與實際部署應用程序的上下文描述符不匹配。你可能想嘗試訪問它像

http://localhost:8080/test

http://localhost:8080/appServlet/test

如果這樣的作品(即return "test";調用)比這是問題。如果您需要其他應用程序名稱,然後appServlet,則應採取一些取決於您使用的應用程序服務器的操作,例如,您可能希望在生成後重命名.war文件,或者如果您在此處部署,則更改IDE設置等等。

+0

謝謝你的迴應!你的假設是正確的 - 我使用/ demo/test來嘗試打開該頁面。我現在已經嘗試/測試和/ appServlet /測試以及你的建議,並仍然有同樣的問題。 – 2014-09-29 18:32:46

+0

@RemziIbrahimZayid好的,你能描述一下你部署應用程序的方式嗎?該名稱也可以是'demo-1.0.0-BUILD-SNAPSHOT',它基於你的'pom.xml'。 – 2014-09-29 18:34:51

+0

事情是,只是/演示/工作正常,所以我會很驚訝,如果它與該名稱的問題,但我一定會給它一個鏡頭。我使用STS(Spring Tool Suite)來部署所有這些代碼。默認情況下,它使用:VMware vFabric tc Server Developer Edition v2.9 作爲其服務器。它與tomcat集成我相信。我在服務器配置上更改的唯一設置是端口,它們現在是: Tomcat管理端口:8140 HTTP/1.1:8141 我正在使用localhost:8141嘗試命中這些URL。 – 2014-09-29 18:39:58

0

您正在嘗試訪問/ demo,但正如我在Controller中看到的,沒有這樣的URI模板。你有根「/」,「/ user」和「/ test」。

+0

非常抱歉!我編輯了原文。 「demo」是項目名稱。我試圖去/演示/測試 – 2014-09-29 18:28:05

+0

你的控制器和jsp文件中的一切看起來都很好。試試這個:停止服務器。 右鍵單擊您的項目 - > Maven安裝後,Maven安裝。啓動服務器並重試:) – user2739823 2014-09-29 18:59:16

1
You need to add the below mapping: 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <!-- <resources mapping="/resources/**" location="/resources/" /> --> 

<!-- <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/> --> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <!-- View Handler --> 
<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
<beans:property name="favorPathExtension" value="true"/> 
<beans:property name="mediaTypes"> 
<beans:map> 
<beans:entry key="xml" value="text/xml"/> 
<beans:entry key="json" value="application/json"/> 
<beans:entry key="html" value="text/html"/> 
<beans:entry key="less" value="text/html"/> 
</beans:map> 
</beans:property> 
<beans:property name="viewResolvers"> 
<beans:list> 
<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/> 
</beans:bean> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<beans:property name="prefix" value="/"/> 
</beans:bean> 
</beans:list> 
</beans:property> 
</beans:bean> 
相關問題