问题描述
我有任何应用程序,其中我的 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
:
这个电子邮件类型检查器应该有结果标签吗?
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>
这是我的操作
代码:
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
结果.对于您的情况,我将考虑第一种选择.首先查看this 答案,让您熟悉如何使用 返回
内容类型.然后在 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 检查唯一用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!