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

来源:互联网 发布:央视网络电视打不开 编辑:程序博客网 时间:2024/05/21 09:26

本来今天找了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 0