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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 介紹 SpringMVC是一款Web MVC框架。 它跟Struts框架類似,是目前主流的Web MVC框架之一。 本章通過實例來介紹SpringMVC的入門知識。 # 實例 本章所寫的實例是一個員工的CRUD demo。 用idea編寫,基于maven, Web框架使用SpringMVC,視圖采取Freemarker技術,數據庫使用MySQL,用Hibernate4存儲數據。 項目文件目錄如下: ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro01.jpg) 首先在web.xml中配置入口servlet: <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springConfig/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 注意有個初始參數 contextConfigLocation, 顧名思義 上下文配置文件路徑。 **還有另外一個注意點,這個servlet對應的url-pattern最好寫成 "/", 不要寫成 "/*"。** 至于為什么需要寫成 "/" , 具體請參考[http://www.cnblogs.com/fangjian0423/p/servletContainer-tomcat-urlPattern.html#springmvc](http://www.cnblogs.com/fangjian0423/p/servletContainer-tomcat-urlPattern.html#springmvc) 在dispatcher-servlet.xml中主要是配置springmvc的一些Controller的初始化,靜態文件的映射策略,視圖的配置等。 然后開始編寫Controller類(類似Struts2中的Action) 首先來看一個主頁的Controller: @Controller @RequestMapping("/") public class IndexController { @RequestMapping public ModelAndView index() { ModelAndView view = new ModelAndView("index"); view.addObject("welcome", "hello"); return view; } } 這里有2個注解: @Controller和@RequestMapping @Controller注解就是表明這是一個Controller,且會被spring容器進行初始化。 dispatcher-servlet.xml中的掃描包配置語句: <context:component-scan base-package="org.format.demo.controller" /> 這條語句是掃描org.format.demo.controller下被@Controller(還有其他的如 @Component, @Service, @Repository)注解的那些類,并進行實例化。 @RequestMapping 顧名思義,就是請求映射。 我們看到@RequestMapping("/")中的"/"的意義就是contextPath后面的路徑;也就是 http://host:port/contextPath 后面的路徑。 **(這里不一定都要以"/"開頭,比如 "/employee", 我們可以寫成 "employee" )** ModelAndView對象就是一個帶模型的視圖對象。 我們看到IndexController返回了1個index名稱的ModelAndView對象。 addObject對象就類似HttpServletRequest的setAttribute對象,也就是視圖里面丟數據。 我們看到丟了一個key為welcome的對象。 最后的視圖代碼: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <h2>Welcome to user SpringMVC</h2> <h3>your welcome param: ${welcome}</h3> </body> </html> 我們看到用了el表達式得到了丟入的數據welcome,也就是hello。 因此,最終生成的頁面及地址如下: ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro02.jpg) 最后SpringMVC會通過配置文件中的視圖對象拼接成最終的視圖地址。 ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro03.jpg) 圖中viewName也就是返回的ModelAndView中的viewName 即index。 因此,最終的視圖路徑為 /WEB-INF/view/index.ftl。 下面在來看一個員工操作的Controller代碼: @Controller @RequestMapping(value = "/employee") public class EmployeeController { @Autowired private IEmployeeService employeeService; @Autowired private IDeptService deptService; @RequestMapping public ModelAndView index() { ModelAndView view = new ModelAndView("employee/list"); List<Employee> employees = employeeService.list(); view.addObject("list", employees); return view; } @RequestMapping(method = RequestMethod.POST, value = "/delete/{employeeId}") @ResponseBody public String delete(@PathVariable Integer employeeId) { employeeService.delete(employeeId); return "success"; } @RequestMapping(method = RequestMethod.GET, value = "/add") public ModelAndView add(ModelAndView view) { view.setViewName("employee/form"); view.addObject("depts", deptService.listAll()); return view; } @RequestMapping(method = RequestMethod.GET, value = "/detail/{employeeId}") public ModelAndView detail(@PathVariable Integer employeeId, ModelAndView view) { view.setViewName("employee/form"); view.addObject("employee", employeeService.getById(employeeId)); view.addObject("depts", deptService.listAll()); return view; } @RequestMapping(method = RequestMethod.POST, value = "/update") public String add(Employee employee) { if(employee.getDept().getId() == null) { employee.setDept(null); } employeeService.saveOrUpdate(employee); return "redirect:/employee/"; } } 這里多了幾個新的內容: 1.@RequestMapping注解的作用位置   @RequestMapping可以作用在類名上,也可以作用在方法上。 如果都有, 產生作用的路徑是類名上的路徑+方法上的路徑。 比如EmployeeController的add方法,最終起作用的路徑是 http://host:port/contextPath/employee/add 2.@RequestMapping注解的method參數   method參數表示的HTTP請求的方式。常見的有GET,PUT,POST,DELETE等。若請求的方法與后臺編寫的方法不一致,會出現HTTP 405錯誤。 3.@PathVariable注解   這是一種基于RESTFUL的注解。我們看到detail方法的@RequestMapping的value值/detail/{employeeId},參數中加入了一個@PathVariable employeeId。 這樣起作用的路徑就根據employee的Id,即每個員工都有獨立的一個URI路徑資源。 符合RESTFUL架構。 4.Controller的方法參數   Controller的方法訪問非常靈活。 比如Employee有id,name,age等屬性。 只要我們在前臺傳入name為id,name,age這3個參數,并且接受的方法有一個Employee對象參數,SpringMVC會自動把3個注入到這個對象中。 還有其他一些Integer,Long參數等,SpringMVC會默認幫我們自動轉化。 同時參數也可以丟入一些HttpServletRequest, HttpServletResponse, HttpServletSession對象,SpringMVC會自動幫我們注入。 這點非常方便。 5.不帶參數基于方法的@RequestMapping會被當然基于類的@RequestMapping所作用的地址的默認進入的方法 ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro04.jpg) add方法。 get請求 ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro05.jpg) detail方法。 RESTFUL風格 ![](http://7x2wh6.com1.z0.glb.clouddn.com/springmvc-intro06.jpg) delete方法。RESTFUL風格 # 總結 SpringMVC是一個Web MVC框架。 它的特點是輕便,與Spring無縫整合,上手簡單。它的易用性、可擴展性、安全性均非常理想。 # 參考資料 [http://www.ruanyifeng.com/blog/2011/09/restful.html](http://www.ruanyifeng.com/blog/2011/09/restful.html) [http://jinnianshilongnian.iteye.com/blog/1593441](http://jinnianshilongnian.iteye.com/blog/1593441)
                  <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>

                              哎呀哎呀视频在线观看