最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Struts2写接口返回json数据

旗下网站admin44浏览0评论

Struts2写接口返回json数据

Struts2写接口返回json数据

公司老项目Struts,写了一些接口的方法,记录下来。

第一种:

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC    
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
    ".0.dtd">
<struts>
	<!-- 该 属性指定Struts 2中的action由spring容器创 建 -->   
	<constant name="struts.objectFactory" value="spring" />
	<package name="ajax" extends="json-default" namespace="/">
		<action name="testAjaxJson" class="StudentAction">
		    <result type="json">
		        <param name="root">ajaxResult</param>
		    </result>
		</action>
	</package>	
</struts>

Action:

package com.mangocity.ship.web.action;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport {
	private JSONObject ajaxResult;

	public String queryStudent() {
		JSONObject json = new JSONObject();
		json.put("code", "00");
		json.put("Message", "Success");
		return SUCCESS;
	}

	public JSONObject getAjaxResult() {
		return ajaxResult;
	}

	public void setAjaxResult(JSONObject ajaxResult) {
		this.ajaxResult = ajaxResult;
	}

}

访问地址http://localhost:8080/TestAjax/testAjaxJson!queryStudent.shtml

第二种:

struts.xml配置文件像平常返回页面一样配置,但是注意返回的jsp文件的内容如下:

<%@ page contentType="text/html;charset=utf-8"%>${rsJson}

其中用el表达式获取action中的rsJson属性值,这个属性就是接口要返回的数据。

这种方式只是在公司内网各个业务线使用的接口,如果是对外的不建议这样使用。

发布评论

评论列表(0)

  1. 暂无评论