最近自己想用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 所有建議不要那樣寫。