如何在Struts 2中使用jQuery Ajax检查唯一用户? [英] How to check for an unique user with jQuery Ajax in Struts 2?
问题描述
我有任何应用程序,其中我的 email_id
是唯一的,当最终用户输入他的 email_id
时,我已经通过Ajax触发了SQL查询,以检查此 email_id
是否存在.在此之前,它工作正常,但是现在,如果该 email_id
存在,那我想在JSP页面上显示它,例如此ID已被使用".反之亦然.
I'm having any application in which I have email_id
as unique and when the end user enters his email_id
I have fired SQL query through Ajax which check whether this email_id
exists or not. Until here it's working fine but now I what if that email_id
exists then I want to display it on the JSP page like "This id is already taken" and vice-versa.
所以我的问题是如何从Struts应用程序获取响应并将其提供给Ajax或JSP页面,以及在 struts.xml
结果标记中写入什么?
So my question is how to get the response from Struts app and give it to Ajax or JSP page and what to write in struts.xml
result tag?
这是我对 EmailCheck.action
的Ajax调用:
This is my Ajax call to EmailCheck.action
:
$("#email").blur(function() {
var EmailV = $("#email").val();
if(EmailV.length > 10){
$('#Loading').show();
$.get("EmailCheck.action", {
Email_Id: EmailV
}, function(response){
$('#Info').fadeOut();
$('#Loading').hide();
setTimeout("finishAjax('Info', '"+escape(response)+"')", 450);
});
return false;
}
});
这是我的 struts.xml
:
This is my struts.xml
:
此电子邮件类型检查器是否应有结果标签?
Should there be result tag for this Email type checker?
<action name="EmailCheck" class="org.register.customers.EmailCheckAction" method="EmailCheck">
<result name="input">index.jsp</result>
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
这是我的 Action
代码:
This is my Action
code:
public String EmailCheck() throws Exception{
System.out.println(user.getEmail_Id());
boolean create = Check_Email(user);
// "this is my sql call to check the existence of email"
if (true) {
return "input";
} else {
return ERROR;
}
}
推荐答案
如果调用Ajax操作,则可以返回 dispatcher
结果.在这种情况下,渲染的JSP片段将作为响应输出.另外,您还可以选择返回 stream
, json
或 none
结果.对于您的情况,我将考虑第一种选择.首先查看此答案,以使您熟悉如何使用返回
内容类型.然后,在JSP中,您应该创建一个目标div来放置响应. stream
结果text/html
If you call an Ajax action you can return a dispatcher
result. In this case a rendered JSP fragment will output as a response. Also, you have options to return a stream
, json
, or none
results. To your case I will consider the first option. First look at this answer to get you familiar how to return a stream
result with text/html
content type. Then in the JSP you should create a target div where you put the response.
<div id="result"/>
现在修改您的JS以将结果插入div.
now modify your JS to insert result into div.
}, function(response){
$("result").html(response);
这篇关于如何在Struts 2中使用jQuery Ajax检查唯一用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!