简体   繁体   English

JQuery Ajax 不能在带有 Struts 的 JSP 中工作

[英]JQuery Ajax not working in JSP with Struts

I am trying to create a web app demo with the feature of Ajax.我正在尝试使用 Ajax 功能创建一个 Web 应用程序演示。 However, the Ajax seems not working using JQuery.但是,Ajax 似乎无法使用 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.但是,单击按钮后没有任何反应。

Maybe there is something wrong in the Ajax parameter?也许 Ajax 参数有问题? I am still not clear how to pass variable from Ajax to action and another way around.我仍然不清楚如何将变量从 Ajax 传递到 action 以及另一种方法。

JQuery:查询:

<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 TAG: HTML 标签:

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

Action : Action

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 to action an all way around passing parameters instead of json and return json results. Ajax 一直在传递参数而不是 json 并返回 json 结果。 You could use Struts2 JQuery plugin along with Struts2 JSON plugin .您可以将Struts2 JQuery 插件Struts2 JSON 插件一起使用。 It has tags that you can use in JSP to generate HTML and jQuery content when the page is rendered.它有一些标签,您可以在 JSP 中使用这些标签在呈现页面时生成 HTML 和 jQuery 内容。 This way is preferable because you need to write less javascript code and get less errors doing so.这种方式更可取,因为您需要编写更少的javascript 代码并减少错误。 For example a basic ajax call例如一个基本的ajax调用

<%@ 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>

There you can add parameters to the URL在那里您可以向 URL 添加参数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM