1.Ajax 技术在现有开发中使用非常多,大多是做管理类型系统。在servlet中可以使用ajax。在struts2中共还可以使用servlet的方式来实现ajax。
2.案例:用户名检查
public String checkName() throws IOException{ HttpServletResponse resp = ServletActionContext.getResponse(); if("siggy".equals(name)){ resp.getWriter().print("true"); }else{ resp.getWriter().print("false"); } //return Action.NONE; return null; }
3.使用struts2提供的插件来完成 ajax步骤
a) 导入相关 jar 包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
freemarker-2.3.19.jar
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
javassist-3.11.0.GA.jar
json-lib-2.3-jdk15.jar
jstl-1.2.jar
log4j-1.2.17.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
struts2-json-plugin-2.3.4.jar
xwork-core-2.3.4.jar
b) 编写Action
public class UserAction { private String name; private String result; public String checkName() throws IOException{ HttpServletResponse resp = ServletActionContext.getResponse(); if("siggy".equals(name)){ resp.getWriter().print("true"); }else{ resp.getWriter().print("false"); } //return Action.NONE; return null; } public String list(){ System.out.println("list----------"); List<User> list = new ArrayList<User>(); list.add(new User("张三","男",33)); list.add(new User("李四","男",23)); list.add(new User("王五","男",13)); try { result=JSONArray.fromObject(list).toString(); } catch (Exception e) { e.printStackTrace(); } System.out.println(result); return Action.SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
c) 编写配置文件
<package name="default" extends="json-default" namespace="/"> <action name="checkName" class="cn.sxt.action.UserAction" method="checkName"> </action> <action name="list" class="cn.sxt.action.UserAction" method="list"> <result type="json"> <param name="root">result</param> </result> </action> </package>
4.另一种获取 json 的方式:
public class ListAction { private List<User> list; public String list(){ list = new ArrayList<User>(); list.add(new User("张三","男",33)); list.add(new User("李四","男",23)); list.add(new User("王五","男",13)); return Action.SUCCESS; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } }
Struts.xml
<action name="listJson" class="cn.sxt.action.ListAction" method="list"> <result type="json"> <param name="root">list</param> </result> </action>
Jsp
<script type="text/javascript"> $(function(){ $("#btn").click(function(){ $.post("listJson.action",function(data){ var html=""; for(var i=0;i<data.length;i++){ html+="<tr><td>"+data[i].name+ "</td><td>"+data[i].sex+ "</td><td>"+data[i].age+"</td></tr>"; } $("#content").html(html); }); }); }); </script> </head> <body> <button id="btn">获取数据</button> <table width="80%" align="center"> <tr> <td>姓名</td> <td>性别</td> <td>年龄</td> </tr> <tbody id="content"> </tbody> </table> </body>