posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          spring3.0 mvc和rest風格小例子

          Posted on 2010-07-03 19:25 penngo 閱讀(33859) 評論(21)  編輯  收藏 所屬分類: Java

          之前在新一個項目中用了spring3 的mvc開發,用得很爽,不過當時想找些入門的小例子時,找了好久也沒找到,

          現在寫個簡單的小例子出來給初學者學習下。
          srping3也支持rest,所以例子也包括這部分內容。
          先看web.xml配置
          <!-- 像js,css,gif等靜態文件,需要配置為默認的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配置為/,不帶文件后綴,會造成其它靜態文件(js,css等)不能訪問。如配為*.do,則不影響靜態文件的

          訪問 
          --> 
           
          </servlet-mapping>  

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

          的名稱,如果上邊為<servlet-name>mvc</servlet-name>則這個文件名為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"
          >  
                    
                 
          <!-- 自動掃描bean,把作了注解的類轉換為bean -->  
                
          <context:component-scan base-package="com.mvc.rest" />  
              
                 
          <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->  
                
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

           
               
                 
          <!-- 對模型視圖名稱的解析,在請求時模型視圖名稱添加前后綴 -->  
                 
          <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類
          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";
                  }
            
          }

          兩個jsp頁面
          \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>

          運行效果
          訪問http://localhost:8089/mvc/welcome

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

          訪問http://localhost:8089/mvc/login/pengo

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

          附件:源碼

          評論

          # re: spring3.0 mvc和rest小例子[未登錄]  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

          2010-07-05 18:33 by Jie
          留個記號

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

          2010-08-24 16:11 by zzl
          @jsparrow
          無語了。


          LZ只是在做一個簡單的示例而已。跟地址欄輸入用戶名密碼又什么關系啊。

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          呵呵,只是做個rest的例子,并沒有跟用戶名密碼有關,不知你是怎理解到登陸的。

          # re: spring3.0 mvc和rest小例子[未登錄]  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

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

          感謝這位仁兄~~~

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

          2012-03-09 14:04 by bowen.xiao
          內容很簡單,入門的好例子,謝謝你的分享。
          入門經典,一次編譯通過

          # re: spring3.0 mvc和rest風格小例子[未登錄]  回復  更多評論   

          2012-04-07 21:54 by aaa
          你好,我想問一下為什么我運行不了這個例子呢?

          # re: spring3.0 mvc和rest風格小例子[未登錄]  回復  更多評論   

          2012-04-07 22:09 by aaa
          顯示的是這個錯誤??!求樓主及各位高手指教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風格小例子  回復  更多評論   

          2013-02-26 18:27 by 技術人生
          我也運行不了

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest風格小例子[未登錄]  回復  更多評論   

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

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

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

          # re: spring3.0 mvc和rest風格小例子  回復  更多評論   

          2014-10-30 15:45 by 扎根三
          毛線的源碼,發布了的,懶得反編譯,沒意思??!

          # re: spring3.0 mvc和rest風格小例子[未登錄]  回復  更多評論   

          2015-04-27 22:35 by spring
          @pengo
          學習了,謝謝!
          主站蜘蛛池模板: 虞城县| 工布江达县| 东乌珠穆沁旗| 怀柔区| 扎赉特旗| 崇阳县| 吉木乃县| 晋中市| 龙海市| 桦川县| 泗洪县| 航空| 凯里市| 招远市| 巴林左旗| 博乐市| 巴塘县| 钟祥市| 固阳县| 潼关县| 永定县| 广水市| 司法| 白城市| 定边县| 安新县| 莒南县| 青海省| 公主岭市| 武穴市| 修文县| 皮山县| 景宁| 博乐市| 措美县| 江北区| 吉首市| 恩施市| 昆山市| 洛川县| 台山市|