posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          spring3.0 mvc和rest風(fēng)格小例子

          Posted on 2010-07-03 19:25 penngo 閱讀(33867) 評(píng)論(21)  編輯  收藏 所屬分類(lèi): Java

          之前在新一個(gè)項(xiàng)目中用了spring3 的mvc開(kāi)發(fā),用得很爽,不過(guò)當(dāng)時(shí)想找些入門(mén)的小例子時(shí),找了好久也沒(méi)找到,

          現(xiàn)在寫(xiě)個(gè)簡(jiǎn)單的小例子出來(lái)給初學(xué)者學(xué)習(xí)下。
          srping3也支持rest,所以例子也包括這部分內(nèi)容。
          先看web.xml配置
          <!-- 像js,css,gif等靜態(tài)文件,需要配置為默認(rèn)的servlet -->

           <servlet-mapping>  
               
          <servlet-name>default</servlet-name>  
               
          <url-pattern>*.js</url-pattern>  
           
          </servlet-mapping> 
             
          <servlet-mapping>  
               
          <servlet-name>default</servlet-name>  
               
          <url-pattern>*.css</url-pattern>  
           
          </servlet-mapping>  

           
          <servlet>  
               
          <servlet-name>spring</servlet-name>  
               
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
               
          <load-on-startup>1</load-on-startup>  
           
          </servlet>  
           
          <servlet-mapping>  
               
          <servlet-name>spring</servlet-name>  
               
          <url-pattern>/</url-pattern> 
          <!-- url配置為/,不帶文件后綴,會(huì)造成其它靜態(tài)文件(js,css等)不能訪問(wèn)。如配為*.do,則不影響靜態(tài)文件的

          訪問(wèn) 
          --> 
           
          </servlet-mapping>  

          spring-servlet.xml文件配置,注:XXXX-servlet.xml的XXXX就是上邊<servlet-name>spring</servlet-name>中

          的名稱(chēng),如果上邊為<servlet-name>mvc</servlet-name>則這個(gè)文件名為mvc-servelt.xml。

          <beans xmlns="http://www.springframework.org/schema/beans"  
                 xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 

          xmlns:p
          ="http://www.springframework.org/schema/p"  
                  xmlns:context
          ="http://www.springframework.org/schema/context"  
                  xsi:schemaLocation
          ="http://www.springframework.org/schema/beans   
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                     http://www.springframework.org/schema/context   
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd"
          >  
                    
                 
          <!-- 自動(dòng)掃描bean,把作了注解的類(lèi)轉(zhuǎn)換為bean -->  
                
          <context:component-scan base-package="com.mvc.rest" />  
              
                 
          <!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 -->  
                
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

           
               
                 
          <!-- 對(duì)模型視圖名稱(chēng)的解析,在請(qǐng)求時(shí)模型視圖名稱(chēng)添加前后綴 -->  
                 
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
                    p:prefix
          ="/WEB-INF/view/" p:suffix=".jsp" />  
                  
                 
          <bean id="multipartResolver"  
                    class
          ="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
                    p:defaultEncoding
          ="utf-8" />  
                     
            
          </beans>  

          controller類(lèi)
          package com.mvc.rest;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.springframework.stereotype.Controller;
          import org.springframework.ui.ModelMap;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;
          import org.springframework.web.servlet.ModelAndView;

          @Controller
          public class RestController {
              
              
          public RestController(){
                  
              }

              
              @RequestMapping(value 
          = "/login/{user}", method = RequestMethod.GET)   
              
          public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,     
                      @PathVariable(
          "user") String user, ModelMap modelMap) throws Exception {  
                  modelMap.put(
          "loginUser", user);
                  
          return new ModelAndView("/login/hello", modelMap);
              }
             
              
               @RequestMapping(value 
          = "/welcome", method = RequestMethod.GET)  
                  
          public String registPost() {  
                   
          return "/welcome";
                  }
            
          }

          兩個(gè)jsp頁(yè)面
          \WEB-INF\viewwelcome.jsp
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=GBK">
          <title>Insert title here</title>
          </head>
          <body>
          歡迎
          </body>
          </html>

          \WEB-INF\view\login\hello.jsp
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=GBK">
          <title>Insert title here</title>
          </head>
          <body>
          你好:<%=request.getAttribute("loginUser") %>
          </body>
          </html>

          運(yùn)行效果
          訪問(wèn)http://localhost:8089/mvc/welcome

          http://www.aygfsteel.com/pengo/

          訪問(wèn)http://localhost:8089/mvc/login/pengo

          http://www.aygfsteel.com/pengo/

          附件:源碼

          評(píng)論

          # re: spring3.0 mvc和rest小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2010-07-05 11:07 by lveyo
          在controller映射方式上像struts2了。

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-07-05 18:33 by Jie
          留個(gè)記號(hào)

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-07-11 18:19 by yaoneng
          期待交流
          這是我的Q:1125528760
          謝謝!!

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-07-17 13:49 by 嚴(yán)維杰
          大俠能否告知用“/“和”*.html"的區(qū)別哦,用“/"有什么好處。qq:361273693

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-08-24 10:20 by jsparrow
          這算是什么登陸???讓每個(gè)登陸的人在地址欄輸入用戶(hù)名密碼????????

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-08-24 16:11 by zzl
          @jsparrow
          無(wú)語(yǔ)了。


          LZ只是在做一個(gè)簡(jiǎn)單的示例而已。跟地址欄輸入用戶(hù)名密碼又什么關(guān)系啊。

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-08-24 22:16 by pengo
          @jsparrow

          呵呵,只是做個(gè)rest的例子,并沒(méi)有跟用戶(hù)名密碼有關(guān),不知你是怎理解到登陸的。

          # re: spring3.0 mvc和rest小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2010-09-07 22:28 by 劉俊杰
          歡迎加入“16023628技術(shù)群

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2010-12-13 12:33 by pandora jewelry
          其中hsqldb和hibernate都是從jbpm4.4的lib文件夾里面拷過(guò)去的

          # re: spring3.0 mvc和rest小例子  回復(fù)  更多評(píng)論   

          2011-02-15 10:18 by Chaos
          你這根本不是REST

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2011-12-31 17:33 by YangJie
          困擾我一下午的問(wèn)題終于解決了。

          感謝這位仁兄~~~

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2012-03-09 14:04 by bowen.xiao
          內(nèi)容很簡(jiǎn)單,入門(mén)的好例子,謝謝你的分享。
          入門(mén)經(jīng)典,一次編譯通過(guò)

          # re: spring3.0 mvc和rest風(fēng)格小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2012-04-07 21:54 by aaa
          你好,我想問(wèn)一下為什么我運(yùn)行不了這個(gè)例子呢?

          # re: spring3.0 mvc和rest風(fēng)格小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2012-04-07 22:09 by aaa
          顯示的是這個(gè)錯(cuò)誤!!求樓主及各位高手指教2012-4-7 22:08:43 org.springframework.web.servlet.DispatcherServlet noHandlerFound
          警告: No mapping found for HTTP request with URI [/Spring2012040704/welcome] in DispatcherServlet with name 'spring'

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2013-02-26 18:27 by 技術(shù)人生
          我也運(yùn)行不了

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2013-03-08 19:51 by Iamlonely
          @aaa
          base-package="com.mvc.rest"
          這里的package要是自己的package name

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2014-03-03 11:06 by Jove
          傻叉啊 。。你知道什么是REST么、。。

          # re: spring3.0 mvc和rest風(fēng)格小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2014-03-07 18:58 by spring
          @Jove
          你真有病,博主寫(xiě)的是對(duì)的,spring官方文檔就是這樣寫(xiě)法。自己不懂,就別裝b

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2014-05-17 17:40 by zuidaima
          最代碼轉(zhuǎn)載地址:
          spring3.0 mvc和rest風(fēng)格的小例子配置demo代碼教程http://www.zuidaima.com/share/1826552160996352.htm

          # re: spring3.0 mvc和rest風(fēng)格小例子  回復(fù)  更多評(píng)論   

          2014-10-30 15:45 by 扎根三
          毛線的源碼,發(fā)布了的,懶得反編譯,沒(méi)意思!!

          # re: spring3.0 mvc和rest風(fēng)格小例子[未登錄](méi)  回復(fù)  更多評(píng)論   

          2015-04-27 22:35 by spring
          @pengo
          學(xué)習(xí)了,謝謝!
          主站蜘蛛池模板: 六盘水市| 建水县| 兴国县| 南皮县| 库车县| 霞浦县| 墨玉县| 武川县| 高密市| 龙门县| 玉门市| 辽阳市| 麻城市| 宜都市| 蒲江县| 渝北区| 巨鹿县| 吴旗县| 缙云县| 包头市| 时尚| 鄂尔多斯市| 隆德县| 嘉峪关市| 新乐市| 皋兰县| 义马市| 电白县| 武清区| 汕尾市| 鄯善县| 玛沁县| 运城市| 花垣县| 弥渡县| 徐水县| 辉南县| 凤山县| 剑河县| 高雄市| 静海县|