一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

spring MVC筆記3

 ruodream 2016-02-01

一、 簡單環(huán)境的搭建:

          1、創(chuàng)建項目并加入jar

               

          2、配置web.xml

                <servlet>

                        <servlet-name>springMVC</servlet-name>

                        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

                        <init-param>

                                <param-name>contextConfigLocation</param-name>

                                <param-value>classpath:spring_mvc.xml</param-value>

                        </init-param>

                        <load-on-startup>1</load-on-startup>

                </servlet>

                <servlet-mapping>

                      <servlet-name>springMVC</servlet-name>

                      <url-pattern>/</url-pattern>

                </servlet-mapping>

        

         3、 配置spring_mvc.mxl(啟動掃描機制和配置視圖解析器)

                <beans xmlns="http://www./schema/beans"

                              xmlns:xsi="http://www./2001/XMLSchema-instance"

                              xmlns:context="http://www./schema/context"

                              xmlns:mvc="http://www./schema/mvc"

                              xsi:schemaLocation="http://www./schema/beans

                              http://www./schema/beans/spring-beans-3.2.xsd

                              http://www./schema/context

                              http://www./schema/context/spring-context-3.2.xsd

           B"> 

二、參數(shù)傳遞

        1、直接傳參:

              這種方法很簡單,就像上面搭建環(huán)境時,在login()里面添加name、password兩參數(shù),只需頁面?zhèn)魅雲(yún)?shù)時有對應(yīng)的參數(shù)名時,他

              就會自動接收到對于的參數(shù)。

              簡單的例子:

              添加一登陸頁面login.jsp和一相應(yīng)頁面helloWorld.jsp

              login.jsp

              <body>

                     <form action="<%=path%>/springController/login">

                           名字<input type="text" name="name"><br>

                           密碼<input type="password" name="password"><br>

                           <input type="submit" value="提交">

                     </form>

              </body>

              helloWorld.jsp

              <body>

                    ${name}<br>

                    ${password}<br>

                    傳參成功

              </body>

              此時啟動服務(wù)器,輸入http://localhost:8080/springMVC/login.jsp  在login.jsp頁面中輸入名字和密碼,點擊登陸時,在控制臺打印出了名

              字和密碼,同時跳helloWorld.jsp頁面輸出相應(yīng)的信息。

              

        2、實體傳參

              這個方法是將實體直接作為參數(shù)接收的,比如是loginUser user),但這里,User實體必須有與參入值名字相同的屬性。比如建一

              實體類user

              public  class User {

                     private String name;

                     private String password;

                     public String getName() {

                                  return name;

                     }

                     public void setName(String name) {

                                  this.name = name;

                     }

                     public String getPassword() {

                                  return  password;

                     }

                     public void setPassword(String password) {

                                  this.password = password;

                     }

              }

              然后同上面說的,將User做為login()的參數(shù)即可

             

        3、json前后臺傳參

              Json傳參,其實就是把數(shù)據(jù)組裝成json格式,然后傳到后臺,這里修改login.jsp

              <script type = "text/javascript"  src = "<%=path %>/script/jquery/src/jquery-1.4.2.min.js"></script>

              <script type="text/javascript">

                           $(document).ready(function(){

                                 $("#onclick").click(function(){

                                         var name = $("#name").val();

                                         var password = $("#password").val();

                                         var user={name:name ,password:password};               

                                         $.ajax({

                                                     url:"/springMVC/springController/login",       

                                                     data:user,

                                                     async:false,

                                                     success:function(data){

                                                            alert(data.name);

                                                     }                  

                                         });

                                  });    

                           });

              </script>

               

              <body>

                   <form>

                       名字<input type="text" name="name" id="name"><br>

                       密碼<input type="password" name="password" id="password"><br>

                       <button id="onclick">提交</button>

                   </form>

              </body>

              這里需要注意的是引進jquery庫,用ajax實現(xiàn)數(shù)據(jù)的傳遞。而引人jquery是需在spring_mvc.xml里添加<mvc:resources  location="/script/"

              mapping="/script/**"/>這句表明了引進的jquery不需要經(jīng)過前端控制器,location指明不去經(jīng)過前端控制器的包,mapping,指明具體哪些

              文件。 var user={name:name ,password:password};就是將數(shù)據(jù)轉(zhuǎn)成json格式。

              而后臺接受參數(shù)不變,后臺SpringController代碼

              @Component

              @RequestMapping(value="/springController")

               publicclass SpringController extends MultiActionController{

                       @RequestMapping(value="/login")

                       publicvoid login(User user, HttpServletRequest request, HttpServletResponse response){

                             //組裝json(還有其他的方法)     

                             String result="{\"name\":\""+user.getName()+"\",\"password\":\""+user.getPassword()+"\"}";

                             //指明輸出json格式

                             response.setContentType("application/json");

                             try {

                                    PrintWriter out=response.getWriter();

                                    out.print(result);

                             } catch (IOException e) {

                                    e.printStackTrace();

                             }

                       }

               }

               這樣子,開啟服務(wù)器,打開login.jsp頁面輸入用戶名和密碼,點擊登錄后就能會進入后臺,并在控制臺輸出用戶名和密碼。然后轉(zhuǎn)

               到login.jsp頁面,并跳出一會話框,輸出用戶名和密碼。

 

三、中文亂碼

        上面的例子,當輸入中文的時候,都會出現(xiàn)中文亂碼,解決方案,首先在web.xml中添加spring提供的中文亂碼過濾器

        <filter>

             <filter-name>encodingFilter</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>encodingFilter</filter-name>

              <url-pattern>/*</url-pattern>

        </filter-mapping>

        這個過濾器要放在前端控制器之前,然后對要傳入的參數(shù)進行壓碼就如login.jsp頁面var name = encodeURI($("#name").val());name進行壓

        碼,接著在后臺對name進行解碼URLDecoder.decode(user.getName(),"UTF-8")。此時,輸入中文,就不會出現(xiàn)亂碼問題了

 

 

 

 

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    欧美日韩国产一级91| 久久国产精品熟女一区二区三区 | 亚洲国产av一二三区| 日本东京热加勒比一区二区| 欧洲一区二区三区自拍天堂| 国产综合香蕉五月婷在线| 久久这里只有精品中文字幕| 日韩精品一区二区三区四区| 日韩欧美在线看一卡一卡| 91精品视频免费播放| 色好吊视频这里只有精| 人妻精品一区二区三区视频免精| 日韩国产中文在线视频| 日韩精品在线观看完整版| 麻豆精品在线一区二区三区| 亚洲综合色在线视频香蕉视频 | 在线亚洲成人中文字幕高清| 天堂网中文字幕在线视频| 一区二区福利在线视频| 高清一区二区三区不卡免费| 99久久免费看国产精品| 日本东京热视频一区二区三区| 日韩中文字幕狠狠人妻| 91爽人人爽人人插人人爽| 人妻人妻人人妻人人澡| 一区二区三区日韩在线| 国产成人精品国内自产拍| 极品少妇一区二区三区精品视频| 日韩丝袜诱惑一区二区| 激情五月天深爱丁香婷婷| 狠狠干狠狠操在线播放| 日本理论片午夜在线观看| 99精品人妻少妇一区二区人人妻| 黑鬼糟蹋少妇资源在线观看| 一区二区日本一区二区欧美| 亚洲夫妻性生活免费视频| 日本一区二区三区黄色| 亚洲三级视频在线观看免费| 午夜色午夜视频之日本| 精品一区二区三区免费看| 熟女少妇久久一区二区三区|