SpringMVC中JSP页面显示为源码

时间:2021-07-24 17:38:05
@RequestMapping(value = "login")
public ModelAndView login(ModelAndView mav) throws Exception {
mav.setViewName("login.jsp");
return mav;
}

<url-pattern>/</url-pattern>不会匹配到*.jsp,不会进入spring的DispatcherServlet类。
<url-pattern>/*</url-pattern>会匹配*.jsp,导致进入spring的DispatcherServlet 类,然后去寻找controller,接着找不到对应的controller所以报错。配置文件中加入<mvc:default-servlet-handler />,login.jsp文件会被当做静态文件显示为文本。

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.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>