本文主要讲述,在项目操作的时候,通过servlet或者Struts2或者springmvc向前台页面传入json数据或者传递HTML表单以及html的一些元素以及JavaScript函数等。

代码如下:

1.这是在Struts2的框架下的传递方法直接传向页面:

    public String login() throws Exception {


        Admin loginuser=userservice.loginUser(username,password);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter print=response.getWriter();
        if(loginuser!=null){

            //放入session
            request.getSession().setAttribute("loginUser",loginuser);
            //下边这一种线程不安全因为hashMap是非线程安全的
//            ActionContext.getContext().getSession().put("loginUser",loginuser);
            print.write("1");
            print.flush();
            print.close();
            return     NONE;
        }else{

            
            //测试地址:http://localhost:8080/newdemo/login
            response.setContentType("text/html");  
            response.setCharacterEncoding("utf-8");  
            PrintWriter out = response.getWriter();  
            //通过StringBuilder追加的形式用JavaScript进行前台的传递
//            StringBuilder builder = new StringBuilder();    
//            builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");    
//            builder.append("alert(\"用户名或者密码错误,请重新登陆!\");");
//            builder.append("window.location.href=\"/newdemo/login.jsp\";");    
//            builder.append("</script>");    
//            out.print(builder.toString());
            
            //通过html的形式向前台页面传入一个表单或者页面常用标签
            out.write("<table align=\"center\" border=\"1\">");
            out.write("<tr>");
            out.write("<td>" +"陆垚知马俐"+"</td>");
            out.write("<td>¥" +"日久见人心"+"</td>");
            out.write("</tr>");
            out.write("<table>");
            out.write("<h1 style=\"font-size: ;color:blue;\">" +"说到做到"+"</h1><br/>");
            out.write("<h1>" +"不放空炮"+"</h1");
            out.close();

           
            
            
            //1,利用json数据形式写好直接传递,用ajax里的success的result来接受如result.status=n;
//            String json = "{\"status\":\"n\",\"info\":\"手机号已存在!\"}";
//            print.write(json);
            
//            2,对象转为json,通过阿里巴巴的控件fastjson-1.2.6.jar方法JSONArray进行转换
//            Map<String,String> map=new HashMap<String, String>();
//            map.put("sdf", "啦啦啦啦啦");
//            map.put("地方", "发反反复复");
//            map.put("个人", "灌灌灌灌");
//            map.put("会员", "谁是谁");
//            map.put("为", "去去去");
//            print.write(JSONArray.toJSONString(map));
            
            //把对象转为json
//            Admin andmin=new Admin();
//            andmin.setId(1);
//            andmin.setLoginName("对方答复");
//            andmin.setLoginPwd("奋斗奋斗");
//            Set<Note> notesForNewuser = new HashSet<Note>();
//            Note n=new Note();
//            n.setId(3);
//            n.setAlert("速度快金晶科技");
//            notesForNewuser.add(n);
//            andmin.setNotesForNewuser(notesForNewuser);
//            print.write(JSONArray.toJSONString(andmin));
            
//            print.write("2");
            print.flush();
            print.close();
            
            return NONE;
        }
        
    }


2,通过springmvc的框架

    @RequestMapping(value="/login", produces="text/html;charset=UTF-8")
    @ResponseBody
    public String login(@RequestParam final String name,
            @RequestParam final String pwd,
            @RequestParam final String type){


        
        AdminBean admin=new AdminBean();
        admin.setAdmin_Username(name);
        admin.setAdmin_Password(pwd);
        Integer role=Integer.parseInt(type);
        admin.setAdmin_Role(role);
        
        AdminBean adminbean=adminService.login(admin);
        String json=null;
        if(adminbean!=null){

            json="{\"code\":\"200\"}";
        }else{

            json = "{\"code\":\"100\",\"msg\":\"您输入的用户名或密码或权限有误,请核对后再输\"}";
        }
                return json;
        
    }

当然了还有一种对象转json的框架,是公司中比较常用的,相信大家学习了我的这篇分享后对json数据的操作肯定就没有问题了,下面就讲一下这种转换

代码大致是这样的:跟之前fastjson-1.2.6.jar方法JSONArray不同的是用的 JSONArray.fromObject();方法进行的转换,我会把应用案例传在博客中请从项目地址四进行下载,是一个了利用jQuery做的高效率分页项目,分享给大家》》》》》》》用到的jar包,jar包都在项目中。

public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {
        
        response.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        PaginationSupport page = new PaginationBiz().findUsersByPage(pageNo, pageSize);
        
        JSONArray toJSON = JSONArray.fromObject(page);
        System.out.println(toJSON);
        out.print(toJSON);
        out.close();
    }

有四个小项目源码地址:

项目×××地址:

项目地址二:

项目地址三:

项目地址四:http://down.51cto.com/data/2237422