2012-03-13 115 views
1

我试图设置一个控制器,很遗憾无法查看输出...所有内容都正确呈现。当我去http://localhost:8080/CMT/content/edit时,我收到一个404页面。从NetBeans中运行我的应用程序去http://localhost:8080/CMT@RequestMapping没有正确映射

package com.cmt.controllers; 

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.servlet.ModelAndView; 

@Controller 
public class Content { 

    @RequestMapping(value="/content/edit", method=RequestMethod.GET) 
    public ModelAndView edit(Model model) { 
     ModelAndView mv = new ModelAndView(); 
     mv.setViewName("index"); 
     return mv; 
    } 
} 

APP-config.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:context="http://www.springframework.org/schema/context" 
    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.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <!-- Scans the classpath of this application for @Components to deploy as beans --> 
    <context:component-scan base-package="com.cmt" /> 
    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven /> 
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/app-config.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

如何调试这一点,看看是工作和什么不是?

更新

GlassFish服务器日志中的Netbeans显示

INFO: Pre-instantiating singletons in org.s[email protected]1531ed: defining beans [content,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,viewResolver]; parent: org.s[email protected]b91162 
INFO: Mapped URL path [/content/edit] onto handler [[email protected]] 
INFO: Mapped URL path [/content/edit.*] onto handler [[email protected]] 
INFO: Mapped URL path [/content/edit/] onto handler [[email protected]] 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: FrameworkServlet 'Spring-MVC-Dispatcher-Servlet': initialization completed in 1234 ms 
INFO: WEB0671: Loading application [CMT] at [/CMT] 
INFO: CMT was successfully deployed in 3,725 milliseconds. 
+0

您的servlet在web.xml中配置是否正确? – alephx 2012-03-13 19:28:44

+0

常见的日志告诉你什么?你的/ WEB-INF/jsp /文件夹中是否有一个名为index.jsp的文件?你的web.xml是什么样的? – smp7d 2012-03-13 19:30:01

+0

@ smp7d - 我添加了web.xml并且是的,我的jsp文件夹中有一个index.jsp文件。 – Webnet 2012-03-13 19:56:13

回答

2

是您DispatcherServlet命名appweb.xml映射到/*?从你的previous的问题,我看到:

<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/app-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 

/content/edit网址不符合*.htm模式。尝试/content/edit.htm可能的解决方法。

+0

很棒!我试过用'/ *'代替'* .htm',但没有运气。我不希望在URL上使用.htm,只是完全避免使用文件扩展名。 – Webnet 2012-03-13 19:54:54