日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

大数据WEB阶段Spring框架(四)Spring-MVC

發(fā)布時(shí)間:2024/4/30 javascript 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大数据WEB阶段Spring框架(四)Spring-MVC 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring-MVC

零、目錄

  • Spring-MVC介紹
  • Spring-MVC入門案例
  • 使用注解形式簡(jiǎn)化Cotroller
  • 配置資源解析器
  • 三種方式獲取頁(yè)面?zhèn)鬟f過來(lái)得參數(shù)
  • 獲取參數(shù)時(shí)的亂碼問題
  • 日期獲取問題
  • 同名參數(shù)提交問題
  • 提交參數(shù)名稱與接收時(shí)的變量名稱不一致的問題
  • 文件上傳
  • 重定向和轉(zhuǎn)發(fā)

一、Spring-MVC介紹

  • 主要負(fù)責(zé)前端 控制的框架 , 主要負(fù)責(zé)頁(yè)面和servlet之間的交互
  • 可以與Spring無(wú)縫銜接
  • 回顧Servlet的缺點(diǎn):
  • 一個(gè)Servlet需要在web.xml中配置8行配置
  • 一個(gè)servlet只能處理一個(gè)邏輯 , 一張表最少需要增、 刪、 改、 查 四個(gè)servlet , 在web.xml文件中至少會(huì)有32+n行配置
  • 獲取的屬性只能獲取到字符串類型 , 需要自己手動(dòng)轉(zhuǎn)換成需要的類型。
  • 獲取參數(shù)比較繁瑣 , 需要手動(dòng)封裝到bean中。
  • Spring-mvc優(yōu)點(diǎn):
  • 不管工程中有多少個(gè)控制器(servlet) , 只需要在web.xml中配置一個(gè)Spring自帶的Servlet分發(fā)器即可
  • 在Spring-mvc中cotroller代替了servlet , 一個(gè)cotroller可以有多個(gè)業(yè)務(wù)處理
  • 獲取的參數(shù)支持自動(dòng)轉(zhuǎn)換 , 而且支持對(duì)象的自動(dòng)封裝
  • 二、Spring-mvc入門案例

  • 導(dǎo)入jar包:
  • 在web.xml文件中配置Spring-mvc請(qǐng)求分發(fā)器

    <!-- 配置servlet分發(fā)器 --><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:/applicationContext-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern><!-- / --></servlet-mapping>
  • 添加Spring配置文件 applicationContext-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!-- 配置控制層 --><bean name="/hello.action" class="com.tj.controller.HelloController2"></bean> <!-- 有springmvc注解之後就不用手動(dòng)配置了 --> </beans>
  • 創(chuàng)建HelloCotroller.java實(shí)現(xiàn)Cotroller接口

    public class HelloController2 implements Controller{public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {ModelAndView mav = new ModelAndView();//返回信息mav.addObject("msg", "需要返回的信息");//指定返回的視圖mav.setViewName("index.jsp");return mav;}}
  • 測(cè)試: 訪問本機(jī)ip地址:端口號(hào)/工程名/hello.action時(shí), 跳轉(zhuǎn)回主頁(yè)面
  • SpringMVC工作過程 , 在瀏覽器中輸入地址訪問時(shí)會(huì)由Spring分發(fā)器攔截 , 通過Spring容器找到相應(yīng)的controller后執(zhí)行對(duì)應(yīng)的方法 。
  • 三、通過注解形式簡(jiǎn)化Controller

  • 在Spring配置文件中開啟包掃描 ,開啟注解模式, 開啟SpringMVC注解開關(guān) , 并引入mvc的約束

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!-- 開啟注解模式 --><context:annotation-config /><!-- 開啟包掃描 --><context:component-scan base-package="com.tj"></context:component-scan><!-- 掃描com.tj子包下的所有類 --><!-- 配置控制層 --><!-- <bean name="/hello.action" class="comtj.controller.HelloController"></bean> --><!-- 有springmvc注解之後就不用手動(dòng)配置了 --><!-- 開啓mvc注解 --><mvc:annotation-driven/></beans>
  • 創(chuàng)建controller

    @Controller public class HelloController {@RequestMapping("/hello.action")public String helloaction(Model m){m.addAttribute("msg" , "哈哈哈哈哈");return "index.jsp";}}
  • SpringMVC的執(zhí)行流程:
  • 補(bǔ)充說(shuō)明:
  • 當(dāng)web工程修改名字的時(shí)候 , 還需要修改myeclipse向tomcat配置項(xiàng)目的名稱
  • 四、配置資源解析器

  • 在spring配置文件applicationContext-mvc.xml中配置資源解析器

    <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!-- 開啟注解模式 --><context:annotation-config /><!-- 開啟包掃描 --><context:component-scan base-package="com.tj"></context:component-scan><!-- 掃描com.tj子包下的所有類 --><!-- 配置控制層 --><!-- <bean name="/hello.action" class="comtj.controller.HelloController"></bean> --><!-- 有springmvc注解之後就不用手動(dòng)配置了 --><!-- 開啓mvc注解 --><mvc:annotation-driven/><!-- 配置資源解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/"></property><property name="suffix" value=".jsp"></property></bean></beans>
  • 修改controller層方法中的return語(yǔ)句 , 只需要返回頁(yè)面的名稱即可 , 資源解析器會(huì)自動(dòng)拼接前綴和后綴

    @RequestMapping("/hello.action")public String helloaction(Model m){m.addAttribute("msg" , "哈哈哈哈哈");//請(qǐng)求轉(zhuǎn)發(fā)到WEB-INF下的Hello.jsp頁(yè)面 return "Hello";}
  • 五、獲取頁(yè)面提交參數(shù)的三種方式

  • 假設(shè)需要獲取表單提交的name屬性
  • 通過HttpServletRequest方式獲取頁(yè)面提交的參數(shù)

    public class HelloController2 implements Controller{public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {ModelAndView mav = new ModelAndView();String name = (String) request.getAttribute("name");//返回信息mav.addObject("msg", "需要返回的信息");//指定返回的視圖mav.setViewName("index.jsp");return mav;}}
  • 快速獲取參數(shù)的方式

    @RequestMapping("/hello.action")public String helloaction(String name , Model m){//需要參數(shù)名稱 與表單中name屬性一致才能獲取到//獲取到之后會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)換//將參數(shù)帶到頁(yè)面顯示 m.addAttribute("msg" , name);return "Hello";}
  • 使用對(duì)象把獲取到的參數(shù)直接封裝

  • 這種方式需要bean中的屬性名稱與表單的name屬性一致
  • 獲取到的參數(shù)會(huì)自動(dòng)轉(zhuǎn)換為bean中 的類型
  • 要求bean中必須要有set方法和無(wú)參構(gòu)造
  • 參數(shù)判空 , 參數(shù)有效性的檢驗(yàn)可以放在bean中的set方法中進(jìn)行
  • 當(dāng)bean中有自定義類型時(shí),需要指明層級(jí)關(guān)系 如: user類中有dog屬性 , dog有自己的name , 這時(shí)表單中的name應(yīng)該為name=“dog.name”

    @RequestMapping("/hello.action")public String helloaction(User user, Model m){m.addAttribute("msg" , "哈哈哈哈哈");return "Hello";}
  • 六、 中文亂碼問題

  • 使用Spring-mvc代替servlet層之后 , 不在使用request , 和response 對(duì)象了, 但是亂碼問題 依然存在
  • 解決亂碼問題:

  • 在jsp頁(yè)面中指定pageEncoding=“utf-8”
  • 并且在web.xml配置文件中配置Spring-Mvc自帶的過濾器

    <!-- 配置Spring自帶的亂碼過濾器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
  • 但是Spring自帶的過濾器中只提供題post方法對(duì)應(yīng)的過濾 , 對(duì)get方法的請(qǐng)求沒有做處理(mmp) , 所以盡量使用post吧 。

  • 七、 日期問題

  • 瀏覽器 提交日期格式的參數(shù)時(shí) 格式為yyyy/MM/dd
  • 而Java中Date類默認(rèn)的日期格式為yyyy-MM-dd
  • 所以 如果不做處理瀏覽器會(huì)爆出400的錯(cuò)誤
  • 解決辦法:在controller層添加日期格式轉(zhuǎn)換的方法 , 如果多出需要添加可以寫一個(gè)BaseController類 , 并且把一些常用的功能方法 放入其中 , 后續(xù)所有的Controller繼承這個(gè)BaseController 。

    //日期格式轉(zhuǎn)化 , 使Spring日期格式與瀏覽器日期個(gè)數(shù)匹配@InitBinderpublic void initBinder(ServletRequestDataBinder binder){binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));}
  • 八、 多個(gè)同名參數(shù)提交問題

  • 如果提交像復(fù)選框一類 , 相同name多個(gè)值的參數(shù)時(shí),若接收參數(shù)使用的是String類型 , 則spring會(huì)把 多個(gè)值用“,”拼接起來(lái) , 合成一個(gè)String;若接收參數(shù)時(shí)使用String[] 接收 , 則會(huì)把多個(gè)同名 參數(shù)自動(dòng)放入這個(gè)String[] 數(shù)組中 。
  • 九、提交的參數(shù)名與接收時(shí)聲明的變量名不一只問題

  • 可以通過@RequestParam注解解決此問題

    @RequestMapping("/hello.action")public String helloaction(Model m , @RequestParam("name")String 名字){//使用“名字”這個(gè)變量接收了表單的name屬性m.addAttribute("msg" , "哈哈哈哈哈");return "Hello";}
  • @RequestParam的使用
  • 當(dāng)只需要指定接收參數(shù)的name時(shí) , 可以直接寫@RequestParam(“xxxx”) , 但是 , 此時(shí)實(shí)際上是使用的@RequestParam的value屬性 , 所以規(guī)范的寫法應(yīng)該是@RequestParam(value=”xxxx”)
  • @RequestParam還可以指定參數(shù)的默認(rèn)值 , 當(dāng)表單提交的屬性為空 , 或是獲取不到該屬性時(shí) , 使用默認(rèn)值:@RequestParam(value=”xxxx” , defaultValue=”好像沒有值啊”)
  • @RequestParam的required屬性用于指定該參數(shù)是不是必須有值 , 如果required=“true” ,表單值為空或或者獲取不到的情況下會(huì)報(bào)錯(cuò)
  • 十、 文件上傳

  • 修改表單提交的類型enctype的值

    <form action="register.action" method="post" enctype="multipart/form-data">
  • 在Spring配置文件中配置文件上床解析器

    <!-- 配置文件上傳解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="10485760"></property></bean>
  • 在Controller層中對(duì)應(yīng)對(duì)應(yīng)的方法列表中添加MultipartFile參數(shù)

    //文件上傳@RequestMapping("/register.action")public String register( Model m , MultipartFile photo){String fillename = photo.getName();String originalFilename = photo.getOriginalFilename();long size = photo.getSize();System.out.println("字段名:"+fillename);System.out.println("文件名:"+originalFilename);System.out.println("文件大小:"+size);m.addAttribute("msg", originalFilename+"上傳成功!");return "Hello";}
  • 十一、轉(zhuǎn)發(fā)和重定向

  • 重定向: 兩次請(qǐng)求 , 兩次響應(yīng), 不能傳遞參數(shù) , 地址會(huì)發(fā)生改變
  • 轉(zhuǎn)發(fā): 一次請(qǐng)求 , 一次響應(yīng) , 可以再請(qǐng)求鏈中傳遞參數(shù) , 地址不會(huì)發(fā)生改變
  • SpringMVC中默認(rèn)使用請(qǐng)求轉(zhuǎn)發(fā)

    @RequestMapping("/hello.action")public String helloaction(Model m , @RequestParam(value="name" )String 名字){m.addAttribute("msg" , "哈哈哈哈哈");return "Hello";}
  • 如果需要重定向 , 需要在返回的地址中添加redirect
  • @RequestMapping(“/rrr.action”)
    public String helloaction(Model m , String name){
    m.addAttribute(“msg” , “哈哈哈哈哈”);

    return "redirect:hello.action";//重定向的是一個(gè)請(qǐng)求 , 而不是資源的地址 }

    5. 注意:
    1. 一般情況下除了主頁(yè)面在webroot目錄下 , 其他的頁(yè)面文件都放在WEB-INF目錄下, 不能直接訪問, 需要訪問時(shí) , 先請(qǐng)求Controller層中對(duì)應(yīng)的邏輯處理方法后轉(zhuǎn)發(fā)到訪問的資源 。
    2. 請(qǐng)求重定向時(shí)也不能直接訪問WEB-INF下的資源 , 所以重定向時(shí)redirect:后跟的是一個(gè)請(qǐng)求 , 而不是資源的地址

    總結(jié)

    以上是生活随笔為你收集整理的大数据WEB阶段Spring框架(四)Spring-MVC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。