举个例子 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文件,自行下载。

转载请注明出处。

struts2框架下的一个简单的ajax例子的更多相关文章

  1. Eclipse搭建Struts框架,及一个简单的Struts例子

    一.下载struts2.0.1 http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包含了开发struts2所需的 ...

  2. 企业级任务调度框架Quartz(3) 一个简单的Quartz 例子

    1. 一个简单的Quartz 工程     本示例应用比起众所周知的 System.out.println("Hello world from Quartz") 来还是要有趣些.当 ...

  3. DOM编程艺术章12:一个简单的Ajax例子

    大概入了JavaScript的门,现在要回过头恶补Ajax和json了,随手翻到dom编程艺术发现有一个适合回忆的例子,先抄录下来,引入对Ajax作用的大概印象,再去掰开了研究. <!DOCTY ...

  4. 关于在Struts2框架下实现文件的上传功能

    struts2的配置过程 (1)在项目中加入jar包 (2)web.xml中filter(过滤器)的配置 <?xml version="1.0" encoding=" ...

  5. Struts2的配置和一个简单的例子

    Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...

  6. MUI框架-05-用MUI做一个简单App

    MUI框架-05-用MUI做一个简单App MUI 是一个前端框架,前端框架就像 Bootstrap,EasyUI,Vue ,为了做 app 呢,就有了更加高效的 MUI,我觉得前端框架有很多,也没有 ...

  7. Android下实现一个简单的计算器源码

    下面的内容是关于Android下实现一个简单的计算器的内容. import android.app.Activity; import android.os.Bundle;import android. ...

  8. Struts2框架下的文件上传文件类型、名称约定

    Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...

  9. linux系统下开启一个简单的web服务

    linux 下开启一个简单的web服务: 首先需要linux下安装nodejs 然后创建一个test.js:   vi test.js var http =require("http&quo ...

随机推荐

  1. ASP.NET Gridview数据库绑定支持增删改,记得要完整实现

    1.错误情况 /WebSite3"应用程序中的服务器错误. 指定的参数已超出有效值的范围. 参数名: index 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息, ...

  2. Advanced Sort Algorithms

    1. Merge Sort public class Mergesort { private int[] numbers; private int[] helper; private int numb ...

  3. How to parse project properties or how to parse files with key-value pair

    If a file has content like raven.enabled = false raven.host = "localhost" raven.port = 808 ...

  4. mysqldump备份还原mysql

    本文实现在mysql 5.7 解压版为例子 1.在window上简单试下一个例子 1.使用管理员权限打开cmd命名行,并切换到mysqldump执行程序下

  5. TP框架 增删查

    TP框架添加数据到数据库1.使用数组方式添加造模型对象 2.使用AR方式 强类型语言存在的方式 3.使用自动收集表单添加 :只能用POST方式,提交数据一个操作方法实现两个逻辑:A显示页面B得到数据 ...

  6. Linux与mv命令结合,移动文件至指定目录

    转自:http://blog.csdn.net/hardwin/article/details/7711635 把当前目录下面的file(不包括目录),移动到/opt/shell find  .  - ...

  7. reshape: from long to wide format(转)

    This is to continue on the topic of using the melt/cast functions in reshape to convert between long ...

  8. NetBeans+Xdebug调试php代码

    本文目录 : Xdebug的工作原理 Xdebug扩展的配置 NetBeans的配置 调试实例 本文小结 参考文档 Xdebug:是PHP的调试器和分析器(Debugger and Profiler ...

  9. 怎么一次性获取form所有的值?rerialize() 、 serializeArray()方法的使用

    from直接提交当然方便,但是有时候需要对数据进行处理再用ajax提交,挨个去获取值未免太麻烦,讲两个一次性获取所有值的方法. 方法1 $("form").serialize(); ...

  10. css代码初始化

    @charset "utf-8";/* 页面元素初始化和常用样式定义-start *//*======== 全局 ========*/body, div, dl, dt, dd, ...