springmvc全稱是spring web mvc,是spring框架一部分,是一個mvc的框架,和struts2同樣是一個表現層框架。css
原理簡寫:DispatcherServlet-->映射器-->適配器-->視圖解析器-->頁面html
新建一個java的web項目,在web目錄下新建一個lib目錄,存在jar包java
3.2:編寫配置文件jquery
在web.xml中配置以下內容:web
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
其中servlet是用來攔截請求,交給spring管理;init-param來加載配置文件;url-pattern經常使用*.do或*.actionspring
在src目錄下或者本身建立一個config資源目錄下建立springmvc.xml文件,引入以下約束信息:編程
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<beans>
建立一個springmvc的handler類,來處理請求,至關於servlet。json
package com.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class SpringMvc implements Controller{ @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index.jsp");//設定視圖,相對的是web return modelAndView; } }
在springmvc.xml中配置:數組
<!-- 把SpringMvc類交給springmvc管理 --> <bean id="springmvc" class="com.controller.SpringMvc"></bean> <!-- 配置適配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 配置處理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!--key表示的是訪問的路徑--> <prop key="/springmvc.action">springmvc</prop> </props> </property> </bean>
使用註解就不須要說明那麼麻煩,也是在springmvc.xml中配置。瀏覽器
註解的處理器映射器:
spring3.1版本以前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
3.1版本以後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
註解的處理器適配器:
spring3.1以前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
3.1以後使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
<!-- 註解處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 註解處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
以上代碼能夠簡寫爲兩行:
<!--配置註解處理器映射器和適配器--> <mvc:annotation-driven></mvc:annotation-driven>
注意,還要進行包掃描:
<!--包掃描--> <context:component-scan base-package="com.controller"/>
而後新建一個handler類:
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class SpringMvc2{ //配置註解映射,必定要加/ @RequestMapping("/show.action") public ModelAndView show(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index2.jsp"); return modelAndView; } }
新建一個index.jsp,在裏面寫入內容,而後去訪問springmvc.action,效果以下:
兩種方式配置的效果是同樣的,經常使用註解配置。
一個類中能夠有多個映射,能夠有多個方法。
ModelAndView主要是爲了跳轉頁面,所以這個方法也能夠簡寫。
1.直接返回一個路徑字符串
@RequestMapping("/show2.action") public String show2(){ return "index2.jsp"; }
2.訪問內部私有的資源,外部不能訪問
//使用程序訪問私有的資源 @RequestMapping("/show3.action") public String show3(){ return "WEB-INF/index3.jsp"; }
3.在springmvc.xml中配置前綴和後綴
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 指定視圖路徑前綴 --> <property name="prefix" value="/view/" /> <!-- 指定視圖路徑後綴 --> <property name="suffix" value=".jsp" /> </bean>
配置了前綴和後綴以後,返回的路徑就很簡單,不須要前綴和後綴,可是有侷限性,不常用。
4.編碼處理:設置一個編碼過濾器,參數等不在亂碼。web.xml中配置:
<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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
springmvc接收請求的key/value串(好比:id=2&type=101),通過類型轉換,將轉換後的值賦值給controller方法的形參,這個過程就叫參數綁定。
在controller方法形參中以下類型是能夠直接綁定成功,springmvc框架給如下類型的參數直接賦值:
HttpServletRequest:經過request對象獲取請求信息
HttpServletResponse:經過response處理響應信息
HttpSession:經過session對象獲得session中存放的對象
Model/ModelMap:ModelMap是Model接口的實現類,經過Model或ModelMap向頁面傳遞數據
@RequestMapping("/login4.action") public void login4(HttpServletRequest request, HttpServletResponse response) throws IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); response.sendRedirect("loginSuccess.jsp"); }
其餘都雷同,比較麻煩,不推薦使用
Integer、string、boolean、float。。。
綁定規則:對於基本類型參數綁定,當請求的參數的名稱和controller方法的形參名一致時能夠綁定成功。針對當前類型作類轉換,當類型明確時使用明確的類型,當類型不明確時,可使用string類型,須要時再強轉。
@RequestParam:取別名
@RequestBody:把請求體中的數據封裝到對象中,請求體中的內容必須是json格式
若是請求的參數的名稱和controller方法的形參名不一致時,如何綁定?
就要使用@RequestParam進行綁定:@RequestParam(value="ids") Integer id ,將請求的參數名爲ids的值綁定方法形參的id上,Value能夠省略.
@RequestMapping("/login4.action") public void login4(@RequestParam(value="id") int id2) { System.out.println(id2);
多個參數
@RequestMapping("/login2.action") public void login(String username,String password){ System.out.println(username+" "+password); }
簡單pojo:pojo中都基本類型
Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="get" action="login.action"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit"> </form> </body> </html>
User類
package com.entity; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
獲取內容
@RequestMapping("/login3.action") public void login2(User user){ System.out.println(user); }
一個類中不只有簡單數據類型,還有對象屬性。那麼頁面須要使用相應的類標識屬性名,以下:
<form method="get" action="login.action"> <input type="text" name="name"> <input type="text" name="user.username"> <input type="text" name="user.password"> <input type="submit"> </form>
實體類
package com.entity; public class OtherUser { private String name; private User user; public String getName() { return name; } public void setName(String name) { this.name = name; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "OtherUser{" + "name='" + name + '\'' + ", user=" + user + '}'; } }
獲取內容
@RequestMapping("/login5.action") public void login5(OtherUser otherUser){ System.out.println(otherUser); }
<form method="get" action="login.action"> <input type="checkbox" name="hobby" value="籃球">籃球 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="羽毛球">羽毛球 <input type="submit"> </form>
獲取選中的內容
@RequestMapping("/login.action") public void login3(String[] hobby){ for (String s : hobby) { System.out.println(s); } }
requestMapping註解的做用:對controller方法進行映射。
URL路徑映射:requestMapping指定url,對controller方法進行映射。
@RequestMapping("/login.action")
化請求映射:爲了更好的管理url,爲了不url衝突,能夠在class上使用requestMapping指定根url。
@Controller @RequestMapping("/user") public class UserController { @RequestMapping("/login3.action") public void login2(User user){ System.out.println(user); }}
訪問時使用user/login3.action。在開發時候,須要提早進行url規劃,以免後期修改url後,須要大量修改頁面上的url地址。
請求方法限定:經過requestMapping限制http的請求方法,能夠提升系統安全性。
@RequestMapping(value="/login2.action",method = RequestMethod.GET) public void login(String username,String password){ System.out.println(username+" "+password); }
若是沒有經過指定的方式來訪問該請求就會出現以下錯誤
6、Controller方法返回值
controller方法中定義ModelAndView對象並返回,對象中可添加model數據、指定邏輯視圖名。
@Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index.jsp"); return modelAndView; }
相似原始serlvet 的開發。
響應結果的三種方法:
1、使用request轉發頁面,以下:
request.getRequestDispatcher("頁面路徑").forward(request, response);
2、也能夠經過response頁面重定向:
response.sendRedirect("url")
3、也能夠經過response指定響應結果,例如響應json數據以下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
1.頁面轉發方式
格式是:forward:轉發地址(不能寫http://,只能寫action的地址)
特色:轉發的上一個請求request和要轉發的地址共用request,轉發後瀏覽器的地址是不變化。
@RequestMapping("/login2.action") public String login2(){ return "forward:index.jsp"; }
2.頁面重定向方式
格式是:redirect:重定向地址(好比:http://.....)
特色:重定的上一個請求request和要重定的地址不公用request,重定後瀏覽器的地址是變化的。
@RequestMapping("/login2.action") public String login3(){ return "redirect:index.jsp"; }
3.表示邏輯視圖名
返回一個string若是即不是轉發格式,也不是重定向的格式,就表示一個邏輯視圖名。
@RequestMapping("/login2.action") public String login(){ return "index.jsp"; }
數據回顯就是把數據傳給頁面,在jsp頁面能夠直接經過${}來獲取內容
@Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { ModelAndView modelAndView=new ModelAndView(); //傳遞參數給頁面,相似於域對象 modelAndView.addObject("msg","這是mvc"); modelAndView.setViewName("index.jsp"); return modelAndView; }
在請求轉發時經常使用。
@RequestMapping("/login6.action") public String login6(Model model){ model.addAttribute("msg","我是model傳遞過來的數據"); return "index.jsp"; }
//把方法的返回值封裝到model中,攜帶到要跳轉的頁面中 //添加註解後這個方法是全局的,無論任何一個方法執行都會調用這個方法,不經常使用 @ModelAttribute public User getUser(){ User user=new User(); user.setUsername("hhh"); user.setPassword("123"); return user; } //在jsp頁面可使用${user.username}的方式取到值
以下代碼會把傳遞過來的user放到model中攜帶到要跳轉的界面
//將傳入的參數做爲參數返回到頁面 @RequestMapping("/login2.action") public String login2(@ModelAttribute("user") User user){ return "index.jsp"; } //在jsp頁面可使用${user.username}的方式取到值
當使用return 「index.jsp」傳遞數據給頁面時,可使用request方法,上面已經介紹了,下面介紹重定向傳遞數據。重定向傳遞數據使用RedirectAttributes類,使用方式見下面代碼:
addAttribute能夠把參數傳遞給頁面,可是隻是拼接在地址欄中
@RequestMapping("/login.action") public String login(RedirectAttributes attributes){ attributes.addAttribute("msg","我是addAttribute"); return "redirect:index.jsp"; }
addFlashAttribute把參數傳遞給頁面,存放在session的map中,使用完畢後自動清空session。若是要獲取參數,不能直接獲取,要使用@ModelAttribute
@RequestMapping("/login.action") public String login(RedirectAttributes attributes){ attributes.addFlashAttribute("msg","我是addFlashAttribute"); return "redirect:index.jsp"; }
以上兩種方式雖然均可以傳遞參數到頁面,可是頁面不能顯示這些數據,所以須要其餘方式結合來使用。
第一種方式:內部跳轉(分發器)
@Controller public class Login { @RequestMapping("/login.action") public String login(RedirectAttributes attributes){ attributes.addFlashAttribute("msg","我是addFlashAttribute"); //這裏不直接跳轉到頁面,而是內部跳轉,而後再跳轉到頁面 return "redirect:login_jsp.action"; } @RequestMapping("login_jsp.action") public String login(@ModelAttribute("msg")String msg){ System.out.println(msg); return "index.jsp"; } }
第二種方式:直接將數據存放到session中,相似於以前的servlet中重定向的傳值,獲取到數據後清空session.
當異常發生時跳轉到錯誤的頁面,給用戶相應的提示
一、自定義一個異常類用於拋出異常
package com.controller; public class MyException extends Exception { //自定義異常,繼承Exception,重寫父類中的部分方法 public MyException() {} public MyException(String message) { super(message); } }
二、建立一個異常處理器,實現ExceptionResolver接口,並交給springmvc管理
package com.controller; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class HandlerException implements HandlerExceptionResolver { //建立一個異常處理器,實現ExceptionResolver接口 //只要任何一層發生異常,就會調用下面的方法 @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView modelAndView=new ModelAndView(); //判斷是自定義異常仍是系統異常 if(e instanceof MyException){ modelAndView.addObject("msg",e.getMessage()); }else{ modelAndView.addObject("msg","服務器飛啦,請稍後再試"); } modelAndView.setViewName("error.jsp"); return modelAndView; } }
三、寫一個類,測試異常
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Test { @RequestMapping("test.action") public void test() throws MyException { //這裏發生自定義異常,用於某些特定異常發生時處理異常 MyException myException=new MyException("我是自定義異常"); throw myException; } @RequestMapping("test2.action") public void test2(){ //這裏會發生系統異常 int i=10/0; } }
四、錯誤頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body><center> <img src="http://img1.imgtn.bdimg.com/it/u=2272026066,3668786831&fm=26&gp=0.jpg"><br> ${msg}</center> </body> </html>
1.導入所須要的jar
2.建立上傳文件的jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <!--文件上傳必須按照下面的格式寫,缺一不可--> <form action="upload.action" enctype="multipart/form-data" method="post"> <input type="file" name="file"> <input type="submit"> </form> </body> </html>
3.建立接收文件上傳的controller
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.UUID; @Controller public class UploadController { @RequestMapping("/upload.action") public String upload(MultipartFile file) throws IOException { //多文件上傳MultipartFile[] file,遍歷這個數組 if(file!=null&& file.getOriginalFilename()!=null&&!file.getOriginalFilename().equals("")){ //獲取文件的名字 String fileName = file.getOriginalFilename(); //獲取文件的後綴名 int index = fileName.lastIndexOf("."); //UUID這個方法是給文件命名,不重名 String newFileName =UUID.randomUUID()+fileName.substring(index); File NewFile=new File("E:\\upload",newFileName); //把上傳的文件的內容保寫到保存的位置 file.transferTo(NewFile); } return "success.jsp"; } }
4.在springmvc.xml中配置文件解析器
<!-- Multipart解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設置上傳文件的最大尺寸爲5MB --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean>
5.配置虛擬目錄
將文件上傳到固定的物理目錄下,經過虛擬目錄 訪問物理目錄 下的圖片。
在tomcat上配置虛擬目錄,訪問虛擬目錄能夠訪問虛擬目錄對應物理目錄
在server.xml添加如下代碼:
<Context docBase="F:\upload" path="/upload/files"/>
@RequestBody註解將json數據轉成java對象 ..test(@RequestBody User user){}
@ResponseBody註解實現將java對象轉成json輸出
導入jackson包或fastjson包,springmvc默認是使用jackson包。
1)導入jackson包並配置springmvc.xml
若是使用xml配置適配器和映射器,須要以下代碼:
<!--註解適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
若是使用註解配置適配器和映射器,就不要再配置。
2)導入fastjson包並配置springmvc.xml
<!--配置註解處理器映射器和適配器--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
1.register.jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script src="${pageContext.request.contextPath}/js/jquery-1.11.0.min.js"></script> </head> <body> <input type="text" name="username" id="username"><font id="msg"></font> <script> $(function(){ $("#username").blur(function () { var username=$(this).val(); $.post("regist.action",{"username":username},function (data) { $("#msg").html(data.message); }); }); }) </script> </body> </html>
2.UserController類
package com.controller; import com.entity.JsonResult; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @RequestMapping("regist.action") @ResponseBody public JsonResult regist(String username){ if("zys".equals(username)){ return new JsonResult(false,"用戶名已存在"); }else{ return new JsonResult(true,"驗證經過"); } } }
3.註解介紹
@Controller
//是將這個類交給springmvc管理 @RestController
//它是等於@Controller+@ResponseBody,當這個類中的方法所有有返回值時能夠直接使用,省去兩個註解,它會在每一個方法前面自動加@ResponseBody那麼這個類只能作返回,不能頁面跳轉
RESTful,即Representational State Transfer的縮寫,表現層狀態轉化。所謂"資源",就是網絡上的一個實體,或者說是網絡上的一個具體信息,應四種基本操做:GET用來獲取資源,POST用來新建資源(也能夠用於更新資源),PUT用來更新資源,DELETE用來刪除資源。
url模板映射:@RequestMapping(value="/ user/{id}"):{×××}佔位符,請求的URL能夠是「/user/1」或「/user/2」,經過在方法中使用@PathVariable獲取{×××}中的×××變量。
@PathVariable用於將請求URL中的模板變量映射到功能處理方法的參數上。
在Controller中添加以下代碼:
@RequestMapping("/test3/{id}") public void test3(@PathVariable("id")int id){ System.out.println("id"+id); }
在瀏覽器進行測試,控制檯會打印id47
注意:使用restful時,web.xml中url-pattern必須是/,不能是其餘的,那麼這樣又會攔截一些須要的靜態資源,因此須要在springmvc.xml中配置來加載靜態資源:
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> mapping表明映射文件目錄,**表示當前以及子類的全部文件夾,*只是當前文件夾
restful的使用(瞭解)
//restful四種方式的使用(瞭解) @RequestMapping(value = "/user/{abc}",method = RequestMethod.GET) public void add(@PathVariable("abc") int id){ System.out.println("獲取數據"+id); } @RequestMapping(value = "/user/{abc}",method = RequestMethod.DELETE) public void delete(@PathVariable("abc") int id){ System.out.println("刪除數據"+id); } @RequestMapping(value = "/user/{abc}",method = RequestMethod.PUT) public void update(@PathVariable("abc") int id){ System.out.println("更新數據"+id); } @RequestMapping(value = "/user/{abc}",method = RequestMethod.POST) public void newRes(@PathVariable("abc") int id){ System.out.println("新建數據"+id); }
springmvc提供攔截器實現對Handler進行面向切面編程,能夠Handler執行以前、以後、之中添加代碼,這種方式就是切面編程。
1.定義一個類實現HanlderInterceptor接口
package com.controller; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Myintercept implements HandlerInterceptor { //preHandle:在Handler執行以前調用 @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("pre..."); //return true表明放行 //return flase表明攔截 return true; } //postHandle:在Handler中方法執行一半時調用(return ModelAndView前),能夠更改跳轉的視圖 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("post..."); } //afterCompletion:在Handler執行完畢以後調用,能夠用於異常的處理 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("after..."); } }
2.在springmvc.xml中配置攔截器
<!--配置攔截器--> <mvc:interceptors> <!--設置一個攔截路徑,也能夠寫多個 <mvc:interceptor>攔截多個路徑--> <mvc:interceptor> <!--path裏面是要攔截的路徑--> <mvc:mapping path="/test.action"/> <!--把Myintercept交給springmvc管理--> <bean class="com.controller.Myintercept"></bean> </mvc:interceptor> </mvc:interceptors>
dao層:mybatis+spring
service層:spring
controller層:springmvc+spring
整合步驟: