建立一個java類:
package com.myproject.cn;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MyController implements Controller {
@Override
//注意:因為MyController這個類名字是随意起的,那麼也就是說它是需要進行注冊的。是以我們應該在xml裡面書寫配置環境。
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//因為要傳回ModelAndView,是以我在這裡new一個對象出來。
ModelAndView mv = new ModelAndView();
//因為mv裡面沒有數值是以我們就要進行初始化。
mv.addObject("message","Hello SpringMVC World!");
//設定視圖
mv.setViewName("/WEB-INF/jsp/welcome.jsp");//讓這個頁面直接跳轉到jsp頁面。
//在web-inf下面進行建立jsp
/*注意:在web-inf下面的資源與以及在src下面存放的資源是有明顯的差別的。
* (1)在web-inf下面存放的資源是不能夠通過浏覽器直接通路到的,比較安全,意味着重定向(重定向是浏覽器發出的第二次請求 )的時候是不能重定向到web-inf下面的。隻能是背景服務端的程式才可以直接跳轉過去的。
* (2)
*/
return mv;
}
}
注冊中央處理器:在src下面建立一個springmvc.xml,進行注冊中央處理器,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注冊視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 注冊處理器 -->
<bean id="/my.do" class="com.myproject.cn.MyController"/>
</beans>
注意:因為MyController這個類名字是随意起的,那麼也就是說它是需要進行注冊的。是以我們應該在xml裡面書寫配置環境。是以在web-inf下面進行注冊;
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 注冊中央排程器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springMVC配置檔案的位置及檔案名 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 在Tomcat啟動時直接建立目前Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在浏覽器中運作即可。如果運作處問題,比如說:

那麼,第一種可能是:
第二種可能是:
可能是同時運作了幾個項目,然後項目之間互相影響。解決辦法:
找到 tomcat檔案,然後在tomcat裡面删除一些項目,如圖:
第三種可能是路徑不對,你看看springmvc的路徑是在src下面還是在web-inf下面。本題是在src下面;
第四種可能是tomcat出問題了。但是這個可能性就是比較低的,還是有可能出現這種情況。