看到别人用了Struts2和JSON,自己也想练练手。记录下练习过程中遇到的问题,以便参考。
使用Maven新建项目:
先挂上pom.xml
13 4.0.0 4com.sk 5struts 6war 70.0.1-SNAPSHOT 8struts Maven Webapp 9http://maven.apache.org 1011 6812 17 18junit 13junit 143.8.1 15test 1619 23org.apache.logging.log4j 20log4j-api 212.0-beta7 2224 28 29 30org.apache.logging.log4j 25log4j-core 262.0-beta7 2731 35org.apache.struts 32struts2-core 332.3.1.2 3436 40 41 42org.apache.struts 37struts-taglib 381.3.10 3943 47 48 49javax.servlet 44servlet-api 452.5 4650 55 58net.sf.json-lib 51json-lib 522.2.3 53jdk15 5459 63 64 65 66 67org.apache.struts 60struts2-json-plugin 612.3.15 6269 81 8270 8071 79org.apache.maven.plugins 72maven-compiler-plugin 733.0 7475 78
再整个VO:User.java
1 package com.sk.struts2.bean; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 7 private String id; 8 private String username; 9 private String pwd;10 // 省略setter和getter11 }
然后是action:JSONAction.java
1 package com.sk.struts2.action; 2 3 import net.sf.json.JSONObject; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 import com.sk.struts2.bean.User; 7 8 public class JSONAction extends ActionSupport { 9 10 private static final long serialVersionUID = -795596058695827298L;11 12 private User user;13 private String jsonString;14 15 @Override16 public String execute() throws Exception {17 JSONObject jsonObject = null;18 if(user != null){19 jsonObject = JSONObject.fromObject(user);20 jsonString = jsonObject.toString();21 System.out.println(jsonString);22 }23 return SUCCESS;24 }25 26 public User getUser() {27 return user;28 }29 30 public void setUser(User user) {31 this.user = user;32 }33 34 public String getJsonString() {35 return jsonString;36 }37 38 public void setJsonString(String jsonString) {39 this.jsonString = jsonString;40 }41 42 }
接着是View:json.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="ISO-8859-1"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 5 6 7 820 21Hello World! 9 10 11 12 13
Here is result:
22 23 24还有JS:json.js
1 $(function(){ 2 $('#btn').click(function(){ 3 var params=$("input").serialize(); 4 var actionPath = getRootPath() + "/jsonTest/jsonAction"; 5 6 $.ajax({ 7 url: actionPath, 8 // 数据发送方式 9 type: "post",10 // 接受数据格式11 dataType : "json",12 // 要传递的数据13 data : params,14 // 回调函数,接受服务器端返回给客户端的值,即result值15 success : show 16 }).always(function(){alert('done');});17 });18 });19 20 function show(result){21 //测试result是否从服务器端返回给客户端22 //alert(result);23 //解析json对象24 var json = eval("("+result+")");25 var obj = "编号: "+json.id+" 用户名: "+json.username+" 密码: "+json.pwd;26 $("#result").html(obj);27 }28 29 //js获取项目根路径,如: http://localhost:8083/uimcardprj30 function getRootPath(){31 //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp32 var curWwwPath=window.document.location.href;33 //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp34 var pathName=window.document.location.pathname;35 var pos=curWwwPath.indexOf(pathName);36 //获取主机地址,如: http://localhost:808337 var localhostPaht=curWwwPath.substring(0,pos);38 //获取带"/"的项目名,如:/uimcardprj39 var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);40 return(localhostPaht+projectName);41 }
最后奉上:struts.xml
1 2 5 67 8 9 10 11 18 1912 1713 14 jsonString15 16
ok,all done.下面测试哈:
最后给个目录结构:
OK.Enjoy!