使用AJAX从JSP页面调用Action类
Call Action class from JSP page using AJAX
我正试图通过单击按钮刷新textarea
来从JSP中调用我的操作类。在调试模式下,我不会碰到我的操作类。
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>
行动类别:
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
用于映射操作,但它在操作类中不存在。
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>
相关文章:
- 调用laravel{{action(Controller@method}}通过传递vue.js数组中的变量
- 从javascript调用Rails Controller/action提交表单
- Alt flux action:_this.actions未定义,即使调用了this.generateActions
- Rails4:从Javascript调用的Action;t请参阅模型关联(NoMethodError)
- 调用Action Method,关闭对话框并刷新父项
- 可以't设法调用mojarra.ab(this,evt,'action',false,false
- 从 MVC Action 方法调用 jquery 函数
- 如何使用javascript调用Struts2 Action Class方法中的方法
- 通过从Javascript调用Action Method来更新视图
- 在MVC4中使用Ajax调用Action方法
- 对象不支持IE中的属性或方法,从javascript调用Action脚本函数,asp.net
- 基于DropDownList的值和模型调用Action方法
- asp.net MVC应用从javascript调用action方法时的安全问题
- 使用javascript调用Action
- 加载模板时调用action函数(Ember.js)
- MVC 3在JavaScript中从视图调用Action方法
- iframe在设置src属性时调用Action方法两次
- jQuery:在调用preventDefault()之后调用action's表单
- 使用AJAX从JSP页面调用Action类
- asp.net mvc 3 -从javascript调用Action方法