spring mvc中的控制器类映射

时间:2017-10-15 20:43:23

标签: xml spring maven spring-mvc servlets

org.springframework.web.servlet.PageNotFound noHandlerFound

我无法在以下代码中找到错误,因为我只是在学习spring mvc。 无法在控制器类中映射请求并尝试检查配置文件但无法找到错误。请帮助我完成该计划。

的web.xml

 <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

 <web-app>
 <display-name>Archetype Created Web Application</display-name>


 <servlet>
  <servlet-name>telusko</servlet-name>
   <servlet-class>   
     org.springframework.web.servlet.DispatcherServlet 
  </servlet-class>
</servlet>

 <servlet-mapping>
   <servlet-name>telusko</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>

telusko-servlet.xml中

   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:ctx="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
  <ctx:annotation-config></ctx:annotation-config>
  <ctx:component-scan base-package="com.telusko"></ctx:component-scan>
  </beans> 

的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>teludkospringmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>teludkospringmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
 <dependencies>

  <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
     <scope>test</scope>
  </dependency>

  <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.1.8.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.8.RELEASE</version>
 </dependency>

 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.36</version>
 </dependency>

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

 </dependencies>
 <build>
 <finalName>teludkospringmvc</finalName>
 </build>
 </project>

控制器类

package com.telusko;
import org.springframework.stereotype.Controller;
import  org.springframework.web.bind.annotation.RequestMapping; 
@Controller
public class AddController {

@RequestMapping("/add")
public void add(){
System.out.print("hi");
}

}

ErrorResult

WARNING: No mapping found for HTTP request with URI [/teludkospringmvc/add] 
in DispatcherServlet with name 'telusko'

2 个答案:

答案 0 :(得分:0)

我认为您在telusko-servlet.xml文件中缺少此行以启用spring MVC注释:

<mvc:annotation-driven/>

希望它有效。

答案 1 :(得分:0)

更新:添加<mvc:annotation-driven/>后请关注mlg的建议。

您仍然会错过内部资源查看解析程序,他们无法找到您display.jsp

telusko-servlet.xml中

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

AddController.java

@Controller
public class AddController {

    @RequestMapping("/add")
    public String add(){
        return "display";
    }

}