Struts2学习笔记七(Annotation配置、异步调用ajax(dom4j、Json))
来源:互联网 发布:淘宝儿童足球 编辑:程序博客网 时间:2024/06/06 04:47
1、使用注解,必须使用这个插件struts2-convention-plugin-2.3.30.jar 两者冲突一注解为准。。
2、在action之前,此时就可以删除struts.xml文件了,但是拦截器的定义还需要在xml中。
<span style="white-space:pre"></span>@ParentPackage("struts-default")<span style="white-space:pre"></span>@Action(value="login",results={<span style="white-space:pre"></span>@Result(name="success",location="/result.jsp"),<span style="white-space:pre"></span>@Result(name="input",location="/login.jsp")<span style="white-space:pre"></span>})<span style="white-space:pre"></span>@InterceptorRef("")<span style="white-space:pre"></span>@InterceptorRefs({@InterceptorRef(""),@InterceptorRef("")})<span style="white-space:pre"></span>@ExceptionMappings({@ExceptionMapping(),@ExceptionMapping("")})
struts2-convention-plugin-2.3.30.jar 这个插件会出现result无法找到的问题 ,需要在package 设置 namespace="/"
3、ajax,异步调用。 大概流程。。
dom4j 封装xml。
前台 <input type="button" value="get information" onclick="getInfo();"> 通过onclick 拦截action请求,发送给function getInfo()
请求 $.post("login.action"{ name: $("#name").val() },并传参数到action, 执行 action
action中:
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=utf-8");response.setHeader("cache-control", "no-cache"); PrintWriter out = response.getWriter();OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");XMLWriter writer = new XMLWriter(out, format);writer.write(document);out.flush();out.close();
直接写到response 中,通过流直接写到前台。 前台对得到的流数据进行处理,组装成 html语言。显示出来。
function(returnedData, status)
{var id = $(returnedData).find("id").text();var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>age</th><th>address</th><tr align='center'><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td><td>" + address + "</td></tr></table>";$("#theBody table:eq(0)").remove();$("#theBody").append(html);});
4、Gson 一样的 ,只不过不用dom4j,
<span style="white-space:pre"></span>People people = new People();people.setId(1);people.setName(name);people.setAge(30);people.setAddress("beijing");Gson gson = new Gson();String result = gson.toJson(people);.....................out.print(result);
前台读取时:
var people = returnedData;
var id = people.id;
var name = people.name;
var age = people.myAge;
var address = people.address;
5、 也可以用 struts2-json-plugin-2.3.30.jar Gson插件
public String execute() throws Exception{this.id = 1;this.name="lisi";this.age = 30;this.address = "beijing";return SUCCESS;}
<package name="struts2" extends="json-default" namespace="/">继承json-default 实际上也继承了struts-default包,
<span style="white-space:pre"></span><action name="getJsonAction2" class="com.yanlei.struts2.GetJsonAction2"><result name="success" type="json">/login.jsp</result></action>结果类型为 json。在继承的保重定义了 结果类型。
<result name="success" type="json">
<param name="excludeProperties">address</param> // 可以排除address属性,不传递到前台。防止属性太多,麻烦。
</result>
也可以在属性的get方法上@JSON(name=“myAge”) 换掉传递给前台的名字。
- Struts2学习笔记七(Annotation配置、异步调用ajax(dom4j、Json))
- Struts2学习笔记(十四)Annotation实现Struts2的配置
- Struts2学习笔记(十六)异步传输机制Json插件
- Struts2学习笔记(七)
- spring零配置(Annotation)学习笔记
- Struts2学习笔记(七) 上传文件
- Java Web学习笔记(七)struts2
- Struts2 学习笔记(七)模型驱动
- struts2学习笔记之七(常量)
- java基础学习笔记(七) ajax
- Ajax异步交互 - 学习笔记(1)
- struts2 配置 jquery ajax 调用 action 返回JSON 数据
- Struts2学习(四)利用ajax异步上传
- J2EE系列之Struts2学习笔记(七)--struts.xml配置
- ajax,json学习笔记(四)跨域
- ajax,json学习笔记(五)总结
- Struts2学习笔记七
- struts2学习笔记(8)-------struts2的ajax支持
- 模块编译方式
- Android将bitemap保存在SD卡并读取
- tomcat+nginx+redis实现均衡负载、session共享(二)
- 搭建faster-rcnn进行目标检测的环境
- php rsa 加密、解密、签名、验签
- Struts2学习笔记七(Annotation配置、异步调用ajax(dom4j、Json))
- java多线程向数据库写入数据 -完整版
- 日历
- Boost程序库学习-foreach
- 《Java虚拟机》必知必会的 14 个问题总结(内存模型+GC)
- An App ID with Identifier "xxx" is not available . Please enter a different string.解决方法
- C# 7.0 新功能代码范例
- Android 常用 adb 命令总结
- linux文件属性类型