<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 清晰的角色劃分 * 前端控制器(DispatcherServlet) * 請求到處理器映射(HandlerMapping) * 處理器適配器(HandlerAdapter) * 視圖解析器(ViewResolver) * 處理器或頁面控制器(Controller) * 驗證器( Validator) * 命令對象(Command 請求參數綁定到的對象就叫命令對象) * 表單對象(Form Object 提供給表單展示和提交到的對象就叫表單對象) # 和struct2區別 共同點: * 它們都是表現層框架,都是基于 MVC 模型編寫的。 * 它們的底層都離不開原始 ServletAPI。 它們處理請求的機制都是一個核心控制器。 區別: * Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter * Spring MVC 是基于方法設計的,而 Struts2 是基于類,Struts2 每次執行都會創建一個動作類。所 以 Spring MVC 會稍微比 Struts2 快些。 * Spring MVC 使用更加簡潔,同時還支持 JSR303, 處理 ajax 的請求更方便 * (JSR303 是一套 JavaBean 參數校驗的標準,它定義了很多常用的校驗注解,我們可以直接將這些注解加在我們 JavaBean 的屬性上面,就可以在需要校驗的時候進行校驗了。) * Struts2 的 OGNL 表達式使頁面的開發效率相比 Spring MVC 更高些,但執行效率并沒有比 JSTL 提升,尤其是 struts2 的表單標簽,遠沒有 html 執行效率高。 # 入門程序 ## 代碼 1. 啟動服務器,加載一些配置文件 * DispatcherServlet對象創建 * springmvc.xml被加載了 * HelloController創建成對象 2. 發送對象,后臺處理請求 ![](https://img.kancloud.cn/00/cb/00cbf948cefac9e0cfa2a91757296080_1447x302.png) **web.xml** ~~~ <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ~~~ **springmvc.xml** ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置創建 spring 容器要掃描的包 --> <context:component-scan base-package="com.jdxia.controller"/> <!-- 視圖解析器對象,幫我們跳轉頁面的 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 以后會找這個目錄下的文件 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 文件的后綴名是 --> <property name="suffix" value=".jsp"/> </bean> <!-- 開啟SpringMVC框架注解的支持 --> <mvc:annotation-driven/> </beans> ~~~ **控制器** ~~~ package com.jdxia.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(path = "/user") public class HelloController { @RequestMapping(path = "/hello", method = {RequestMethod.GET}) // public String sayHello() { System.out.println("hello sayHello"); //返回的會自動找對應的jsp文件 webapp/WEB-INF/pages/success.jsp return "success"; } } ~~~ ## 執行過程 ![](https://img.kancloud.cn/b2/a9/b2a9475fc55136cc6fd14ba70eb418b3_1160x647.png) # 防止中文亂碼 web.xml中 webapp下 ~~~ <!--配置解決中文亂碼的過濾器--> <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> ~~~ 在 springmvc 的配置文件中可以配置,靜態資源不過濾: ~~~ <!-- location表示路徑,mapping表示文件,**表示該目錄下的文件以及子目錄的文件 --> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/scripts/" mapping="/javascript/**"/> ~~~ get 請求方式: tomacat 對 GET 和 POST 請求處理方式是不同的,GET 請求的編碼問題,要改 tomcat 的 server.xml 配置文件,如下: ~~~ <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> ~~~ 改為: ~~~ <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/> ~~~ 如果遇到 ajax 請求仍然亂碼,修改 tomcat/conf/server.xml請把: `useBodyEncodingForURI="true"`改為 `URIEncoding="UTF-8"` 即可。 ~~~ <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8543" URIEncoding="UTF-8" /> ~~~ # 獲取原生servlet對象 SpringMVC 還支持使用原始 ServletAPI 對象作為控制器方法的參數。 支持原始 ServletAPI 對象有: * HttpServletRequest * HttpServletResponse * HttpSession * java.security.Principal * Locale * InputStream * OutputStream * Reader * Writer 我們可以把上述對象 ,直接寫在控制的方法參數中使用 ~~~ @RequestMapping(path = "/abc") public String sayAbc(HttpServletRequest request, HttpServletResponse response) { System.out.println("request: " + request); System.out.println("response: " + response); System.out.println("session: " + request.getSession()); System.out.println("servletContext: " + request.getSession().getServletContext()); return "success"; } ~~~ # 配置哪些資源不攔截 springmvc.xml下 ~~~ <!--前端控制器,哪些靜態資源不攔截--> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/js/" mapping="/js/**"/> ~~~
                  <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>

                              哎呀哎呀视频在线观看