이 기사는 주로 일반 데이터를 JSON 데이터로 빠르게 변환하는 방법에 대해 설명하고 총 2 가지 방법에 대해 설명합니다.
총리는 페이지 및 단체 카테고리를 준비합니다.
페이지:
<body> <div id = "toplogindiv"> username : <input name = "user.name"id = "loginname" /> password : <input name = "user.password"id = "loginpassword" /> <label> <입력 유형 = "button"name = "loginbutton"value = "login"onclick = "dollick ="; " /> </label> </div> <div id = "demo"> </div> </body>
엔티티 클래스 :
패키지 Bean; public class user {private int id; private string username; private string password; ...... get and set methods}}방법 1 : JSON 변환 패키지를 사용하여 JSON 데이터 변환
첫 번째 단계는 관련 패키지를 도입하는 것입니다
2 단계 : 페이지 제출 및 콜백 기능 처리 결과.
<script type = "text/javaScript"> function dologin () {var name = $ ( '#loginname'). val (); var password = $ ( '#loginpassword'). val (); var data1 = { 'user.username': name, 'user.password': password}; $. $ .getJson은 (data.flag) {$ ( '##toplogindiv'). html ( ""); $ ( '#demo'). html ( "현재 사용자 :"+data.user.username+"를 처리하는 데 사용해야합니다. "+data.msg);} else {$ ( '#demo'). html (data.msg);}});} </script> 3 단계 : struts2는 JSON "키 단계"를 변환하기 위해 작업으로 점프합니다.
private user = new user (); private boolean flag; private string msg; ...... get and set 메소드 public string login ()가 ioexception {if (user.getusername ( "admin") && user.getpassword (). 잘못된! "; flag = false;} map <string, object> list = new Hashmap <string, object> (); // 여기에 맵은 get and set methods.put ("flag ", flag)을 사용하지 않습니다. list.put ( "msg", msg); if (flag) {list.put ( "user", user);} servletactionContext.getResponse (). setCharacterEncoding ( "utf-8"); servletactionContext.getResponse (). getWriter (). print (jsonobject.fromobject (list)); return null; // 여기의 리턴 값은 null이며, 처리를 위해 조치 구성으로 돌아갈 필요가 없습니다}방법 2 : Struts2를 사용하여 JSON 데이터 변환에 대한 조치를 구성합니다.
1 단계 : 패키지를 소개합니다
이 방법은 struts2에 필요한 패키지를 기반으로 다음 패키지를 도입하면됩니다.
2 단계 : 페이지 제출 및 콜백 기능 처리 결과. 방법 1의 두 번째 단계를 참조하십시오.
3 단계 : 작업 구성
<package name = "json_default"namespace = "/"extends = "json-default"> // extends configuration <action name = "user_*"method = "{1}"> <result type = "json"> // 여기에 유형을 지정하는 루트 객체는 세로화되도록 루트 객체를 지정합니다. 기본값-> <param name = "root"> list </param> <!-매개 변수 includeproperties 직렬화 될 루트 객체의 속성을 지정하고 여러 속성이 쉼표로 구분됩니다-> <param name = "Includeproperties"> msg, user.username </param> <!-뿌리 득점자가 명시되어 있습니다. 제외 된 속성은 직렬화되지 않습니다-> <param name = "excludeproperties"> user.password </param> <!-parameter excludenullProperties 빈 값으로 속성을 직렬화할지 여부를 지정합니다-> <param name = "excludenullProperties"> true </param> </acture> 4 단계 : struts2는 JSON "키 단계"를 변환하기 위해 작업으로 점프합니다.
private user = new user (); private boolean flag; private boolean flag; private map <string, object> list = null; // 맵을 준비하고 설정해야합니다 ............ get and set 메소드 공개 문자열 login () ioexception {if (user.getUsername ( "admin") && user.getpassword (). 성공적으로 "; flag = true;} else {msg ="로그인 실패, 사용자 이름 또는 비밀번호가 잘못되었습니다! "; flag = false;} list = new Hashmap <string, object> (); list.put ("flag ", list.put ("msg ", msg", msg); if (flag) {list.put ( "사용자", 사용자 "; 데이터 변환을위한 구성 파일Struts에서 JSON의 위의 전환 및 전달 방법은 내가 공유하는 모든 내용입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.