1. 程式人生 > >java前後臺之間傳值的幾種方式

java前後臺之間傳值的幾種方式

原文地址:http://blog.sina.com.cn/s/blog_13066bce70102wi1w.html           作者:EasonWang

自己寫的程式碼太少,有時候前後臺傳值還寫的不是很熟練,現在總結一下,加深下印象。

1.jquery的Ajax傳值

----》前臺到後臺

期望功能:把前臺使用者輸入的資訊儲存在資料庫裡。

前臺jsp程式碼:

function saveUserInfo(){

 var params = {};//定義一個數組


  var userNO = jq("#userNO").val();//把前臺頁面上的值都賦予一個變數
  var userName = jq("#userName").val();
  var phoneNumber = jq("#phoneNumber").val();

  params["userNO"] = userNO ;//把這些變數都存在這個數組裡
  params["userName"] = userName;
  params["phoneNumber"] = phoneNumber;

  jq.ajax({
      type : "post",
        url : "<%=basePath%>personalSetting/personalSettingSaveAll.do",//找到action裡相應的方法
        data : params, //把陣列的值賦給data,把上面的param改為data,這裡就能直接去掉
        dataType : "text",//根據資料型別選擇text、jason等
        success : function(data) { 
          alert(msg); //msg可以在後臺Action配置,也可以在這裡直接寫提示資訊
        },
       error : function(data) {
        alert("msg");
       }
      });
       } 
 }

後臺Action程式碼:

@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
@RequestMapping(value ="/personalSetting/personalSettingSaveAll.do", method = RequestMethod.POST)
public void saveAll(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
   response.setCharacterEncoding("UTF-8");
   HttpSession session = request.getSession();
    String userNO = request.getParameter("userNO")+"";
   String userName = request.getParameter("userName")+"";
    String phoneNumber = request.getParameter("phoneNumber")+"";     
    PersonalSettingEntity pSE = new PersonalSettingEntity(); 
    if(!("".equals(userNO))||"".equals(userName )||"".equals(phoneNumber ))){ //如果都不為空  

       pSE.setUserNO(userNO);
       pSE.setUserName(userName);
       pSE.setPhoneNumber(phoneNumber);
       PersonSettingService.saveProcessSetting(pSE);
       msg = "儲存成功";
   }else{
       msg = "填寫不完整"; //可以在前臺校驗,這裡為了舉例子
   }
       response.getWriter().print(msg); //這裡的msg是text資料,所以前臺資料型別是text。
}

----》後臺到前臺

期望功能:把資料庫裡的使用者資訊顯示到前臺頁面。

後臺Action程式碼:

@SuppressWarnings({ "unchecked", "unused", "rawtypes" })
@RequestMapping(value = "/personalSetting/personalSettingInitAll.do", method = RequestMethod.POST)
public void qureyAll(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
   response.setCharacterEncoding("UTF-8");
   HttpSession session = request.getSession();
    String userId = request.getSession().getAttribute("currentUserId").toString();
   PersonalSettingEntity pSE = PersonSettingService.getPersonalSetting(Long.valueOf(userId));    
    response.getWriter().print(JSONArray.fromObject(pSE).toString());
}

前臺jsp程式碼:

function get(){ 
 jq.ajax({
        type : "post",
        url : "<%=basePath%>personalSetting/personalSettingInitAll.do",
        dataType : "json",
        success :function (data) {
         var bean;
         jq.each(data,function(index){
          bean = data[index];
          jq("#userNO").attr("value",bean.userNO);
          jq("#userName").attr("value",bean.userName); 
          jq("#phoneNumber").attr("value",bean.phoneNumber);
           }                  
        },
        error :function (data){
          alert("系統出錯!");
        }
  });  
 } 

另外一個傳值的部落格地址:http://www.cnblogs.com/Benjamin/p/3414449.html