举个例子 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>My JSP 'ajax.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=basePath%>res/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button").bind("click",function(){
$.ajax({
type:"post",
url:"<%=basePath%>ajax.action",
data:"array="+$("#text").val(),
datatype:"json",
success:function(msg){
alert(msg);
},
error:function()
{
alert("失败");
}
});
})
});
</script>
</head> <body>
<input id = "text" type = "text" >
<input id = "button" type = "button" value ="提交">
</body>
</html>

后台:

package test;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class ajax {
private String str;
public String ajax()
{
HttpServletRequest request = ServletActionContext.getRequest(); str = "action处理请求后返回给ajax参数:"+request.getParameter("array");
System.out.println(request.getParameter("array")); return Action.SUCCESS;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
} }

  Struts2 配置文件:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="example" extends="json-default">
<action name = "ajax" class = "test.ajax" method = "ajax">
<result type = "json">
<param name = "root">str</param>
</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>

  所用到的jar包:

所用到的jQuery文件,自行下载。

转载请注明出处。

最新文章

  1. Navicat软件中mysql中int、bigint、smallint和tinyint的区别、布尔类型存储以及乱码问题的解决
  2. win10快速开机
  3. ASP.NET简单实现APP中用户个人头像上传和裁剪
  4. Xcode 缓存 帮助文档 隐藏文件夹显示方法
  5. oracle 查看运行中sql
  6. 04-树5 Complete Binary Search Tree
  7. UVa 10791 Minimum Sum LCM【唯一分解定理】
  8. git的CentOS服务端安装和windows客户端的使用
  9. webpack(一) 配置
  10. 【frame系列标签】
  11. solr6.4.1搜索引擎(4)tomcat重启后数据加载缓慢或丢失
  12. python3通过gevent.pool限制协程并发数量
  13. 身在上海的她,该不该继续&quot;坚持&quot;前端开发?
  14. 原生js实现图片轮播效果
  15. Redis数据库介绍
  16. 我的菜单在母版页,如何更改菜单点击后的效果 Ver2
  17. 高性能JavaScript(高性能Ajax)
  18. (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  19. [js常用]百度将文字转化为语音实例
  20. 音视频处理之FFmpeg封装格式20180510

热门文章

  1. elasticsearch 不能通过9200端口访问
  2. CCNP第一课:默认路由(路由黑洞,路由终结)
  3. # Instrument Time Profiler教程之Time Profiler
  4. Hadoop化繁为简(二)—层层递进轻松入门hdfs
  5. 抓包工具 - HttpWatch
  6. 用C写一个web服务器(四) CGI协议
  7. 一个web应用的诞生(13)--冲向云端
  8. Scala note 1
  9. 浅析DES与AES、RSA三种典型加密算法的比较
  10. android设计,图标等概述