程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> springmvc-maven配置spring-mvc請求到不了controller

springmvc-maven配置spring-mvc請求到不了controller

編輯:編程綜合問答
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就不能跳轉到對應頁面了。

最佳回答:


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

這樣寫訪問url是:http://localhost:8080/MavenDemo/index.do?index
你這樣將action命名訪問路徑為“/”,那你怎麼去區分不同的action 所有建議不要那樣寫。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved