前台post过来一字符串str = "张三,23,\n李四,22"
后台可以对此字符串解析装入二维数组,然后用数据初始化出两个对象
person1姓名张三 年龄23 person2姓名李四 年龄22
也可以把两个对象装入list,调用hibernateTemplate的save来保存此list
问题是把接收字符串,初始化出两个对象的代码封装在哪里比较好?Action里?DAO里?或者DTO里?
DTO我知道可以对前台参数与后台实体bean不匹配做数据转换,但具体没写过,可否给端代码瞧瞧重要的是给段代码瞧瞧你们如何封装以及封装在哪javabean实体beanDTO数据转换

解决方案 »

  1.   

    这边有个单个对象Json的样例,LZ看看可满足要求,感兴趣再去细看数组对象怎么搞定的吧。(请重点关注注释部分)
    前端jsp:  <script type="text/javascript" src="js/json2.js"></script><!--使用json的函数,需要下载个json2.js-->
      <script language="javascript">
       var xmlHttp;<!--这边的例子恰好是ajax实现方式。无需额外js或jar-->
      
       function createXMLHttpRequest(){
       if(window.ActiveXObject){
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       else if(window.XMLHttpRequest()){
       xmlHttp=new XMLHttpRequest();
       }
       }
      
       function addNew(){
       var arg0,arg1,arg2,arg3,arg4,arg5;
       arg0=document.all.userName.value;
       arg1=document.all.password.value;
       arg2=document.all.department.value;
       arg3=document.all.headship.value;
       arg4=document.all.sex.value;
       arg5=document.all.old.value;
      
       if(arg0==""){
       alert("请输入姓名!");
       return;
       }
       if(arg1==""){
       alert("请输入密码!");
       return;
       }
       if(arg2==""){
       alert("请输入部门!");
       return;
       }
       if(arg3==""){
       alert("请输入职务!");
       return;
       }
       if(arg4==""){
       alert("请输入性别!");
       return;
       }
       if(arg5==""){
       alert("请输入年龄!");
       return;
       }
      
       var newuser=new User(arg0,arg1,arg2,arg3,arg4,arg5);
       var userAsJSON=JSON.stringify(newuser);<!--json2.js中带的函数,将user对象解析成json字符串-->
       createXMLHttpRequest();
       xmlHttp.open("POST","AddUser",true);
       xmlHttp.onreadystatechange=process;
       xmlHttp.send(userAsJSON);<!--发送这个json字符串-->
       }
      
       function process(){
       var user,arg0,arg1,arg2,arg3,arg4,arg5;
       if(xmlHttp.readyState==4){
       if(xmlHttp.status==200){
       user=JSON.parse(xmlHttp.responseText);<!--解析响应中获得的json字符串,生成user对象-->
       arg0=user.userName;
       arg1=user.password;
       arg2=user.department;
       arg3=user.headship;
       arg4=user.sex;
       arg5=user.old;
       updateTable(arg0,arg1,arg2,arg3,arg4,arg5);
       }
       }
       }
      
    <!--User对象构造函数-->
       function User(userName,password,department,headship,sex,old){
       this.userName=userName;
       this.password=password;
       this.department=department;
       this.headship=headship;
       this.sex=sex;
       this.old=old;
       }
      
       function updateTable(arg0,arg1,arg2,arg3,arg4,arg5){
       document.all.userTable.insertRow(-1);
       var rows=document.all.userTable.rows.length;
       document.all.userTable.rows[rows-1].bgColor="#ffffff";
       document.all.userTable.rows[rows-1].align="center";
      
       document.all.userTable.rows[rows-1].insertCell(-1);
       document.all.userTable.rows[rows-1].insertCell(-1);
       document.all.userTable.rows[rows-1].insertCell(-1);
       document.all.userTable.rows[rows-1].insertCell(-1);
       document.all.userTable.rows[rows-1].insertCell(-1);
       document.all.userTable.rows[rows-1].insertCell(-1);
      
       document.all.userTable.rows[rows-1].cells[0].innerText=arg0;
       document.all.userTable.rows[rows-1].cells[1].innerText=arg1;
       document.all.userTable.rows[rows-1].cells[2].innerText=arg2;
       document.all.userTable.rows[rows-1].cells[3].innerText=arg3;
       document.all.userTable.rows[rows-1].cells[4].innerText=arg4;
       document.all.userTable.rows[rows-1].cells[5].innerText=arg5;
       }
      </script>
      <body>
        <h2>AJAX+JSON示例</h2>
       姓名:<input name="userName" id="userName" type="text"><br>
       密码:<input name="password" id=""password"" type="text"><br>
       部门:<input name="department" id="department" type="text"><br>
       职务:<input name="headship" id=""headship"" type="text"><br>
       性别:<select id="sex">
       <option value="1">男</option>
       <option value="2">女</option>
       </select><br>
       年龄:<input name="old" id=""old"" type="text"><br>
       <input type="button" name="btn" value="新增用户" onClick="addNew()">
       <br><br>
       <table id="userTable" border="1" width="500px" bgcolor="#eeeeee">
       <tr>
       <td align="center">姓名</td>
       <td align="center">密码</td>
       <td align="center">部门</td>
       <td align="center">职务</td>
       <td align="center">性别</td>
       <td align="center">年龄</td>
       </tr>
       </table>
      </body>
    Servlet类对Json的解析:      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
            StringBuilder json = new StringBuilder();
            String line = null;
            //post形式获取json数据
            BufferedReader reader = req.getReader();
            while (null != (line = reader.readLine()))
            {
                line = new String(line.getBytes("ISO8859-1"), "UTF-8");//IE页面传输,默认编码格式ISO8859-1;火狐的为UTF-8
                json.append(line);
            }
            System.out.println("JSON:" + json);        //得到Json对象//JSONObject的实现形式多种多样,这里用的是json-lib-2.4-jdk15.jar的net.sf.json.JSONObject
            JSONObject jsonObj = JSONObject.fromObject(json.toString());        //取得想要的字段
            String userName = jsonObj.getString("userName").toUpperCase();
            String password = jsonObj.getString("password");
            String department = jsonObj.getString("department");
            String headship = jsonObj.getString("headship");
            String sex = jsonObj.getString("sex");
            String old = jsonObj.getString("old") + "岁"; ....//后续想new对象就好办了,不赘述
         }