Today to do the struts2 ajax validate input regarding the existence of the function, and now share with everyone, I hope everyone many opinions!!!
input.jsp page code:
<html>
<body>
<s:form action="" method="post" theme="simple">
<tr>
<td height="35" bgcolor="#FFFFFF" align="left">
<span> * </ span> title
</ td>
<td bgcolor="#FFFFFF">
<s: textfield name = "title" maxlength = "50"
<span> (1) / / authentication back to display the results of existence, or you can use <a href="#"> <u> can be used to verify whether the title </ u>
</ a>
</ span>
</ td>
</ tr>
</ s: form>
<script>
function validteTitle () (
if (document.getElementById ( "title"). value == "") (
alert ( "Please enter a title");
document.forms [ "form1"]. title.focus ();
return false;
)
document.forms [ "form2"]. title.value = document.forms [ "form1"]. title.value; (2) / / under the current form of the text box value from where imported proven document.forms [ " form2 "]. submit ();
)
</ script>
<s:form action="examinTitle" target="frame1"> (3) / / verification form
<input type="hidden" name="title">
</ s: form>
<iframe name="frame1" height="0" src="about:blank"> </ iframe> (4) / / verification results will be back to do a pop-up to iframe
</ body>
</ html>
action code:
public class examinTitleAction extends ActionSupport (
private String title; (5) / / struts2 jsp page tags from the value of
public void getTitle () (
return title;
)
public String setTitle (String title) (
this.title = title;
)
public String examinTitle () (
String res = "";
int num = service.examinCopyTitle (title); / / call the query method, whether the conditions exist if (num == 0) (
res = "success"; / / if does not exist, mark the successful return) else (
res = "nosuccess"; / / if it exists, return unsuccessful logo)
return res;
)
)
struts.xml code:
<action name="examinTitle" method="examinTitle">
<result name="success"> / examin.jsp
</ result>
<result name="nosuccess"> / examinNo.jsp
</ result>
</ action>
examin.jsp code:
<% @ Page language = "java" contentType = "text / html; charset = GBK" pageEncoding = "GBK"%>
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<script language="JavaScript">
<! --
parent.document.getElementById ( "checkTitle"). innerHTML = (6) / / and (1) correspond to display return value
"<font color='#FF0000'> title already exists </ font>";
parent.document.forms [ "form1"]. subject.focus ();
//-->
</ script>
examinNo.jsp code:
<% @ Page language = "java" contentType = "text / html; charset = GBK" pageEncoding = "GBK"%>
<% @ Taglib prefix = "s" uri = "/ struts-tags"%>
<script language="JavaScript">
<! --
if (parent.document.forms [ "form1"]. flag.value == false) (
) else (
parent.document.getElementById ( "checkTitle"). innerHTML = (6) / / and (1) correspond to display return value
"<font color='#00FF00'> can use the title </ font>";
)
//-->
</ script>
Note: struts2 ajax authentication: form to specify theme = "simple" property to work, or could not be verified







