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

  • 相关阅读:
    iframeUpload
    获取当前文件路径。
    ie6 overflow 失效
    ie8 vml不显示
    json转换
    nodejs for windows
    模块化管理组件(2012/05/09)
    模块化管理组件v0.1
    Firefox和IE之间7个JavaScript的差异
    c输入函数细节
  • 原文地址:https://www.cnblogs.com/lyf906522290/p/8573533.html
Copyright © 2020-2023  润新知