本文主要讲述,在项目操做的时候,经过servlet或者Struts2或者springmvc向前台页面传入json数据或者传递HTML表单以及html的一些元素以及JavaScript函数等。javascript
代码以下:html
1.这是在Struts2的框架下的传递方法直接传向页面:java
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;
}
}ajax
2,经过springmvc的框架spring
@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的框架,是公司中比较经常使用的,相信你们学习了个人这篇分享后对json数据的操做确定就没有问题了,下面就讲一下这种转换
安全
代码大体是这样的:跟以前fastjson-1.2.6.jar方法JSONArray不一样的是用的 JSONArray.fromObject();方法进行的转换,我会把应用案例传在博客中请从项目地址四进行下载,是一个了利用jQuery作的高效率分页项目,分享给你们》》》》》》》用到的jar包,jar包都在项目中。
session
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();
}
mvc
有四个小项目源码地址:
app
项目×××地址:http://down.51cto.com/data/2231519
项目地址二:http://down.51cto.com/data/2231520
项目地址三:http://down.51cto.com/data/2231521
项目地址四:http://down.51cto.com/data/2237422