博主一开始在web.xml配置的DispatcherServlet如下:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/springmvcApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
视图解析器:组件扫描、mvc驱动、ViewResolver配置如下:
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.jie.spring.mvc"></context:component-scan>
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
Controller如下:
@Controller
@RequestMapping("/")
public class SimpleMvcAction {
@RequestMapping("simpleMvc.do")
public ModelAndView topage(){
ModelAndView view = new ModelAndView();
view.setViewName("showMsg");
return view;
}
}
showMsg.jsp页面也存在,但是请求Controller就是访问不到页面。刚开始以为是jar包版本的问题,换成高版本的jar包后问题还是没有解决,后来将DispatcherServlet的<url-pattern>配置成了<url-pattern>*.do</url-pattern>请求就成功了。
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/springmvcApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
很奇怪,后来把<url-pattern>配置成<url-pattern>/</url-pattern>也可以访问。更奇怪了,于是百度了一下,原来是:
<url-pattern>/</url-pattern>
不会匹配.jsp的请求,而
<url-pattern>/*</url-pattern>
则会匹配.jsp,然后去找该请求的Controller,找不到就会报错404。