1、簡介 前端
Spring MVC採用了鬆散耦合的可插拔組件結構,比其餘的MVC框架更具備靈活性和擴展性,Spring MVC經過使用一套註解,使一個Java類成爲前端控制器(Controller),不須要實現任何接口,同時,Spring MVC支持RES形式的URL請求,除此以外,Spring MVC在在數據綁定、視圖解析、本地化處理及靜態資源處理上都有許多不俗的表現。
Spring MVC圍繞DispatcherServlet(前端控制器)爲中心展開,DispatcherServlet(前端控制器)是Spring MVC的中樞,和MVC的思想同樣,它負責從視圖獲取用戶請求而且分派給相應的處理器處理,並決定用哪一個視圖去把數據呈現給給用戶java
2、搭建springMVC步驟 web
一、導包 spring
二、配置前端控制器數組
Ctrl+shift+t 搜索DispatchServlettomcat
在web.xml中app
<!-- 配置前端控制器 --> <servlet> <servlet-name>aaa</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加載配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value><!-- 配置文件的位置 classpath表明src spring.xml放在src目錄下 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>aaa</servlet-name> <url-pattern>/</url-pattern><!-- 攔截規則 --> </servlet-mapping>
三、掃描controller框架
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 之後會配一些須要的配置 上面爲一些頭部約束自帶(可在官網上自行粘貼) --> <!-- 1掃描controller包 --> <context:component-scan base-package="com.zy.controller"></context:component-scan> </beans>
四、建立controllerjsp
package com.zy.controller; import java.util.ArrayList; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.zy.entity.A; import com.zy.entity.User; //使用註解開發時,須要在配置文件中掃描該包,註解才能生效 @Controller @RequestMapping("/zy")//請求映射 public class MyController {//接收請求,完成響應 //1表示控制器 //2用@RequestMapping接收請求---接收請求的規則 //3ModelAndView【模型和視圖】 //4ModelAndView 這個對象具有兩個功能,1數據處理功能2視圖跳轉功能 @RequestMapping("/goabc")//請求映射 public ModelAndView go(){//類上的@RequestMapping和方法上的@RequestMapping構成一個完整的請求映射 /zy/goabc //響應到abc.jsp ModelAndView mv = new ModelAndView(); mv.setViewName("/abc.jsp"); return mv; } //接收參數 @RequestMapping("/show") public ModelAndView show(int a,@RequestParam("b")int buy,String name){ ModelAndView mv = new ModelAndView(); //接收參數,直接在方法的括號裏接收,只要名字對應上就能夠 //若是參數名不一致,可使用@RequestParam("b")設置別名接收參數 System.out.println("a的值"+a); System.out.println("b的值"+buy); System.out.println("name"+name); return mv; } @RequestMapping("/add") public ModelAndView add(User uu){//該對象會自動接收表單中與屬性名匹配的值 //痛點servlet參數必須一個一個用request接收 //SpringMVC 支持對象接收參數 System.out.println(uu); return null; } @RequestMapping("/array") public void arr(String []a){//數組接收(傳遞過來一組數據) for (String v : a) { System.out.println(v); } } //@RequestMapping("/list")//集合在springMVC中不能獨立接收參數 //public void list(ArrayList<String> list){//集合接收---失敗 // for (String v : list) { // System.out.println(v); // // } //} //若是用集合接收一組數據,能夠把該集合放在對象中當作屬性來用,就能夠接收了 @RequestMapping("/list") public void list(A a){ //數據會自動接收到a對象中的list屬性上 for (String v : a.getList()) { System.out.println(v); } } }
package com.zy.entity; import java.util.ArrayList; public class A { //用來包裝集合接收參數 private ArrayList<String> list; public ArrayList<String> getList() { return list; } public void setList(ArrayList<String> list) { this.list = list; } }
<form action="zy/add" method="post"> 添加 姓名:<input name="name"><br> 年齡:<input name="age"><br> 性別:<input name="sex"><br> 地址:<input name="address"><br> 生日:<input name="birthday"><br> <input type="submit"> </form> 數組 <!-- 數組 --> <form action="zy/array" method="post"> 姓名:<input name="a"><br> 年齡:<input name="a"><br> 性別:<input name="a"><br> 地址:<input name="a"><br> 生日:<input name="a"><br> <input type="submit"> </form> 集合 <form action="zy/list" method="post"> 姓名:<input name="list[0]"><br> 年齡:<input name="list[1]"><br> 性別:<input name="list[2]"><br> 地址:<input name="list[3]"><br> 生日:<input name="list[4]"><br> <input type="submit"> </form>
3、中文亂碼問題post
在tomcat9之下,get請求沒有中文亂碼
其餘版本 找到tomcat的server.xml,修改
Post請求會產生中文亂碼(SpringMVC已經準備好了編碼過濾器,咱們只須要配置上就ok)
在web.xml中
<!-- / /* /*範圍更廣,包括jsp的攔截 -->
<!-- 配置編碼過濾器 --> <filter> <filter-name>bbb</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>bbb</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>