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”) 换掉传递给前台的名字。

        










     


0 0