action处理Ajax请求
来源:互联网 发布:win10装ubuntu 编辑:程序博客网 时间:2024/05/16 18:09
action有3种方式处理Ajax请求:
(1)跟servletAPI耦合的方式:借助response流获取out来完成;
(2)借助Stream结果类型,跟servlet API解耦合方式;
1-给result指定type=stream
<result type="stream">
<param name="contentType">test/html;charset=utf-8</param>
<param name="inputName">is</param>
</result>
2-action中:is = new ByteArrayInputStream(str.getBytes("utf-8"));
(3)struts内置的json插件;
1-.导入:struts2-json-plugin-2.3.20.jar
2-后台:直接给action定义属性,然后给属性赋值即可
public String testjson(){
this.p = new Person();
p.setId(2);
p.setName("zhangsan");
return SUCCESS;
}
3-配置:
1-package----继承json-default包
2-action---result---type:json
1-result中不需要写相应的资源url
具体实例:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'testajax.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> <script type="text/javascript" src="jquery-1.9.1.js"></script> <script type="text/javascript"> $(function(){ $("#b1").click(function(){ $.post('testajax','',function(data){ alert(data); },'text'); }); $("#b2").click(function(){ $.post('testajax1','',function(data){ alert(data.name); },'json'); }); $("#b3").click(function(){ $.post('json/testajax3','',function(data){ alert(data.name); alert("p.id="+data.p.id+",p.name"+data.p.name); },'json'); }); }); </script> </head> <body> <button id="b1">耦合servletapi</button><br> <button id="b2">stream结果类型</button><br> <button id="b3">struts2内置json</button><br> </body></html>Struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myInter" class="com.interceptors.MyInter"/> <interceptor-stack name="abc"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="tokenSession"></interceptor-ref> <interceptor-ref name="myInter"></interceptor-ref> </interceptor-stack> <action name="testajax" class="com.handler.AjaxAction" method="testajax"> </action> <action name="testajax1" class="com.handler.AjaxAction" method="testajax1"> <result type="stream"> <param name="contentType">test/html;charset=utf-8</param> <param name="inputName">inputStream</param> </result> </action> </package> <package name="json" namespace="/json" extends="json-default"> <action name="testajax3" class="com.handler.AjaxAction" method="testjson"> <result type="json"></result> </action> </package></struts>action域:
package com.handler;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.bean.Person;import com.google.gson.Gson;import com.opensymphony.xwork2.ActionSupport;public class AjaxAction extends ActionSupport {public String testajax()throws Exception{HttpServletResponse response = ServletActionContext.getResponse();PrintWriter out = response.getWriter();out.print("hello");out.flush();out.close();return NONE;}private InputStream inputStream;public InputStream getInputStream() {return inputStream;}public String testajax1()throws Exception{Person p = new Person();p.setId(1);p.setName("李四");Gson g = new Gson();String str = g.toJson(p);inputStream = new ByteArrayInputStream(str.getBytes("utf-8"));return SUCCESS;}private Person p;public Person getP() {return p;}public void setP(Person p) {this.p = p;}private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String testjson(){this.p = new Person();p.setId(2);p.setName("zhangsan");this.name = "abc";return SUCCESS;}}运行结果:
- action处理Ajax请求
- ajax调用action类处理异步请求
- ajax请求action
- ajax请求.action
- ajax传参数到action中处理相应请求,以及过去的servlet处理请求
- Action处理请求参数
- struts2 action处理ajax
- Ajax请求action全攻略
- ajax请求struts2 中的action
- Jqery Ajax请求Action方法
- struts2-Action处理请求参数
- struts2处理ajax请求
- ajax请求中文处理
- Ajax (4) ---处理请求
- Struts2 处理ajax请求
- Ajax Post请求处理
- AJax数据请求处理
- SpringMVC处理AJAX请求
- 是谁要求王选下台?
- mapreduce 输出乱码
- 将下载到本地的JAR包手动添加到Maven仓库
- 2017 Multi-University Training Contest
- caffe随记(五)---/build/tools/caffe.bin工具简析
- action处理Ajax请求
- 神经网络的Dropout的理解
- LintCode-[容易] 211. 字符串置换
- Unity3d中3D Text对模型的穿透显示
- python-pandas的基本用法08
- 高级I/O函数
- 设计模式之命令模式
- UVA
- LeNet的详细网络结构