SSH + Jquery + Ajax framework integration, sshjqueryajax framework

Source: Internet
Author: User

SSH + Jquery + Ajax framework integration, sshjqueryajax framework

I recently learned how to integrate SSH2 (Struts2 + Spring + Hibernate) and started to write a login interface. The result showed that if struts2 is used only for page Jump, the page would not work very well, at the same time, local refreshing cannot be performed (that is, asynchronous submission verification ).

As a result, I began to search for solutions on the Internet, some of which were achieved through a hidden iframe. When I thought it was troublesome and not practical. Later, I asked my teacher and told me that ajax can achieve the desired effect. I also found that many examples on the Internet are ajax, but what is missing is SSH2 (integrated) integration with ajax (ajax uses the jQuery framework ).

Let's talk about what I want:

Click Submit on the logon page to perform background verification;

The verification is successfully redirected to index. Jsp;

If verification fails, a jQuery script is executed on the logon page to prompt the user;

Required packages:

In addition to the packages used by SSH2, the following packages of struts2 are also required:

Commons-beanutils-1.8.0.jar
Commons-collections-3.1.jar
Commons-fileupload-1.3.1.jar
Commons-io-2.2.jar
Commons-lang-2.4.jar
Commons-lang3-3.1.jar
Commons-logging-1.1.3.jar
Ezmorph-1.0.6.jar
Freemarker-2.3.19.jar
Json-lib-2.3-jdk15.jar
Ognl-3.0.6.jar
Struts2-core-2.3.16.3.jar
Struts2-json-plugin-2.3.16.3.jar
Xwork-core-2.3.16.3.jar

Now, start to paste the Code:

Login. jsp

<Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "/> <title> member logon </title> <! -- JQuery must be added; otherwise, ajax will not be enabled. --> <Script type = "text/javascript" src = "js/jquery. js "> </script> <script type =" text/javascript "> $ (document ). ready (function () {$ ("# button "). click (function () {varuser = $ ("# User "). val (); varpassWord = $ ("# PassWord "). val (); $. ajax ({// Struts is required here. the name attribute of <action/> in xml is consistent. Url: 'login. action ', // submission type: 'post', // submit data to the Action to input data: {'user': User, 'Password': PassWord }, // The returned data type dataType: 'json'. // The call method success: function (data) is successful. {// obtain the data returned by the Action using data. get if (data. result = "false") {alert ("Account Password error");} elseif (data. result = "true") {// page Jump, because ajax our Action only returns data, not jump... location. href = "index. jsp ";}}}) ;}); </script> 

Account: <input id = "User" name = "User" type = "text"/> <br/>

PassWord: <input id = "PassWord" name = "password" type = "PassWord"/> <br/>

<Input id = "button" type = "button" value = "Submit"/> </body> 

LoginAction. java

Publicclass LoginAction extends ActionSupport {// use the @ Resource annotation to inject condition attribute names consistent with ref to @ Resource FUserService fUserServiceImp; @ Resource FUser fUser; private String User; private String PassWord; private String result; public String getResult () {returnresult;} publicvoid setUser (String User) {this. user = User;} publicvoid setPassWord (String PassWord) {this. passWord = PassWord;} public String execu Te () throws Exception {HttpServletRequest request = ServletActionContext. getRequest (); // you can use the foreground attribute name to obtain the data transmitted through ajax. FUser. setUserEmail (User); fUser. setUserPassWord (PassWord); if (fUserServiceImp. CheckUser (fUser )! = Null) {// The data returned to ajax. this. result = "true" ;}else {this. result = "false" ;}return "success ";}}

Struts. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Struts> <! -This is the Struts package we configured during SSH --> <package name = "Struts" extends = "struts-default"> <action name = "login" class = "loginAction"> </action> </package> <! -To enable ajax to call actions in Spring to configure ajax package --> <package name = "ajax_json" extends = "json-default"> <! -The id in this section is the value of the ajax url, class references the id of the Action configured by Spring --> <action name = "login" class = "loginAction"> <result name = "success" type = "json"/> </action> </package> </struts>

The integration of other SSH2 frameworks does not need to be changed so that ajax of the jQuery framework can be integrated into the SSH2 framework for use!

The above content is what I personally think. If you have any questions, please leave a message for me. I will contact you as soon as possible. Thank you for your constant support for the help House website.

Related Article

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.