溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

SSM框架實(shí)現(xiàn)前后端信息交互的實(shí)現(xiàn)代碼詳情

發(fā)布時(shí)間:2020-07-17 17:46:34 來(lái)源:億速云 閱讀:393 作者:小豬 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了SSM框架實(shí)現(xiàn)前后端信息交互的實(shí)現(xiàn)代碼詳情,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

一、從前端向后端傳送數(shù)據(jù)

常見(jiàn)的3種方式

1、form表單的action:此方法可以提交form表單內(nèi)的輸入數(shù)據(jù),也可同時(shí)提交某些隱藏但設(shè)置有默認(rèn)值的<input>,如修改問(wèn)題時(shí),我們除了提交問(wèn)題的相關(guān)信息,還需要將用戶(hù)的編號(hào)提交給后端,此時(shí)就可以設(shè)置一個(gè)默認(rèn)值為用戶(hù)編號(hào)的<input>,并將其隱藏

2、<a>標(biāo)簽的href屬性:此方法一般用來(lái)提交一些較少的數(shù)據(jù),比如對(duì)象編號(hào)

1 <a href="<%=path%>/Question/DisplayQuestionInfo&#63;question_id=${question.question_id}" rel="external nofollow" >${question.question_title}</a>
比如該處代碼,顯示了問(wèn)題的標(biāo)題信息,并將其作為超鏈接,點(diǎn)擊該鏈接時(shí)進(jìn)入后端Controller類(lèi)的方法,并向其發(fā)送問(wèn)題編號(hào)question_id

3、ajax請(qǐng)求:此方法一般在不需要頁(yè)面跳轉(zhuǎn)時(shí)采用,可以局部刷新頁(yè)面,比如向后端提交關(guān)注某用戶(hù)的信息,后端收到ajax的請(qǐng)求數(shù)據(jù),對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,并通過(guò)@Response注解返回信息給前端,然后前端進(jìn)行相關(guān)操作,可以不進(jìn)行頁(yè)面跳轉(zhuǎn)

前端部分代碼

<script language="JavaScript">
  ......
      function SaveUserFollowUser(){
      var login_user_id = ${login_user_id}    //登錄者(發(fā)起者)編號(hào)
      var user_id = ${user.user_id};       //接受者用戶(hù)編號(hào)

      $.ajax({
        url:"<%=path%>/UserRelation/SaveUserFollowUser",
        type:"POST",
        async: false,
        contentType:"application/json;charset=UTF-8",
        dataType:'json',

        data:JSON.stringify({"from_user_id":login_user_id,"to_user_id":user_id}), //JSON對(duì)象轉(zhuǎn)為字符串
        success:function(data){
          /* 可在后端增加判斷發(fā)起者和接受者用戶(hù)是否是同一用戶(hù)的判斷 */
          if (data == true) {
            alert("關(guān)注成功");
          } else {
            alert("您已經(jīng)關(guān)注該用戶(hù),不可重復(fù)關(guān)注")
          }
        }
      });
    }
</script>

......
      <button class="btn btn-success"  onclick="SaveUserFollowUser()">關(guān)注用戶(hù)</button>
......

后端Controller類(lèi)

/**
 * 表現(xiàn)層 用戶(hù)關(guān)系相關(guān) (關(guān)注用戶(hù)、被用戶(hù)關(guān)注、關(guān)注問(wèn)題、贊同回答)
 */
@Controller
@RequestMapping("/UserRelation")
public class UserRelationController {

  ......

   /**
   * 新增某用戶(hù)關(guān)注某用戶(hù)
   * @param map
   * @return
   */
  @RequestMapping(value = "/SaveUserFollowUser",method = {RequestMethod.POST})
  public @ResponseBody Boolean SaveUserFollowUser(@RequestBody Map<String,String> map) {

    //關(guān)注發(fā)出者編號(hào)
    Integer from_user_id = Integer.parseInt(map.get("from_user_id"));
    //關(guān)注接受者編號(hào)
    Integer to_user_id = Integer.parseInt(map.get("to_user_id"));
    //是否新增成功
    //該項(xiàng)可以增加發(fā)起者用戶(hù)和接受者用戶(hù)是否是同一用戶(hù)的判斷,即比較from_user_id與to_user_id是否相等,如果相等則關(guān)注失敗
    //通過(guò)返回Integer類(lèi)型而非Boolean類(lèi)型的做判斷 本程序并未增加這項(xiàng)判斷
    Boolean flag = userRelationService.saveUserFollowUser(from_user_id,to_user_id);
    return flag;
  }
  ......
}

二、從后端向前端傳送數(shù)據(jù)

1、Model

后端部分代碼

/**
 * 表現(xiàn)層 用戶(hù)
 */
@Controller
@RequestMapping(value = "/User")
public class UserController {

  ......

  /**
   * 進(jìn)入個(gè)人信息頁(yè)面
   * @param httpSession
   * @param model
   * @return
   */
  @RequestMapping(value = "/DisplayMyInfo")
  public String DisplayMyInfo(HttpSession httpSession, Model model) {
    Integer user_id = (Integer) httpSession.getAttribute("login_user_id");  //登錄者個(gè)人編號(hào)
    User user = userService.findUserById(user_id); //登錄者個(gè)人信息

    model.addAttribute("user",user);       //將登錄者個(gè)人信息返回給前端
    return "User/myInfo";
  }
  ......
}

前端部分代碼

......
      <div class="col-md-6 col-md-offset-5" >
        <h3>用戶(hù)名:${user.user_name}</h3>
        <h3>用戶(hù)昵稱(chēng):${user.user_nickname}</h3>
        <h3>用戶(hù)性別:${user.user_sex}</h3>
        <h3>用戶(hù)郵箱:${user.user_email}</h3>
        <h3>用戶(hù)密碼:${user.user_password}</h3>
      </div>
......

此時(shí)可以通過(guò)${}直接取得后端傳來(lái)的數(shù)據(jù)

2、ModelAndView

該方法與Model相比,多增加了返回的視圖(View),對(duì)于返回給前端的具體數(shù)據(jù)處理類(lèi)似

看完上述內(nèi)容,是不是對(duì)SSM框架實(shí)現(xiàn)前后端信息交互的實(shí)現(xiàn)代碼詳情有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI