Struts2 + jquery implement ajax login, struts2jquery

Source: Internet
Author: User

Struts2 + jquery implement ajax login, struts2jquery

1. Create a web Project: test, configure the struts2 environment (for detailed configuration, see: http://www.cnblogs.com/wuweidu/p/3841297.html)

Import Jquery js files to the Project

2. Create a loginAction. java file under the com. action package.

 

The loginAction. java code is as follows:

Package com. action; import org. apache. struts2.convention. annotation. action; import org. apache. struts2.convention. annotation. parentPackage; import org. apache. struts2.convention. annotation. result; import org. apache. struts2.convention. annotation. results; import com. opensymphony. xwork2.ActionSupport; @ Action ("login") @ ParentPackage (value = "json-default") @ Results ({@ Result (name = "success", type = "json ", params = {"data", "flag"}),}) public class LoginAction extends ActionSupport {/*****/private static final long serialVersionUID = 1751244794407005783L; private String flag; private String username; private String password; public String execute () {try {if (getUsername () = null | getUsername (). trim (). equals ("") {setFlag ("username cannot be blank"); return SUCCESS;} else if (getPassword () = null | getPassword (). trim (). equals ("") {setFlag ("password cannot be blank"); return SUCCESS;} else if (getUsername (). trim (). equals ("admin") & getPassword (). equals ("admin") {setFlag ("Login successful"); return SUCCESS;} else {setFlag ("username or password error"); return SUCCESS ;}} catch (Exception e) {e. printStackTrace (); setFlag ("Logon exception"); return SUCCESS ;}} public String getFlag () {return flag;} public void setFlag (String flag) {this. flag = flag;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username ;}}

3. Create index. jsp and success. jsp In the WebRoot directory

Index. jsp login interface success. jsp jump interface after successful login

The content of index. jsp is as follows:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> Alert (value );}}); </script> <body> <input name = "username" id = "username" type = "text"/> <input name = "password" id = "password" type = "password"/> <button class = "loginButton" class = "btn-inverse"> login </button> </body> 

 


I sincerely share the method for submitting forms through ajax using Struts2 + JQuery !!

Example:

<! Doctype html public "-// W3C // dtd html 4.01 // EN" "www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<Script src = "jquery. js"> </script>
<Script>
Function sub (){
Var value1 = $ ("# id1"). val ();
Var value2 = $ ("# id2"). val ();
If (value1! = "" & Value2! = ""){
$. Ajax ({
Url: 'A. action ',
Type: 'post ',
Data: 'value1 = '+ value1 +' & value2 = '+ value2
// Data: {value1: value1, value2: value2 },
DataType: 'text ',
Success: function (text ){
Alert (text );
}
})
}
}
</Script>
</Head>
<Body>
<Form id = "form1" name = "form1">
<Input type = "text" id = "id1"/> <br/>
<Input type = "text" id = "id2"/> <br/>
<Input type = "button" value = "Submit" onclick = "sub ();"/>
</Form>
</Body>
</Html>

Struts. xml:

<Action name = "a" class = "com. test. a. action" method = "test">

</Action>

A. action:

Private String value1;
Private String value2;
Private String text;

Get ()... set ()...

Public void test () throws Exception {
// Do somethings
HttpServl ...... remaining full text>

SSH struts2 + spring + hibernate how to use ajax for login

It is the same as the normal ajax upload background!
It's just a little different from the return value in struts2 to the foreground.
Here is a simple ajax example implemented by JQuery (you need to introduce the JQuery core jar. If you do not understand it, You can Baidu Hi me ):
① Lib: Introduce struts2-json-plugin-2.2.3.1.jar
② Struts2 configuration file: Create a package to inherit the json-default
(PS: ① is the value in json format returned by struts2)
③ Page js:
$ (Function (){
$ ("# Tj"). click (function (){
Var params = $ ("# entAddForm"). serialize (); // The passed Parameter
$. Ajax ({
Type: "POST ",
Url: "jsonAdvAction! FlexTable. action ", // the action you want to submit
Data: params,
DataType: "text ",
Success: function (json) {// return the value in json format, which is parsed
Var obj = $. parseJSON (json );
Var state_value = obj. gS_Result;
Var state_null = obj. gS_Null;
If (state_value = "success "){
If (state_null = "nulls "){
Alert ("No data is found... ");
}
Parent. $ ("# flexTable"). flexReload ();
Parent. $ (). closeIframe ();
} Else {
Alert ("an error occurred, json =" + json );
}
// Parent. $ ("# flexTable"). flexReload ();
// Parent. $ (). closeIframe ();
},
Error: function (json ){
Alert ("json =" + json );
Return false;
}
});
});
});

Good luck! If you have any questions, you can ask Baidu Hi me !~... Remaining full text>

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.