2016-09-18 34 views
0

这里春天控制器请求映射是我的代码:不工作

的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>Registration Web Service</display-name> 

    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
    </servlet> 

    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/reg/*</url-pattern> 
    </servlet-mapping> 


</web-app> 

调度-servlet.xml中:

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

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

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

</beans> 

的HomeController:

package org.magic.controller; 

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

@Controller 
public class HomeController { 

    @RequestMapping(value="/welcome", method=RequestMethod.GET) 
    public ModelAndView helloWorld() { 

     String message = "<br><div style='text-align:center;'>" 
       + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CrunchifyHelloWorld.java **********</div><br><br>"; 
     return new ModelAndView("index", "message", message); 
    } 
} 

请求地址:http://localhost:8080/registration/reg/welcome

这里是我的POM文件:要提到

<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>org.magic</groupId> 
    <artifactId>registration</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>registration 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.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 

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

    </dependencies> 
    <build> 
    <finalName>registration</finalName> 
    </build> 
</project> 
+0

请显示您的堆栈跟踪 –

+0

我是eclipse的初学者。即使应用程序处于调试模式,也无法获取任何堆栈跟踪。 –

+0

任何错误信息? –

回答

0

两件事情:您正在使用org.springframework.web.servlet.view.UrlBasedViewResolver这需要一个叫做viewClass财产

第一。所以你要么定义它,要么使用不同的视图解析器实现,比如org.springframework.web.servlet.view.InternalResourceViewResolver。更多的信息在这里给出:http://forum.spring.io/forum/spring-projects/web/74825-urlbasedviewresolver

Btw。我可以看到它在堆栈跟踪这样的:

Caused by: java.lang.IllegalArgumentException: Property 'viewClass' is required 
at org.springframework.web.servlet.view.UrlBasedViewResolver.initApplicationContext(UrlBasedViewResolver.java:430) 
at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120) 
at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:76) 
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74) 
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121) 
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ... 64 more 

如果你继续使用UrlBasedViewResolver您的调度员servlet.xml中所以基本上应该是这样的:

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

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

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

</beans> 

而且我看到你”重新期待应用上下文/registration。请仔细检查您是否配置了它。我可以通过简单地拨打http://localhost:8080/reg/welcome

来运行您的应用程序,而无需应用程序环境。请让我知道,如果你把它启动并运行!