初用reslet笔记

近日新作项目采用jquery easyui + reslet +mybatis架构编写一个小项目,对于前两个我都是第一次用,mabatis之前用过一次,算是会用吧。

还有就是自身也是个开发菜鸟,过程中遇到了各种各样问题,下面说下reslet:

用reslet是在网上下载的例子,照着例子改写的,网例如下:

@Path("student")
public class StudentResource {

    @GET
    @Path("{id}/xml")
    @Produces("application/xml")
    public Student getStudentXml(@PathParam("id")
    int id) {
        System.out.println("运行这个方法");
        return ResourceHelper.findStudent(id);
    }

    @GET
    @Path("{id}/json")
    @Produces("application/json")
    public JSONObject getStudentJson(@PathParam("id")
    int id) {
        System.out.println("111111111" + ResourceHelper.findStudent(id));
        Student tstudent = ResourceHelper.findStudent(id);
        Map<String,String> map=new HashMap();
        ArrayList tt = new ArrayList();

        map.put("age", tstudent.getAge()+"") ;
        map.put("clsid", tstudent.getClsid()+"") ;
        map.put("sex", tstudent.getSex()+"") ;
        map.put("name", tstudent.getName()) ;
        map.put("id", tstudent.getId()+"") ;
        tt.add(map) ;
        tt.add(map);
        System.out.println("调用到json");
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("total",2) ;
            jsonObject.put("rows",tt) ;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject ;
    }

    @POST
    @Path("add")
    public String addStudent(Representation entity) {
        Form form = new Form(entity);
        String name = form.getFirstValue("name");
        int clsId = Integer.parseInt(form.getFirstValue("clsId"));
        int sex = Integer.parseInt(form.getFirstValue("sex"));
        Student student = new Student();
        student.setClsid(clsId);
        student.setName(name);
        student.setSex(sex);
        ResourceHelper.maxId++;
        int id = ResourceHelper.maxId;
        student.setId(id);
        //return String.valueOf(ResourceHelper.addStudent(student));
        return String.valueOf("");
    }

    @PUT
    @Path("update")
    public String updateStudent(Representation entity) {
        Form form = new Form(entity);

        int id = Integer.parseInt(form.getFirstValue("id"));
        Student student = ResourceHelper.findStudent(id);

        String name = form.getFirstValue("name");
        int clsId = Integer.parseInt(form.getFirstValue("clsId"));
        int sex = Integer.parseInt(form.getFirstValue("sex"));

        student.setClsid(clsId);
        student.setName(name);
        student.setSex(sex);

//        return String.valueOf(ResourceHelper.updateStudent(student));
        return String.valueOf("");
    }

    @DELETE
    @Path("delete/{id}")
    public String deleteStudent(@PathParam("id")
    int id) {
        //int status = ResourceHelper.deleteStudent(id);
        //return String.valueOf(status);
        return String.valueOf("");
    }

}

 

注释:/**
     * @Path("student")执行了uri路径,student路径进来的都会调用StudentResource来处理。
     * @GET 说明了http的方法是get方法。
     * @Path("{id}/xml") 每个方法前都有对应path,用来申明对应uri路径。
     * @Produces("application/xml") 指定返回的数据格式为xml。
     * @PathParam("id") int id  接受传递进来的id值,其中id为 {id}定义的占位符要一致。
     */

  通过上面注释可以知道,如果我们需要调用上面的某个方法,

  比如说getStudentJson(),那么路径是:.../student/1/json

  再比如addStudent,那么路径是:.../student/add即可

我在easyui中调用的url格式为:$.post("../servlet/menu/add", {..参数..})其他就是在用类中的各个方法时,每种类型都可以有多个,只要@Path定义为不同就可以。

 

 

 

 

下面说下easyui 前台界面加载数据的问题:

可以通过POST、GET等方式获取数据,

$.post("../servlet/menu/add", {
            menuname : $("#name").val()
        }, function(data, states) {
            alert(1);})
           

js界面中如果需要动态获取变量,比如说需要到数据库中获取,那么可以直接调用后台类:var _menus = <%=new MenuResource().getAllMunus()%>;

就可以获取到值了。

以上问题只是遇到所有问题中的一小部分,上面的问题都是自己不会使用的,因此记录下来勉励自己。

 

posted on 2013-10-17 16:43  dzldcp  阅读(358)  评论(0编辑  收藏  举报