java类代码:

package com.rain.json.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@ParentPackage("json-default")
@Results({ @Result(name = "jsonExample", type = "json") })
public class JsonExampleAction {
	private String result;
	private String type;

	@Action(value = "/json/jsonExample")
	public String jsonExample() {
		if ("json".equals(type)) {
			result = "json";
		} else {
			result = "other";
		}
		return "jsonExample";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}

jsp页面代码:

<%@ 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>jsonExample</title>
    <script type="text/javascript" src="<%=basePath%>js/base/jquery-1.4.4.min.js"></script>
    <script type="text/javascript">
    	function jsonExample(){
    		$.ajax({
    			type : "POST",
				dataType : "json",
				data : {type:"json"},
				url : "<%=basePath%>json/jsonExample.do",
				async : false,
				success : function(data) {
					if (data.result == "json") {
						alert("json");
					} else if (data.result == "other") {
						alert("other");
					}
				}
    		});
    	}
    </script>
  </head>
  
  <body>
    <input type="button" value="json" onclick="jsonExample();"/>
  </body>
</html>



Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐