使用 AJAX 从 JSP 页面调用操作 class

Call Action class from JSP page using AJAX

我正在尝试通过单击按钮刷新 textarea 从我的 JSP 中执行我的操作 class。在调试模式下,我没有执行操作 class.

JSP 页数:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<sj:head/>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head>
<body>
    <s:form name="defineFileForm" action="selectDefine">
        <table class="formTable formContent">
            <tbody>

                <fieldset>
                    <legend>Choose the file type that best describes your data</legend>
                    
                    <label for="defineStartRowline">Start Data Preview at Row:</label>
                    <input id="defineStartRowline"  name="defineStartRowline" size="20" maxlength="50" style="visibility : visible" type="text" value="<s:property value='defineStartRowline'/>"/>
                    <input type="button" value="Refresh Data Preview" onclick="refreshTextArea()"/>
                    
                </fieldset>
                
                <fieldset>
                <legend>Data Preview</legend>
                
                <textarea rows="30" cols="50" wrap="off" readonly value="<s:property value='defineTextArea'/>"></textarea>
                
                </fieldset>

            </tbody>
        </table>
    </s:form>

    <script language="JavaScript">
        
        function refreshTextArea(){
            
            alert('Inside ajax call')
            
            var rowNumber = $('#defineStartRowline').val();
            
            alert(rowNumber)
            
            $.ajax({
                type : "GET",
                url: '<s:url namespace="/" action="ajaxRefreshDefineTextArea"/>',
                dataType : 'json',
                data : {'defineStartRowline' : rowNumber},
                success : function(result){
                  if (result != null && result.length > 0){
                    $("defineStartRowline").val() = result.defineStartRowline;
                  }
                },
                error : function(xhr, errmsg) {alert("Nothing found!!");}
            });    
        }
    </script>
</body>
</html>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="fileimport" namespace="/" extends="struts-default, json-default">
      <action name="ajaxRefreshDefineTextArea" 
          class="com.Files.fileimport.FileImportAction" 
          method="loadDefineScreen">
            <result name="success" type="json">
                <param name="root">jsonData</param>
            </result> 
      </action>
        
   </package>
</struts>

动作Class:

public class FileImportAction extends ActionSupport implements SessionAware{


private String defineStartRowline;
private String defineTextArea;

public String getDefineStartRowline() {
    return defineStartRowline;
}

public void setDefineStartRowline(String defineStartRowline) {
    this.defineStartRowline = defineStartRowline;
}

public String getDefineTextArea() {
    return defineTextArea;
}

public void setDefineTextArea(String defineTextArea) {
    this.defineTextArea = defineTextArea;
}

public String RefreshDefineTextArea()
{
   String tempRowNumber = defineStartRowline;
   
   if(!tempRowNumber.equals(""))
   {
       defineTextArea = "Hello There m8!";
   }
   
   
   return "success";
 }

}

我收到 2 个警报,然后我收到 "Nothing found..!! 警报。知道我遗漏或忽略了什么吗?

在Struts2中动作被映射到方法,在配置中方法loadDefineScreen用于映射动作,但在动作class中没有。

javascript有错误,Struts返回的json对象不是数组,所以没有length属性。结果中的 root 参数无效,要获取操作的 属性,您需要删除此参数或使用 #action 值。默认情况下使用此参数。

<result name="success" type="json"/>      

jQuery val() 不带参数的函数不能用于将值设置为元素。在参考站点上检查正确的语法。

你的url应该只有

url: 'ajaxRefreshDefineTextArea',

<script language="JavaScript">

        function refreshTextArea(){

            alert('Inside ajax call')

            var rowNumber = $('#defineStartRowline').val();

            alert(rowNumber)

            $.ajax({
                type : "GET",
                url: "ajaxRefreshDefineTextArea",
                dataType : 'json',
                data : {'defineStartRowline' : rowNumber},
                success : function(result){
                  if (result != null && result.length > 0){
                    $("defineStartRowline").val() = result.defineStartRowline;
                  }
                },
                error : function(xhr, errmsg) {alert("Nothing found!!");}
            });    
        }
    </script>