前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Struts Ajax Json

Struts Ajax Json

作者头像
netkiller old
发布2018-03-05 16:06:26
9290
发布2018-03-05 16:06:26
举报
文章被收录于专栏:NetkillerNetkiller

Netkiller Java 手札

Java, Servlet, JavaBean ...

5.4. Ajax + JSON

struts.xml 中加入

代码语言:javascript
复制
		<action name="Captcha" class="com.example.action.ajax.Captcha">
			<result name="success" type="json"></result>
		</action>		

Java 文件

代码语言:javascript
复制
		package com.example.action.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Captcha extends ActionSupport {
	private static final long serialVersionUID = 7284583098398030297L;

	private String string1 = "A";
	private String[] stringarray1 = {"A1","B1"};
	private int number1 = 123456789;
	private int[] numberarray1 = {1,2,3,4,5,6,7,8,9};
	private List<String> lists = new ArrayList<String>();
	private Map<String, String> maps = new HashMap<String, String>();
	
	//no getter method, will not include in the JSON

	public Captcha(){
		lists.add("list1");
		lists.add("list2");
		lists.add("list3");
		lists.add("list4");
		lists.add("list5");
		
		maps.put("key1", "value1");
		maps.put("key2", "value2");
		maps.put("key3", "value3");
		maps.put("key4", "value4");
		maps.put("key5", "value5");
	}

	public String execute() {
               return Action.SUCCESS;
        }
	
	public String getString1() {
		return string1;
	}

	public void setString1(String string1) {
		this.string1 = string1;
	}

	public String[] getStringarray1() {
		return stringarray1;
	}

	public void setStringarray1(String[] stringarray1) {
		this.stringarray1 = stringarray1;
	}

	public int getNumber1() {
		return number1;
	}

	public void setNumber1(int number1) {
		this.number1 = number1;
	}

	public int[] getNumberarray1() {
		return numberarray1;
	}

	public void setNumberarray1(int[] numberarray1) {
		this.numberarray1 = numberarray1;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}
	
}		

测试URL http://localhost:8080/ajax/Captcha.action

5.4.1. GET/POST JSON

struts.xml 文件加入 enableSMD

代码语言:javascript
复制
		<action name="Captcha" class="com.example.action.ajax.Captcha">
			<interceptor-ref name="defaultStack" />
			<interceptor-ref name="json">
				<param name="enableSMD">true</param>
			</interceptor-ref>
			<result name="success" type="json"></result>
		</action>			

Java 代码

代码语言:javascript
复制
			package com.example.action.ajax;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Captcha extends ActionSupport {
	private static final long serialVersionUID = 7284583098398030297L;

	private String phone = "13113668890";
	private String email = "netkiller@msn.com";
	private Boolean status = false;
	
	//no getter method, will not include in the JSON

	public Captcha() {

	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String execute() {
		this.status = true;
		System.out.printf("%s, %s, %s, %b\n", this.getClass().getName(), this.getPhone(), this.getEmail(), this.getStatus());
		return Action.SUCCESS;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}
	public boolean getStatus() {
		return this.status;
	}
}			

使用 curl 模拟 post 测试

代码语言:javascript
复制
			# curl http://192.168.4.34:8080/ajax/Captcha.do
{"email":"netkiller@msn.com","phone":"13113668890","status":true} 

# curl -X POST -H "Content-Type:application/json" -d '{"phone":"13066884444", "email":"neo.chan@live.com"}' http://192.168.4.34:8080/ajax/Captcha.do
{"email":"neo.chan@live.com","phone":"13066884444","status":true}			

GET 例子

代码语言:javascript
复制
			# curl "http://192.168.4.34:8080/ajax/Captcha.do?phone=13322993040&email=netkiller@mac.com"
{"email":"netkiller@mac.com","phone":"13322993040","status":true}			
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Netkiller Java 手札
    • Java, Servlet, JavaBean ...
      • 5.4. Ajax + JSON
        • 5.4.1. GET/POST JSON
    相关产品与服务
    验证码
    腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档