前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jquery通过JSON和Struts的Action交互

Jquery通过JSON和Struts的Action交互

作者头像
the5fire
发布2019-02-28 15:33:46
6640
发布2019-02-28 15:33:46
举报

前面写过 《JQuery通过JSON和Servlet进行交互》 ,不过在SSH架构的项目中要是依然在写出一个Servle实在是有点另类的(至少我目前是这么认为的),因此就应该顺势而行,使用Jquery通过JSON和后台Action交互。 具体实现起来和前面的那篇文章差不太多。简明扼要的说一下: 首先配置好struts2的配置文件,定义一个Action名为test,对应的Class为TestAction:

.. code:: java

代码语言:javascript
复制
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
    private String testVar;

    public String getTestVar() {
        return testVar;
    }

    public void setTestVar(String testVar) {
        this.testVar = testVar;
    }

    public String execute(){
        System.out.println(testVar);
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json;charset=UTF-8");  
        response.setHeader("Charset","UTF-8"); 
        PrintWriter out = null;
        String json="{\"testVar\":\"" + testVar + "\"}";
        try {
         out = response.getWriter();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
              out.write(json);
              out.flush();  
              return null;
    }
}

然后页面上通过jquery调用,前提是你要引入jquery的库:

::

代码语言:javascript
复制
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.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="js/jquery-latest.js"></script>
  </head>

  <body>
    This is my JSP page. 
    <input type="button" value="getDate" onclick="getCheck()"/>
    <br>
  </body>
  <s:debug></s:debug>
  <script type="text/javascript">
    function getCheck(){
                var url = 'test.action';
                var params = {
                        testVar:"i'm huyang"
                };
                $.ajax({
                 url:url,
                 type:"POST",
                 data:params,
                 dataType:"json",
                 success:function(data){
                    alert(data.testVar);

                 }
                });
     }
  </script>

</html>

最后运行一下效果如下:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-05-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档