Easy to use----struts2.1 and Ajax JSON __JS

Source: Internet
Author: User
Tags stringbuffer
One: Effect preview Description: There is no layout to do any style, just a simple implementation of the function only 1 username and password are null:
2 username and password is incorrect:
3 username and password entered correctly:

Second: Code Demo:1) Project Catalog Demo:


2 Action code (in order to facilitate the user name and password is fixed written dead, do not have access to the database, you can connect the database as required to do the user's famous school inspection):
Package com.test.action;
Import Com.opensymphony.xwork2.ActionSupport;

Import Com.test.bean.AjaxBean;
	public class Ajaxloginaction extends actionsupport{private String method;
	Private String info;
	Private Ajaxbean AB;
		public string Execute () {string url = ' fail ';
			try{if ("Login". Equals (This.getmethod ())) {Url=this.login ();
		}}catch (Exception err) {err.printstacktrace ();
	} return URL; /** * JSON Login Test * @return String */private string login () {//system.out.println ("name and pass11111==========
		=========>>>>>>> "+ab.getusername () +" "+ab.getpassword ()); if ((Ab.getusername () ==null | | ab.getusername (). Equals (")") | | (Ab.getpassword () ==null | | Ab.getpassword (). Equals (""))
		{this.setinfo ("User name or password is blank, please fill in username or password!"); else if (!ab.getusername (). Equals ("Wzh") | | |!ab.getpassword (). Equals ("123")) {This.setinfo ("username or password entered incorrectly, Please enter the correct username and password.
		"); }else{This.setinfo ("Landing success".
		");
	Return to "loginsuccess"; } public String GetmetHod () {return method;
	public void Setmethod (String method) {This.method = method;
	Public String GetInfo () {return info;
	public void SetInfo (String info) {this.info = info;
	Public Ajaxbean Getab () {return AB;
	public void Setab (Ajaxbean ab) {this.ab = AB;
 }
	
	
}
3 entity Class Bean code (in order to conveniently place the username and password two parameters into the original entity class, here only username and password two .):
Package Com.test.bean;

Import java.io.Serializable;
	public class Ajaxbean implements Serializable {private String ID;
	Private String SFDM;
	Private String SFLBBH;
	Private String YWMC;
	Private String XZ;
	Private String rksj;
	Private String GXSJ;
	Private String ZT;
	Private String fplb_id;
	Private String Zdyflag;
	Private String MSFBZ;
	Private String BM;
	Private String Jmbz;
	
	Private String SFBZ;
	Private String username;
	private String password; Assembly SQL statement Public String Getwhere () {//TODO auto-generated method stub StringBuffer sql = new StringBuffer ("WHERE 1
		=1 "); if (sfdm!=null&&! "".
		Equals (Sfdm.trim ())) {Sql.append ("and SFDM = '" +sfdm+ "); }if (ywmc!=null&&! "".
		Equals (Ywmc.trim ())) {Sql.append ("and ywmc like '%" +ywmc+ "%");
		
	return sql.tostring ();
	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 GetId () {return id;
	public void SetId (String id) {this.id = ID;
	Public String GETSFDM () {return SFDM;
	} public void SETSFDM (String sfdm) {THIS.SFDM = SFDM;
	Public String GETSFLBBH () {return SFLBBH;
	} public void Setsflbbh (String sflbbh) {this.sflbbh = SFLBBH;
	Public String GETYWMC () {return YWMC;
	} public void Setywmc (String ywmc) {THIS.YWMC = YWMC;
	Public String Getxz () {return XZ;
	public void Setxz (String xz) {this.xz = XZ;
	Public String getrksj () {return rksj;
	} public void Setrksj (String rksj) {this.rksj = RKSJ;
	Public String GETGXSJ () {return GXSJ;
	} public void Setgxsj (String gxsj) {this.gxsj = GXSJ;
	Public String getzt () {return zt;
	public void setzt (String zt) {this.zt = ZT;
	Public String getfplb_id () {return fplb_id; } public void setfplb_id (String fplb_id) {THIS.FPLb_id = fplb_id;
	Public String Getzdyflag () {return zdyflag;
	} public void Setzdyflag (String zdyflag) {this.zdyflag = Zdyflag;
	Public String Getmsfbz () {return msfbz;
	} public void Setmsfbz (String msfbz) {this.msfbz = MSFBZ;
	Public String GETBM () {return BM;
	public void SETBM (String bm) {THIS.BM = BM;
	Public String Getjmbz () {return jmbz;
	} public void Setjmbz (String jmbz) {this.jmbz = JMBZ;
	Public String Getsfbz () {return sfbz;
	} public void Setsfbz (String sfbz) {this.sfbz = SFBZ;
 }
	
	
}

4 struts.xml configuration file (here I used the introduction of the configuration file Struts-system.xml, just look at the configuration of Ajax JSON below
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
    "-//apache Software foundation//dtd struts Configuration 2.1.7//en"
    "http:// Struts.apache.org/dtds/struts-2.1.7.dtd ">
<struts>
	<package name=" Ajaxtestwzh "namespace="/" extends= "Global" >	
		<action name= "ajaxtest" class= "ajaxaction" >
			<result name= "selectdatasuccess ">/WEB-INF/jsp/ajaxTestList.jsp</result>
		</action>
	</package>
	
	<!--using Ajax The configuration of the JSON asynchronous interaction test begins-->
	<package name= "Default" namespace= "/" extends= "Json-default" ><!--Notice the configuration of the package name-- >
        <action name= "loginjsontest" class= "loginjsonaction" >  
            <!--Returns the result of a single value-->  
            < Result name= "loginsuccess" type= "JSON" ></result>  
        </action>  
    </package>  
    <!- -End of configuration of asynchronous interactive test using AJAX JSON-->
</struts>

5 applicationcontext.xml configuration file (see only the bottom of the Ajax JSON configuration)
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "> <!--data source configuration--> <bean id=" Sfdatasource "class=" Org.apache.commons.dbcp.BasicDataSo
		Urce "destroy-method=" Close "> <property name=" driverclassname "value=" Oracle.jdbc.driver.OracleDriver "> </property> <property name= "url" value= "JDBC:ORACLE:THIN:@192.168.1.8:1521:ORCL" > </property> & Lt;property name= "username" value= "azc2012" ></property> <property name= "password" value= "azc2012" > </property> <property name= "maxactive" value= "></property> <property name=" Maxidle "value=" ></property> <property name= "maxwait" value= "1000" ></property> <property name= "removeabandoned" value= "true" ></property> <property name= " Removeabandonedtimeout "value=" 360 ></property> <property name= "logabandoned" value= "true" ></ property> <property name= "Defaultautocommit" value= "true" ></property> </bean> <bean id= " Ajaxjdbctemplate "class=" Org.springframework.jdbc.core.JdbcTemplate "abstract= false" lazy-init= "default" Autowire
           = "Default" > <property name= "DataSource" > <ref local= "Sfdatasource"/> </property> </bean> <!--test configuration started--> <bean id= "ajaxaction" class= "Com.test.action.AjaxTest" Scope= "Prototype" > <property name= "asservice" ref= "Asservice" ></property> </bean> <bean id= " Asservice "class=" Com.test.service.impl.AjaxServiceImpl "scope=" prototype "> <property name=" Addao "ref=" Addao "></property> </bean> <bean id=" Addao "class=" com. Test.dao.AjaxDao "scope=" prototype "> <property name=" ajaxjdbctemplate "ref=" Ajaxjdbctemplate "></
	 																		property><!--here if you use ajaxjdbctemplate, you need to generate its get and set methods at the DAO layer, and do not inherit jdbcdaosupport this class; If you directly use <property name= "DataSource" ref= "DataSource" ></property> Then you do not have to inherit directly from the DAO layer Jdbcdaosupport This class calls the class to access the database--> </bean> <!--test configuration end--> <!--Ajax JSON configuration started
	; <bean id= "loginjsonaction" class= "Com.test.action.AjaxLoginAction" scope= "prototype" > </bean> <!--


 Ajax JSON configuration End--> </beans>
6 login.jsp page (the need to introduce Jquery.js and Login.js (their own written JS))
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% @taglib uri= "http://java.sun.com/ Jstl/core_rt "prefix=" C "%> <%@ taglib prefix=" s "uri="/struts-tags "%> <% String Path = Request.getcontextpat
H ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
    %>  

7) login.js code:
function login () {
    var username=$ ("#usernameId"). Val ();
	var userpass=$ ("#passId"). Val ();
	$.getjson ("loginjsontest.action?method=login&ab.username=" +username+ "&ab.password=" +userpass,function ( Data) { 
	//$.getjson ("Loginjsontest.action?method=login", "Ab.username=" +username+ "&ab.password=" + Userpass,function (data) {//Another argument form//
   	pass. Operator can obtain the value of info in action  
  	 $ ("#message") from Data.message. HTML ("<font color= ' red ' > ' +data.info+ "</font>");}  

























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.