Dwr — 如何接收自定义类型的返回值

Dwr — 如何接收自定义类型的返回值

1. 自定义类型User

package org.ljm.dwr.service;

public class User {

    private int id;
    private String username;
    private Group group;

    public User() {
    }

    public User(int id, String username, Group group) {
        this.id = id;
        this.username = username;
        this.group = group;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

}

2. 自定义类型Group

package org.ljm.dwr.service;

public class Group {

    private int id;
    private String name;

    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Group() {
    }


    public Group(int id, String name) {
        this.id = id;
        this.name = name;
    }

}

3. 要被客户端调用的类UserService

package org.ljm.dwr.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {

    public static Map<String,User> users = new HashMap<String,User>();

    static {
        users.put("1", new User(1,"孙悟空",new Group(1,"取经组")));
        users.put("2", new User(2,"猪八戒",new Group(1,"取经组")));
        users.put("3", new User(3,"白骨精",new Group(2,"吃肉组")));
        users.put("4", new User(4,"红孩儿",new Group(2,"吃肉组")));
    }

    public List<User> list() {
        List<User> us = new ArrayList<User>();
        for(String key : users.keySet()) {
            us.add(users.get(key));
        }
        return us;
    }

    public void add(User user) {
        users.put(String.valueOf(user.getId()), user);
    }

    public User user(String id) {
        return users.get(id);
    }
}

4. dwr.xml




<dwr>

  <allow>
    
    <create creator="new">
      <param name="class" value="org.ljm.dwr.service.DwrHello"/>
    create>
    <create creator="new">
      <param name="class" value="org.ljm.dwr.service.UserService"/>
    create>
    <convert converter="bean" match="org.ljm.dwr.service.User"/>
    <convert converter="bean" match="org.ljm.dwr.service.Group"/>
  allow>

dwr>

5. jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>通过dwr完成UCRDtitle>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.7.2.min.js">script>

<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/engine.js">script>

<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/interface/UserService.js">script>
<script type="text/javascript">
  $(function(){
      listUsers();
      function listUsers() {
          UserService.list(function(users){
             var tbody = $("#tb tbody").empty();
             $.each(users,function(i,user){
                 tbody.append(""+user.id+""+user.username+""+user.group.name+"");
             });
          });
      }
      $("#btn").on("click",function(){
         var userId = $("#userId").val();
         var username = $("#username").val();
         var gId = $("#gId").val();
         var gName = $("#gName").val();
         var user = {"id":userId,"username":username,"group":{"id":gId,"name":gName}};
         console.log(user);
         UserService.add(user);
         listUsers();
      });
  });
script>
head>
<body>
<table id="tb" style="width:700px;" align="center" border="1">
  <thead>
    <tr>
      <th>用户标识th>
      <th>用户名th>
      <th>用户所在组th>
    tr>
  thead>
  <tbody>tbody>
table>
<div id="addUser" style="text-align:center;margin-top:40px;">
用户Id:<input id="userId" type="text"/>
用户名:<input id="username" type="text"/>
组Id:<input id="gId" type="text"/>
组名:<input id="gName" type="text"/>
<input id="btn" type="button" value="添加"/>
div>
body>
html>

你可能感兴趣的:(dwr)