代碼如下:
1.SpringMVC的 web.xml文件的編寫:(注冊dispatcherServlet)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!--1.dispatcherServlet--> <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-servlet.xml</param-value> </init-param> <!--啟動級別--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.SpringMVC配置文件的編寫:(注解自動掃描,靜態資源的過濾,注解驅動,視圖解析器)
<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--包下注解的自動掃描 @Controller--> <context:component-scan base-package="com.xbf.controller"/> <!--靜態資源過濾 :springmvc不處理靜態資源--> <mvc:default-servlet-handler/> <!--注解驅動 @RequestMapping("****")--> <mvc:annotation-driven/> <!--視圖解析器 就是一個bean--> <!--/WEB-INF/jsp/ 該路徑下的資源比較安全,用戶不能直接訪問--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.Controller層的編寫:(職能:進行數據模型的處理,並返回視圖和數據模型)
package com.xbf.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class ModelTest { //1.使用 ModelAndView對象 @RequestMapping("/t1") public ModelAndView test1(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","modelandview is nice"); mv.setViewName("test"); return mv; } //2.使用servletAPI //請求轉發 request 可攜帶參數 //頁面重定向 response 不能攜帶參數 @RequestMapping("/t2") public void test2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { request.setAttribute("msg","servletAPI is nice"); //請求轉發進行頁面的跳轉 request.getRequestDispatcher("index.jsp").forward(request,response); } //3.使用springmvc 但不用視圖解析器 @RequestMapping("/t3") public String test3(HttpServletRequest request){ request.setAttribute("msg","Controller Test3"); return "redirect:index.jsp"; //重定向不能攜帶參數值 } @RequestMapping("/t33") public String test33(HttpServletRequest request){ request.setAttribute("msg","Controller Test3"); return "forward:index.jsp"; //請求轉發可以攜帶參數 } //4. 使用springmvc 並且使用視圖解析器 //使用Model 對象可以放參數值 @RequestMapping("/t4") public String test4(){ return "test"; } @RequestMapping("/t44") public String test44(Model model){ model.addAttribute("msg","Springmvc use 視圖解析器"); return "test"; } }
4.前端頁面:(獲取到controller層的數據模型並進行渲染后,再返回)
尷尬也沒怎么渲染只是將數據拿出來看看 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>servletAPI</title> </head> <body> ${msg} </body> </html>
總結點:
通過ModleAndView對象可以將頁面跳轉換到 web-inf目錄下;
請求轉發(request)和頁面重定向(response)不能將頁面跳轉到web-inf目錄下,會報 404錯誤!
並且重定向(response)不能攜帶參數值;
lk