Spring4.0MVC學習資料,Controller中的方法詳解和使用(四)
在以前,mvc的框架,基本上就是struts框架了。但是現在不一樣了。springmvc出來了。spring的mvc框架不亞於struts了,springmvc出來了,我們有了更多的選擇。
Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏麵。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,可以選擇是使用內置的 Spring Web 框架還可以是 Struts 這樣的 Web 框架。通過策略接口,Spring 框架是高度可配置的,而且包含多種視圖技術,例如 JavaServer Pages(JSP)技術、Velocity、Tiles、iText 和POI。Spring
MVC 框架並不知道使用的視圖,所以不會強迫您隻使用 JSP 技術。Spring MVC 分離了控製器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定製。
jar包引入我就不講了,在前麵幾章已經說過,而且我的下載資源裏也有相應的jar包進行下載。
我們看看web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定義servlet.xml配置文件的位置和名稱,默認為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/herman/ss/config/testAjax.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.herman</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>在看看spring配置文件,testAjax.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx" xmlns:p="https://www.springframework.org/schema/p" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4.0.xsd https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 啟用spring mvc 注解 --> <context:annotation-config /> <!-- 自動注解 --> <context:component-scan base-package="com.herman.ss.controller"></context:component-scan> <!-- 配置http請求的json映射消息轉換器 --> <bean > <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 完成請求和注解POJO的映射 --> <bean > <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <!-- 對轉向頁麵的路徑解析。prefix:前綴, suffix:後綴 --> <bean p:prefix="/" p:suffix="" /> </beans>ok,一切準備就緒,看看HermanController.java
package com.herman.ss.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("super") public class HermanController { @RequestMapping("testAjax") @ResponseBody public String testAjax(){ System.out.println("我是測試Spring mvc的......"); return "{id:4,text:'Spring mvc'}"; } @RequestMapping("html") public String testHtml(){ //請求被重定向到herman.jsp頁麵 return "redirect:/herman.jsp"; } @RequestMapping("testforward") public String testforward(){ //請求被轉發到forward.jsp頁麵 return "forward:/forward.jsp"; } @RequestMapping(value="root") public String root(){ return "root.jsp"; } @RequestMapping(value="test") public String test(){ return "html/test.html"; } @RequestMapping(value="suffix") public String suffix(){ //如果想返回不帶後綴的頁麵,則必須在配置xml中配置p:suffix="" //如:p:suffix=".jsp" return "suffix"; } @RequestMapping(value="mav") public ModelAndView mav(){ //如果想返回不帶後綴的頁麵,則必須在配置xml中配置p:suffix="" //如:p:suffix=".jsp" ModelAndView mav=new ModelAndView(); mav.setViewName("html/mav.html"); return mav; } @RequestMapping(value="request") public ModelAndView request(HttpServletRequest request, HttpServletResponse response){ //帶request請求參數的示例 //如:p:suffix=".jsp" ModelAndView mav=new ModelAndView(); mav.setViewName("html/mav.html"); System.out.println(request.getParameter("testVale")); return mav; } @RequestMapping(value="request") public ModelAndView session(HttpSession httpSession, HttpServletResponse response){ //帶request請求參數的示例 //如:p:suffix=".jsp" ModelAndView mav=new ModelAndView(); mav.setViewName("html/mav.html"); return mav; } }好吧就到這裏結束了吧,我們下章再見。歡迎大家關注我的博客!!
如有不懂,疑問或者欠妥的地方,也或者需要源碼,請加QQ群:135430763 進行反饋,共同學習!
最後更新:2017-04-03 05:39:51