Hard work The problem of nearly a week finally solved, so happy
In fact, it's very simple, first set up a map set in Controll.
Private New Hashmap<>(); Public Map<string, object> getData () { return data; } Public void setData (map<string, object> data) { this. Data= data; }
And then write the following code
/*** Check email when registering *@return * @throwsException*/ Publicstring Selectemail () {String a=Us.selectemail (User.getemail ()); if(A.equals ("false") {data.put ("Result", "1"); }Else if(A.equals ("true") {data.put ("Result", "2"); } return"Ajax"; }
Struts China code is as follows:
< Packagename= "Beforeuserinfo"namespace= "/user"extends= "Json-default"> <!--Query the mailbox when registering - <Actionname= "Selectemail"class= "Com.zy.controll.UserControll"Method= "Selectemail"> <resultname= "Ajax"type= "JSON"> <paramname= "root">Data</param> </result> </Action></ Package>
The page code is as follows:
<Divclass= "Mask_content_body"> <formID= "RegForm"Action= "${pagecontext.request.contextpath}/user/insertuser.action"onsubmit= "Javascript:return yz ()"> <H3>New User Registration</H3> <inputtype= "Email"ID= "Regemail"placeholder= "Please enter the mailbox"name= "Email"onblur= "Yzemail ()"> <spanID= "Emailmsg"></span> <inputtype= "Password"ID= "REGPSW"placeholder= "Please enter password"name= "Password"onblur= "YZPW ()"><spanID= "Message1"></span> <inputtype= "Password"ID= "Regpswagain"placeholder= "Please enter your password again"name= "Psw_again"onblur= "yzPW2 ()"><spanID= "Message2"></span> <DivID= "Yzm"class= "Form-inline"> <inputtype= "text"name= "Yzm"ID= "Code_input"value=""style= "width:45%; display:inline-block;"placeholder= "Please enter verification code"> <DivID= "V_container"style= "width:45%;height:40px;float:right;"> </Div> </Div> <inputtype= "Submit"onclick= "return Commitregform ();"value= "Note & #x3000; book"> <%-- <div>${error}</Div>--%><Div><H2ID= "AA"></H2></Div> </form> </Div>
function Yzemail () { var reg4 =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/g; var email = $ ("#regEmail"). Val (); var password = $ ("#regPsw"). Val (); if (!reg4.test (email)) { $ ("#emailMsg"). HTML ("The mailbox is not formatted correctly!"). CSS ("Color", "red"); return false; } $ ("#emailMsg"). HTML (""); $.post ("${pagecontext.request.contextpath}/user/selectemail.action", { "email": $ ("#regEmail"). Val (), }, function (data) { alert (data.result); if (data.result== "1") { $ ("#emailMsg"). HTML ("account already exists"). CSS ("Color", "red"); return false; } }) return true;}
Use of struts and Ajax in SSH