Struts2 + ajax + json enables user login, struts2json

Source: Internet
Author: User

Struts2 + ajax + json enables user login, struts2json

The asynchronous login refresh function is implemented. When an error message is returned, the new page is not refreshed.

Front-end code:

<% @ Page language = "java" contentType = "text/html; charset = GBK" pageEncoding = "GBK" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

The struts. xml file is configured as follows:

<Action name = "login" class = "com.cn. useraction. LoginAction" method = "login"> <! -- Return json data --> <result name = "success" type = "json"> <param name = "root"> result <! -- Result is the variable name set in action and the data to be returned from the page, the variable must have the setter and getter methods --> </param> </result> <result name = "UserNotExist" type = "json"> <param name = "root"> result <! -- Result is the variable name set in action and the data to be returned from the page, the variable must have the setter and getter methods --> </param> </result> <result name = "error" type = "json"> <param name = "root"> result <! -- Result is the variable name set in action and the data to be returned on the page. The variable must have the setter and getter methods --> </param> </result> </action>

In addition, json information must be added to the package label:

<package name="useraction" extends="struts-default,json-default">

In addition, the packages used for import:


Note: In jsp, the url is filled with the action name, and different values returned by the action are used to determine what work is to be done on the front end.

The action Code is as follows:

Package com.cn. useraction; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. hashMap; import java. util. map; import net. sf. json. JSONObject; import com.cn. util. DBConnection; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; public class LoginAction extends ActionSupport {private Static final long serialVersionUID = 1L; private String result; private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getResult () {return result;} public void s EtResult (String result) {this. result = result;}/*** check the username and password matching ** @ return result ID String success indicates that the matching is successful. UserNotExist indicates that the user does not have an error and the matching fails */private String userCheck () {String name = null; String pass = null; Connection conn = DBConnection. getConn (); String SQL = "select * from users where username = '" + username + "'"; PreparedStatement pstmt; try {pstmt = conn. prepareStatement (SQL); ResultSet rs = pstmt. e XecuteQuery (); while (rs. next () {name = rs. getString ("username"); pass = rs. getString ("password"); ActionContext. getContext (). getSession (). put ("userflag", rs. getString ("flag");} rs. close (); pstmt. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();} if (name = null) // verify whether the user name has returned "UserNotExist"; else if (name. equals (username) & pass. equals (password) // verify that the logon user name matches the password. return "success "; Elsereturn" error ";}/*** handle ajax requests * @ return SUCCESS */public String login () throws Exception {if (userCheck (). equals ("success") {ActionContext. getContext (). getSession (). put ("username", username); // store the data in the map and convert it to json data. You can also manually construct a json Data Map <String, object> map = new HashMap <String, Object> (); map. put ("success", "Login successful! "); JSONObject json = JSONObject. fromObject (map); // converts a map object to a json-type data result = json. toString (); // assign a value to result and pass it to return "success";} else if (userCheck (). equals ("UserNotExist") {// store the data in the map and convert it to json data. You can also manually construct a json Data Map <String, object> map = new HashMap <String, Object> (); map. put ("error", "Logon Failed: the user name does not exist or the user name is empty! "); JSONObject json = JSONObject. fromObject (map); // converts a map object to a json-type data result = json. toString (); // assign a value to the result and pass it to the return "UserNotExist";} else {// store the data in the map and convert it to json data, you can also manually construct the json Data Map <String, Object> map = new HashMap <String, Object> (); map. put ("error", "Logon Failed: Incorrect password or unknown exception! "); JSONObject json = JSONObject. fromObject (map); // converts a map object to a json-type data result = json. toString (); // assign a value to the result and pass it to the return "error" ;}} page ";}}}

Finished receiving!

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.