JQuery Ajax无法在Struts的JSP中使用 [英] JQuery Ajax not working in JSP with Struts

查看:84
本文介绍了JQuery Ajax无法在Struts的JSP中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有Ajax功能的Web应用程序演示.但是,Ajax似乎无法使用JQuery.

I am trying to create a web app demo with the feature of Ajax. However, the Ajax seems not working using JQuery.

我尚未给出任何答复,但我认为该警报应该起作用.但是,单击按钮后什么也没发生.

I have not given any response yet, but I thought that the alert should be working. However, nothing happens after I clicked the button.

也许Ajax参数有问题?我仍然不清楚如何将变量从Ajax传递到动作以及另一种方法.

Maybe there is something wrong in the Ajax parameter? I am still not clear how to pass variable from Ajax to action and another way around.

任何建议都值得赞赏.

<script>
    $(document).ready(function(){
        $("#select").click(function() {
            $.ajax({
                url:'/db/database!selectAll.action',
                data:'{}',
                type:'POST',
                dataType:"JSON",
                success:function(data){
                    alert(data);
                }
            });
        });
        $("#delete").click(function() {
            alert("aa");
        });
    });
</script>

HTML标记:

<input id="select"type="button" value="select"/>

动作:

package com.rwy.demo.web.db;

import com.opensymphony.xwork2.ActionSupport;
import com.rwy.demo.bean.Person;
import com.rwy.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by cbl on 2016/1/22.
 */
public class DatabaseAction extends ActionSupport{
    @Autowired
    private PersonService personService;


    public void selectAll() {
        List<Person> personList = personService.selectAllPerson();    
    }
    public void update() {

    }
    public void delete() {

    }
    public void add() {

    }    
}

推荐答案

Ajax完全围绕传递参数而不是json进行操作,并返回json结果.您可以将 Struts2 JQuery插件

Ajax to action an all way around passing parameters instead of json and return json results. You could use Struts2 JQuery plugin along with Struts2 JSON plugin. It has tags that you can use in JSP to generate HTML and jQuery content when the page is rendered. This way is preferable because you need to write less javascript code and get less errors doing so. For example a basic ajax call

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
  <head>
    <sj:head/>
  </head>
  <body>
    <div id="div1">Div 1</div>
    <s:url id="ajaxTest" value="/AjaxTest.action"/>

    <sj:a id="link1" href="%{ajaxTest}" targets="div1">
      Update Content
    </sj:a>
  </body>
</html>

您可以在其中添加参数

<s:url id="ajaxTest" value="/AjaxTest.action" escapeAmp="false"><s:param name="id" value="123"/></s:url>

这篇关于JQuery Ajax无法在Struts的JSP中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆