struts2 在jquery 进行ajax请求 返回json 和String

本来今天找了struts2返回json 的相关内容,转再了一篇博客,但是下午自己运行了一下出现些问题 现在重新整理一下。

首先导包,我用的struts-2.3.15.3版本

这是工程里的导入架包,可能有用不到的根据自己情况将导入。

配置struts.xml

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />

	<package name="systemManage" namespace="/jsp"
		extends="json-default" >            
  <!--输出json 必须继承json-default-->
 <action name="WB.*" class="com.jereh.omg.action.system.MyAction" method="{1}">
	<!-- *是通配符,method的{1}意思是第一个*代表的字符 相应的是Action.java里面 对应名称的方法。-->	
         <result name="map" type="json">
                <param name="root">outMap</param>
            </result>
       <result name="list" type="json">
                <param name="root">outList</param>
            </result>

 
 
 
<!-- result 的type是json 型 root代表要唯一输出的-->
</action>
	</package>
	
<!-- Add packages here -->

</struts>
 
 
 ---------------------------------------------------------------新建一个普通类 ,写个返回类型为String的函数作为action调用的函数,注意提高get和set 类名包名和Struts中 统一 
package com.jereh.omg.action.system;

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



public class MyAction  {
private Map<String, Object> outMap;
private List<Object> outList;

	public Map<String, Object> getOutMap() {
	return outMap;
}

public void setOutMap(Map<String, Object> outMap) {
	this.outMap = outMap;
}

public List<Object> getOutList() {
	return outList;
}

public void setOutList(List<Object> outList) {
	this.outList = outList;
}

	public String getMap() throws Exception {
		outMap=new HashMap<String, Object>();
		outMap.put("Rows", "holle");
		
		return "map";
	}
	public String getList() throws Exception {
		outList =new ArrayList<Object>();
		outList.add("123");
		outList.add("456");
		return "list";
	}
}
配置完了就可以用ajax在前台调用了,
outList 是输出jsonArray
outMap 是输出jsonObject
比如说:
 $.ajax({
           type: "POST",
           url: "jsp/WB.getMap.action",//用.*的形式时要加上 .action
              data: "WBCode="+value,
                 success: function(msg){
               alert(msg);
                           }
                          
                       });
 $.post("jsp/WB.getMap.action",function(msg){
alert(msg);
})


 
 
 
输出String是要用到inputstream 帮助文档 写的比较清楚这里就简单介绍servlet里 我们是这样输出的 
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World!  This is an AJAX response from a Struts Action.");
out.flush();
out.close();

struts2中 这样处理java类 提供一个
InputStream
类型的输入流 属性
package actions;
 
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;
 
public class TextResult extendsActionSupport  {
    privateInputStream inputStream;
    publicInputStream getInputStream() {
        returninputStream;
    }
 
    publicString execute()throwsException {
        inputStream =newByteArrayInputStream("Hello World! This is a text string response from a Struts 2 Action.".getBytes("UTF-8"));
        returnSUCCESS;
    }
}
<actionname="text-result"class="actions.TextResult">
    <resulttype="stream">
        <paramname="contentType">text/html</param>
        <paramname="inputName">inputStream</param>
    </result>
</action>




Struts.xml中配置action


 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值