Struts2学习笔记七(Annotation配置、异步调用ajax(dom4j、Json))

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

        










     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值