问题:这是文件目录图,我最开始的目的很简单,就是从webapp下的index.jsp通过
解决:通过网上的大量参考资源,大概有以下几种查错方法:
<jsp:forward page="${APP_PATH }/index.do">jsp:forward>
*这个${APP_PATH }是什么呢?
-
public
class StartSystemListener implements ServletContextListener {
-
-
public void contextInitialized(ServletContextEvent sce) {
-
ServletContext servletContext = sce.getServletContext();
-
String contextPath = servletContext.getContextPath();
-
servletContext.setAttribute(
"APP_PATH", contextPath);
-
System.out.println(contextPath);
-
System.out.println(
"0.0.");
-
}
-
-
public void contextDestroyed(ServletContextEvent sce) {
-
-
}
-
-
}
如图,其实就是项目的根路径名:所以应该输出为:/Atcrowdfunding-main
所以到目前为止没有错,这是完全按照那位老师的代码敲得。
打开我的springmvc-context.xml,找到视图解析那里
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
property>
-
<property name="prefix" value="/WEB-INF/jsp/">
property>
-
<property name="suffix" value=".jsp">
property>
-
bean>
跟网上正确的代码相对照,也没有什么毛病。???
-
@Controller
-
@RequestMapping(
"/")
-
public
class DispatcherController {
-
-
@RequestMapping(
"/index")
-
public String index() {
-
return
"index";
-
}
-
-
}
????
-
<servlet>
-
<servlet-name>springmvc
servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation
param-name>
-
<param-value>classpath:spring/springmvc-context.xml
param-value>
-
init-param>
-
<load-on-startup>1
load-on-startup>
-
servlet>
-
<servlet-mapping>
-
<servlet-name>springmvc
servlet-name>
-
<url-pattern>*.htm
url-pattern>
-
<url-pattern>*.do
url-pattern>
-
-
<url-pattern>/
url-pattern>
-
servlet-mapping>
wtf!!!》》》》》》》》????????也配置了呀?再看下一步
-
-
<context:annotation-config />
我在springmvc-context配置如上,在spring-context的配置如下
-
<context:component-scan base-package="com.bean.atcrowdfunding.*">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
context:component-scan>
也没毛病呀?????????
所以我回到了起点,替换掉了这个让人不熟悉的标签,而改成了熟悉的
-
<%
-
response.sendRedirect("index.do");
-
%>
终于可以跳转正常了,可是404。
后来,突然看到一句话,你是不是没配置
那么为什么静态资源访问不到呢?因为所有请求都被拦截了,包括静态资源,换句话说,就是只要用了@Controller,最好在springmvc-context.xml把
瞬间明朗了许多,我发现只要这两个标签加上了,
-
-
springmvc
-
class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
<init-param>
-
<param-name>contextConfigLocationparam-name>
-
<param-value>classpath:spring/springmvc-context.xmlparam-value>
-
init-param>
-
<load-on-startup>1load-on-startup>
-
servlet>
-
<servlet-mapping>
-
<servlet-name>springmvcservlet-name>
-
-
<url-pattern>/url-pattern>
-
servlet-mapping>