javascript
大数据WEB阶段Spring框架(四)Spring-MVC
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介紹
二、Spring-mvc入門案例
在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;}}三、通過注解形式簡(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";}}四、配置資源解析器
在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ù)的三種方式
通過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ù)直接封裝
當(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";}六、 中文亂碼問題
解決亂碼問題:
并且在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吧 。
七、 日期問題
解決辦法:在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ù)提交問題
九、提交的參數(shù)名與接收時(shí)聲明的變量名不一只問題
可以通過@RequestParam注解解決此問題
@RequestMapping("/hello.action")public String helloaction(Model m , @RequestParam("name")String 名字){//使用“名字”這個(gè)變量接收了表單的name屬性m.addAttribute("msg" , "哈哈哈哈哈");return "Hello";}十、 文件上傳
修改表單提交的類型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ā)和重定向
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";}@RequestMapping(“/rrr.action”)
public String helloaction(Model m , String name){
m.addAttribute(“msg” , “哈哈哈哈哈”);
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据WEB阶段Spring框架(三)声
- 下一篇: 大数据WEB阶段 Servlet配置优先