Struts2 combined with ajax to achieve no refreshing Login

Source: Internet
Author: User

1,

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! -- You must add the struts2-dojo-plugin-corresponding version. jar to use Ajax --> <% @ taglib prefix = "SX" uri = "/Struts-Dojo-tags" %> <HTML> <! -- Must be added to the struts2-dojo-plugin-corresponding version. jar can use Ajax --> <s: Head theme = "XHTML"/> <SX: head parsecontent = "true" DEBUG = "true"/> <title> Huisheng Management System </title> <body> <a href = "action. JSP "> client API </a> <br> <Div id =" logindiv "> <Div id =" errordiv "> </div> <s: form name = "divloginform" id = "divloginform" Action = "login_admin" theme = "XHTML"> <s: label Label = "Enter your username and password"> </s: label> <s: textfield name = "Admin. name "label =" Account "value =" admin" /> <S: password name = "Admin. Password" label = "password" value = "admin"/> <! -- Sx executescripts --> <SX: Submit value = "login" formid = "divloginform" targets = "loginsuccessdiv" executescripts = "true"/> </s: form> </div> <Div id = "loginsuccessdiv" class = "Div" style = "display: none;"> </div> </body> 

2 'after the action is submitted, assign the corresponding value session and request

public String login() {        if(iAdminService.validate(admin.getName(), admin.getPassword())!=0){            getRequest().getSession(true).setAttribute("name", admin.getName());            getRequest().setAttribute("status", "success");        }else {            getRequest().setAttribute("status", "failed");        }        return "script";    }

3. scipt. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <s: bean id = "date" name = "Java. util. date "/> <s: If test =" % {# request. status = 'success'} "> welcome, <s: property value =" # session. name "/>. logon Time: <s: Date name = "% {# date}"/> <s: URL action = "logout_admin" id = "divlogouturl"> </s: URL> <s: a href = "% {# divlogouturl}" theme = "XHTML"> deregister </s: A> <SCRIPT> // set error to an empty document. get Elementbyid ('errordiv '). innerhtml = ''; // hide the document in the logon box. getelementbyid ('logindiv '). style. display = 'none'; // display the successful information document. getelementbyid ('loginsuccessdiv '). style. display = ''; </SCRIPT> </S: If> <s: elseif test =" % {# request. status = 'failed'} "> <SCRIPT> document. getelementbyid ('errordiv '). innerhtml = "the password you entered does not match the account name. Please enter it again. "; </SCRIPT> </S: elseif> <s: else> <SCRIPT> // empty the successful information. Document. getelementbyid ('loginsuccessdiv '). innerhtml = ''; // The document is displayed in the login box. getelementbyid ('logindiv '). innerhtml = ''; // hide the successful information document. getelementbyid ('loginsuccessdiv '). style. display = 'none'; </SCRIPT> </S: else>

Done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.