<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # SpringMVC框架 ## **hello springmvc** 什么是Spring MVC? Spring MVC 為展現層提供的基于 MVC 設計理念的優秀的Web 框架,是目前最主流的 MVC 框架之一。 Spring3.0 后全面超越 Struts2,成為最優秀的 MVC 框架。 導入jar包 我們基于Spring mvc框架進行開發,需要依賴一下的spring jar包: spring-aop-4.0.4.RELEASE.jar spring-beans-4.0.4.RELEASE.jar spring-context-4.0.4.RELEASE.jar spring-core-4.0.4.RELEASE.jar spring-expression-4.0.4.RELEASE.jar spring-web-4.0.4.RELEASE.jar spring-webmvc-4.0.4.RELEASE.jar commons-logging-1.1.1.jar(用來打印log) 在WEB-INF目錄下新建lib文件夾,并將上面的jar包放入其中。 配置文件web.xml(WEB-INF下) ``` <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置DispatchcerServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Spring mvc下的配置文件的位置和名稱 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> ``` 注意: <param-value>classpath:springmvc.xml</param-value>用于配置spring mvc的配置文件的位置和名稱,這里說明會新建一個springmvc.xml的配置文件。 這里的servlet-mapping表示攔截的模式,這里是“*.do”,表示對于.do結尾的請求進行攔截。 Springmvc.xml(scr下)   在src目錄下新建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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自動掃描的包 --> <context:component-scan base-package="com.neusoft.controller"></context:component-scan> <!-- 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/views/"></property> <property name = "suffix" value = ".jsp"></property> </bean> </beans> ``` <context:component-scan base-package="com.neusoft.controller"></context:component-scan> 表示spring監聽的范圍,這里是在com.neusoft.controller下 HelloWorldController.java(com.neusoft.controller下) ``` package com.neusoft.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { /** * 1. 使用RequestMapping注解來映射請求的URL * 2. 返回值會通過視圖解析器解析為實際的物理視圖, 對于InternalResourceViewResolver視圖解析器,會做如下解析 * 通過prefix+returnVal+suffix 這樣的方式得到實際的物理視圖,然后會轉發操作 * "/WEB-INF/views/success.jsp" * @return */ @RequestMapping("/helloworld.do") public String hello(){ System.out.println("hello world"); return "success"; } } ``` 1.首先要在類的前面添加“Controller”注解,表示是spring的控制器,這里會寫一個方法hello() 2. hello方法上方有一個@RequestMapping, 是用于匹配請求的路徑,比如這里匹配的請求路徑就是“http://localhost:8080/helloworld.do”,即當tomcat服務啟動后,在瀏覽器輸入這個url時,如果在這個方法打斷點了,就會跳入該方法。 3. 這個return的結果不是亂寫的,這個返回的字符串就是與上面springmvc.xml中進行配合的,springmvc.xml中聲明了prefix和suffix,而夾在這兩者之間的就是這里返回的字符串,所以執行完這個方法后,我們可以得到這樣的請求資源路徑“/WEB-INF/views/success.jsp”,這個success.jsp是需要我們新建的 ### **index.jsp(WebContent下)** 在新建success.jsp之前,我們需要有一個入口,也就是這里的index.jsp ``` <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="helloworld.do">hello world</a> </body> </html> ``` 當訪問index.jsp時,頁面上會展示一個超鏈接,點擊超鏈后,url中的地址就會發生跳轉,由“http://localhost:8080/springTest/index.jsp”跳轉到“http://localhost:8080/springTest/helloworld.do”,而這個url請求就會進入HelloWorld中的hello方法,因為其與該方法上的“/helloworld.do”匹配。 success.jsp(WEB-INF/views下) 該頁面是作為請求成功后的相應頁面 ``` <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h4>Success Page</h4> </body> </html> ``` ### **Controller方法的返回值** 可以有以下幾種: 1、返回ModelAndView 返回ModelAndView時最常見的一種返回結果。需要在方法結束的時候定義一個ModelAndView對象,并對Model和View分別進行設置。 ``` @Controller public class HelloWorldController { @RequestMapping("/helloworld.do") public String hello(){ System.out.println("hello world"); return "success"; } @RequestMapping("/abc.do") public ModelAndView abc(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("username","zhangsan"); modelAndView.setViewName("success"); return modelAndView; } } ``` ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${username} </body> </html> ``` 2、返回String 1):字符串代表邏輯視圖名 真實的訪問路徑=“前綴”+邏輯視圖名+“后綴” 注意:如果返回的String代表邏輯視圖名的話,那么Model的返回方式如下: ``` @RequestMapping("/helloworld.do") public String hello(Model model){ model.addAttribute("username","zhangsan"); System.out.println("hello world"); return "success"; } ``` 2):代表redirect重定向 redirect的特點和servlet一樣,使用redirect進行重定向那么地址欄中的URL會發生變化,同時不會攜帶上一次的request 案例: ``` @Controller public class HelloWorldController { @RequestMapping("/helloworld.do") public String hello(Model model){ model.addAttribute("username","zhangsan"); System.out.println("hello world"); return "success"; } @RequestMapping("/abc.do") public ModelAndView abc(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("username","zhangsan"); modelAndView.setViewName("success"); return modelAndView; } @RequestMapping("/redirect.do") public String redirect(){ return "redirect:login.do"; } } ``` 3):代表forward轉發 通過forward進行轉發,地址欄中的URL不會發生改變,同時會將上一次的request攜帶到寫一次請求中去 案例: ``` @Controller public class HelloWorldController { @RequestMapping("/helloworld.do") public String hello(Model model){ model.addAttribute("username","zhangsan"); System.out.println("hello world"); return "success"; } @RequestMapping("/abc.do") public ModelAndView abc(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("username","zhangsan"); modelAndView.setViewName("success"); return modelAndView; } @RequestMapping("/redirect.do") public String redirect(){ return "forward:login.do"; } } ``` 3、返回void 返回這種結果的時候可以在Controller方法的形參中定義HTTPServletRequest和HTTPServletResponse對象進行請求的接收和響應 1)使用request轉發頁面 request.getRequestDispatcher("轉發路徑").forward(request,response); 2)使用response進行頁面重定向 response.sendRedirect("重定向路徑"); 3)也可以使用response指定響應結果 response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().println("json串"); ``` @RequestMapping("/returnvoid.do") public void returnvoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request.getRequestDispatcher("轉發路徑").forward(request,response); // response.sendRedirect("重定向路徑"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().println("json串"); } ``` 以上三種返回值沒有什么重要和不重要的分別,一般來說都會使用到, 只不過有的時候使用的方式會有一些細微的差別 ### **SpringMVC的各種參數綁定方式** 1.基本數據類型(以int為例,其他類似): Controller代碼: ``` @RequestMapping("saysth.do") public void test(int count) { } ``` 表單代碼: ``` <form action="saysth.do" method="post"> <input name="count" value="10" type="text"/> </form> ``` 表單中input的name值和Controller的參數變量名保持一致,就能完成數據綁定,如果不一致可以使用@RequestParam注解。需要注意的是,如果Controller方法參數中定義的是基本數據類型,但是從頁面提交過來的數據為null的話,會出現數據轉換的異常。也就是必須保證表單傳遞過來的數據不能為null。所以,在開發過程中,對可能為空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的例子。 2.包裝類型(以Integer為例,其他類似): Controller代碼: ``` @RequestMapping("saysth.do") public void test(Integer count) { } ``` 表單代碼: ``` <form action="saysth.do" method="post"> <input name="count" value="10" type="text"/> </form> ``` 和基本數據類型基本一樣,不同之處在于,表單傳遞過來的數據可以為null,以上面代碼為例,如果表單中count為null或者表單中無count這個input,那么,Controller方法參數中的count值則為null。 3.自定義對象類型: Model代碼: ``` public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } ``` Controller代碼: ``` @RequestMapping("saysth.do") public void test(User user) { } ``` 表單代碼: ``` <form action="saysth.do" method="post"> <input name="firstName" value="張" type="text"/> <input name="lastName" value="三" type="text"/> ...... </form> ``` 非常簡單,只需將對象的屬性名和input的name值一一匹配即可。 ### **解決亂碼問題** 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> ``` ### RESTful架構 RESTful架構,就是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易于理解、擴展方便,所以正得到越來越多網站的采用。RESTful(即Representational State Transfer的縮寫)其實是一個開發理念,是對http的很好的詮釋。 對url進行規范,寫RESTful格式的url   非REST的url:http://...../queryItems.action?id=001   REST的url風格:http://..../items/001   特點:url簡潔,將參數通過url傳到服務端 修改web.xml,添加DispatcherServlet的Restful配置 ``` <servlet> <servlet-name>springmvc-servlet-rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc-servlet-rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` <url-pattern>/</url-pattern>表明所有url模式為/ ### URL 模板模式映射 @RequestMapping(value="/ viewItems/{id}") {×××}占位符,請求的URL可以是“/viewItems/1”或“/viewItems/2”,通過在方法中使用@PathVariable獲取{×××}中的×××變量。@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數上。 ``` @RequestMapping("/viewItems/{id}") public @ResponseBody viewItems(@PathVariable("id") String id) throws Exception{ } ```   如果RequestMapping中表示為"/viewItems/{id}",id和形參名稱一致,@PathVariable不用指定名稱。 ``` @RequestMapping("/viewItems/{id}") public @ResponseBody viewItems(@PathVariable String id) throws Exception{ } ``` #### 多個參數 ``` @Controller @RequestMapping("/person") public class PersonAction{ @RequestMapping(value="/delete/{id}/{name}") public String delete(@PathVariable Integer id,@PathVariable String name){ System.out.println(id + " " + name) ; return "person"; } } ``` ### 靜態資源訪問<mvc:resources>  如果在DispatcherServlet中設置url-pattern為 /則必須對靜態資源進行訪問處理,否則對css,js等文件的請求會被DispatcherServlet攔截。   spring mvc 的<mvc:resources mapping="" location="">實現對靜態資源進行映射訪問。告訴springmvc框架,描述的靜態資源,無須DispatcherServlet攔截,以及查詢的目錄。   如下是對css和js文件訪問配置: ``` <mvc:annotation-driven></mvc:annotation-driven> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> ``` ### 上傳圖片 在頁面form中提交enctype="multipart/form-data"的數據時,需要springmvc對multipart類型的數據進行解析。 在springmvc.xml中配置multipart類型解析器。 ``` <!-- 文件上傳 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="80000"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean> ``` 上傳圖片代碼 頁面 ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="upload.do" method="post" enctype="multipart/form-data"> <h2>文件上傳</h2> 文件:<input type="file" name="file1"/><br/><br/> 用戶名:<input type="text" name="username"> <br/><br/> 圖片:<img src="${imgpath}"/><br/><br/> <input type="submit" value="上傳"/> </form> </body> </html> ``` controller方法 ``` @Controller public class uploadController { @RequestMapping("/upload.do") public void doUpload(@RequestParam MultipartFile file1, HttpServletRequest request) throws IOException { String strName = request.getParameter("username"); System.out.println(strName); if(file1.isEmpty()) { System.out.println("文件未上傳!"); } else { //得到上傳的文件名 String fileName = file1.getOriginalFilename(); //得到服務器項目發布運行所在地址 String strFolder = request.getServletContext().getRealPath("/image")+ File.separator; File folder = new File(strFolder); if(!folder.exists()) { folder.mkdir(); } // 此處未使用UUID來生成唯一標識,用日期做為標識 String strNewFilePath = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ fileName; String strFinalPath = strFolder + strNewFilePath; //查看文件上傳路徑,方便查找 System.out.println(strFinalPath); //把文件上傳至path的路徑 File localFile = new File(strFinalPath); file1.transferTo(localFile); request.getSession().setAttribute("imgpath", "image"+ File.separator+strNewFilePath); } } } ``` ### 攔截器 攔截器是用來動態攔截 action 調用的對象。它提供了一種機制可以使開發者可以定義在一個 action 執行的前后執行的代碼,也可以在一個 action 執行前阻止其執行,同時也提供了一種可以提取 action 中可重用部分的方式 ![image.png](https://upload-images.jianshu.io/upload_images/468490-5cf009d12dcf778c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### HandlerInterceptor概述 在SpringMVC 中定義一個Interceptor是比較非常簡單,實現HandlerInterceptor接口。 HandlerInterceptor接口主要定義了三個方法: 1. boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handle)方法:該方法將在請求處理之前進行調用,只有該方法返回true,才會繼續執行后續的Interceptor和Controller,當返回值為true 時就會繼續調用下一個Interceptor的preHandle 方法,如果已經是最后一個Interceptor的時候就會是調用當前請求的Controller方法; 2.void postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView)方法:該方法將在請求處理之后,DispatcherServlet進行視圖返回渲染之前進行調用,可以在這個方法中對Controller 處理之后的ModelAndView 對象進行操作。 3.void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)方法:該方法也是需要當前對應的Interceptor的preHandle方法的返回值為true時才會執行,該方法將在整個請求結束之后,也就是在DispatcherServlet 渲染了對應的視圖之后執行。用于進行資源清理。 ### 簡單的一個例子: xml需要配置:兩種配置方式(對所有的請求記性攔截,對特定的請求進行攔截) ``` <mvc:interceptors> <!--對所有的請求記性攔截--> <!--<beans:bean class="com.sunp.common.interceptor.Myinterceptor"/>--> <!--對特定的請求進行攔截--> <mvc:interceptor> <mapping path="/kfc/brands/brand1/*"/> <beans:bean class="com.sunp.common.interceptor.Myinterceptor"/> </mvc:interceptor> </mvc:interceptors> ``` interceptors類 ``` public class Myinterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("preHandle run!"); return true; } public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle run!"); } public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion run!"); } } ``` ### 需求 1、用戶請求url 2、攔截器進行攔截校驗 如果請求的url是公開地址(無需登陸即可訪問的url),讓放行 如果用戶session 不存在跳轉到登陸頁面 如果用戶session存在放行,繼續操作。 <!--攔截器 --> ``` <mvc:interceptors> <!--如果配置了多個攔截器,則按順序執行 --> <!-- 登陸認證攔截器 --> <mvc:interceptor> <!-- /**表示所有url包括子url路徑 --> <mvc:mapping path="/**"/> <bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> ``` 定義攔截器,實現HandlerInterceptor接口,實現該接口提供了3個方法: ``` public class LoginInterceptor implements HandlerInterceptor{ //進入Handler方法之前執行 //用于身份認證,身份授權 //比如身份認證,如果認證通過表示當前用戶沒有登陸,需要此方法攔截不再往下執行 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //獲取請求的url String url = request.getRequestURI(); //判斷url是否是公開地址(實際使用時將公開地址配置配置文件中) //這里公開地址是登陸提交的地址 if(url.indexOf("login.action")>0){ return true; } //判斷session HttpSession session = request.getSession(); //從session中取出用戶身份信息 String username = (String) session.getAttribute("username"); if(username != null){ //身份存在,放行 return true; } //執行到這里時表示用戶身份需要認證,跳轉到登陸頁面 request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; } //進入Handler方法之后,返回modelAndView之前執行 //應用場景從modelAndView出發:將公用的模型數據(比如菜單導航)在這里傳到視圖,也可以在這里統一指定視圖 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub System.out.println(" LoginInterceptor..postHandle"); } //執行Handler完成后執行此方法 //應用場景:統一異常處理,統一日志處理 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub System.out.println("LoginInterceptor..afterCompletion"); } } ``` 登錄的controller方法 ``` @Controller public class LoginController { // 登陸 @RequestMapping("/login") public String login(HttpSession session, String username, String password) throws Exception { // 調用service進行用戶身份驗證 // 在session中保存用戶身份信息 session.setAttribute("username", username); // 重定向到商品列表頁面 return "redirect:/items/queryItems.action"; } // 退出 @RequestMapping("/logout") public String logout(HttpSession session) throws Exception { // 清除session session.invalidate(); // 重定向到商品列表頁面 return "redirect:/items/queryItems.action"; } } ``` > 攔截器無法攔截jsp
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看