我正在尝试使用jQuery ajax功能进行简单的异步Struts2表单提交。但是,无论我尝试什么,jquery都不会被触发。
正常表单提交工作顺利,但我需要一个异步调用,可以从服务器获取结果而无需重新加载页面。
我已经尝试过普通表单,使用JSON插件提交,以及我可以从youtube,internet,stackoverflow等获取的所有其他内容。请帮助,我真的无法理解我做错了什么。
使用jQuery 1.3.2,Struts2:2.3.14
P.S:我第一次使用jQuery,与Struts2进行AJAX调用。
$(document).ready(function() {
$("#updtTslcDL").mousedown(function(){
$.ajax({
type: 'POST',
data: {
tslcText : $('#txtTslcDL').val(),
userid : <% session.getAttribute("userid").toString(); %>,
username : 'c'
},
dataType:'json',
url: 'updateTSLC.wht',
success:function(response){
$('#msgTslcDL').html(response.out);
},
error: function(msg) {
if (msg.statusText != "OK") {
alert("Alert", "Error occured while retriving data.");
}
}
//Finish $.ajax()
});
event.preventDefault();
//Finish $('#updtTslcDL').click()
});
//Finish $(document).ready()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<body>
<div>
<s:form id="frmUpdtTslcDL_s" theme="simple">
<s:textarea id="txtTslcDL" name="tslcText" rows="1" cols="7" value="2222:45"/>
<%-- <s:hidden id="userid" name="userid" value="%{#session.userid}"/>
<s:hidden id="username" name="username" value="%{#session.username}"/> --%>
<s:submit id="updtTslcDL" name = "updtTslcDL" cssClass="button" value="Update" onclick="return false;"/>
</s:form>
<!-- <%--
<form id="frmUpdtTslcDL" action="updateTSLC.wht" method="post">
<div>
<textarea id="txtTslcDL" name="tslcText" rows="1" cols="7">123</textarea>
<input type="hidden" id="iptuserid" name="userid" value=<% session.getAttribute("userid").toString(); %> />
<input type="hidden" id="iptusername" name="username" value=<% session.getAttribute("username").toString(); %> />
<input type="submit" id="iptupdtTslcDL" name = "updtTslcDL" class="button" value="Update"/>
</div>
</form>
--%> -->
<div id="msgTslcDL"/>
</div>
</body>
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.action.extension" value="wht" />
<package name="default" namespace="/" extends="struts-default, json-default">
<action name="validateAuthorization" class="test.ValidateUserAction" method="validateUser">
<result name="success" type="redirect">/test.jsp</result>
<result name="error" type="redirect">/error.jsp</result>
</action>
<action name="updateTSLC" class="test.TestAction" method="updateTSLC">
<result name="success" type="json">
<param name="root">jsonResponse</param>
<param name="location">/test.jsp</param>
</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
行动类:
package test;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request = null;
private String tslcText;
private String userid;
private String username;
private Map<String, String> jsonResponse=new HashMap<String, String>();
public String updateTSLC(){
System.out.println(getTslcText()+getUserid()+getUsername());
jsonResponse.put("out", "test: "+getTslcText()+", userid: "+getUserid()+", username: "+getUsername()+"_response");
return SUCCESS;
}
public String getTslcText() {
return tslcText;
}
public void setTslcText(String tslcText) {
this.tslcText = tslcText;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Map<String, String> getJsonResponse() {
return jsonResponse;
}
public void setJsonResponse(Map<String, String> jsonResponse) {
this.jsonResponse = jsonResponse;
}
@Override
public void setServletRequest(HttpServletRequest inRequest) {
request = inRequest;
}
}
答案 0 :(得分:1)
最后 ,它正在努力进行一些进一步的更改,尽管我昨天已经尝试过这些。不知道我放在那里的额外费用,但是OMG现在正在工作。
<% session.getAttribute("userid").toString(); %>
的ajax调用中替换了scriptlet $('#userid').val()
。 [由于某种原因,使用scriptlet导致页面重定向]。$("#updtTslcDL").mousedown(
替换为$('#frmUpdtTslcDL').submit(
。