zhu86804396
2015-06-17 09:38
采纳率: 25%
浏览 4.6k
已采纳

maven配置spring-mvc请求到不了controller

最近自己想用maven搭建spring-mvc项目,按照结构搭建的最后请求到不了

 controller
2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Bound request context to thread: org.apache.catalina.connector.RequestFacade@1e70f68
2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'spring' processing GET request for [/MavenDemo/index.do]
2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@7d554f59] in DispatcherServlet with name 'spring'
2015-06-17 17:29:30 [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping]-[DEBUG] No handler mapping found for [/index.do]
2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@2c0ea67] in DispatcherServlet with name 'spring'
2015-06-17 17:29:30 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]-[DEBUG] No handler mapping found for [/index.do]

下面是配置:

web.xml

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/spring.xml;
        </param-value>
    </context-param>
    <!-- 设计路径变量值 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>springmvc.root</param-value>
    </context-param>


    <!-- Spring字符集过滤器 -->
    <filter>
        <filter-name>SpringEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SpringEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 日志记录 -->
    <context-param>
        <!-- 日志配置文件路径 -->
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:conf/log4j.properties</param-value>
    </context-param>
    <context-param>
        <!-- 日志页面的刷新间隔 -->
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

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

    <!-- springMVC核心配置 -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:conf/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

spring-mvc.xml

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


   <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

   <!-- 对模型视图添加前后缀 -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

UserController.java

 package com.tarena.controller;

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

@Controller
@RequestMapping("/")

public class UserController {
    @RequestMapping("index")
    public String index(){
        return "index";
    }
}

tomcat启动起来之后访问http://localhost:8080/MavenDemo可以正常跳转到欢迎页面,但是http://localhost:8080/MavenDemo/index.do就不能跳转到对应页面了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 茂盛的小绿芽 2015-06-19 06:09
    已采纳

    @Controller
    @RequestMapping("/index")
    public class UserController {
    @RequestMapping("index")
    public String index(){
    return "index";
    }
    }

    这样写访问url是:http://localhost:8080/MavenDemo/index.do?index
    你这样将action命名访问路径为“/”,那你怎么去区分不同的action 所有建议不要那样写。

    点赞 打赏 评论
  • frank_20080215 2015-06-17 14:36

    没有配置index.do的View与Action的映射关系

    点赞 打赏 评论
  • Evankaka 2015-06-17 14:39

    你拦截的是@RequestMapping("index"),注意看清楚了http://localhost:8080/MavenDemo/index估计才可以

    点赞 打赏 评论
  • ToQuery 2015-06-30 16:15

    mappimg注解改成inde.do

    点赞 打赏 评论

相关推荐 更多相似问题