• java前后台之间传值的几种方式


    自己写的代码太少,有时候前后台传值还写的不是很熟练,现在总结一下,加深下印象。

    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("系统出错!");
             }
       });  
     } 

  • 相关阅读:
    【C3】04 工作原理
    【C3】03 如何构建
    【C3】02 操作总览
    【C3】01 概述
    【H5】16 表单 其五 表单验证
    【H5】15 表单 其四 数据发送
    【H5】14 表单 其三 原生表单部件
    【H5】13 表单 其二 如何构造
    【H5】12 表单 其一 第一个表单
    【H5】11 表格
  • 原文地址:https://www.cnblogs.com/caozengling/p/5593662.html
Copyright © 2020-2023  润新知