Tianyi 28 -- simple use of struts2.1 and Ajax JSON

Source: Internet
Author: User
I. effect Preview Note: No style la s are made here, just simple implementation functions. 1) the username and password are empty:
2) incorrect user name and password:
3) enter the correct user name and password:

Ii. Code Demo: 1) project directory Demo:

2) Action Code (for convenience, the user name and password are fixed and are not accessed to the database, you can connect to the database for user name verification as needed ):
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 Str Ing */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 ("the user name or password is blank. Enter the user name or password! ");} Else if (! AB. GetUserName (). Equals ("wzh") |! AB. GetPassword (). Equals ("123") {This. setinfo ("incorrect user name or password. Please enter the correct user name and password! ");} Else {This. setinfo (" Login successful! ");} Return" 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 (to facilitate the put of the username and password parameters in the original entity class, onlyUsername and password.):

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; // assemble the SQL statement Public String getwhere () {// todo auto-generated method stubstringbuffer 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 (the imported configuration file is used here.Struts-system.xml, here only look at the following Ajax JSON configuration can be)

<? 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> <! -- Start with Ajax JSON Asynchronous interaction test configuration --> <package name = "default" namespace = "/" extends = "JSON-Default"> <! -- Pay attention to the package name configuration --> <action name = "loginjsontest" class = "loginjsonaction"> <! -- Return the result of a single value --> <result name = "loginsuccess" type = "JSON"> </result> </Action> </package> <! -- The configuration of Asynchronous interaction test with Ajax JSON is complete --> </struts>

5) applicationcontext. xml configuration file (just look at the configuration of the bottom Ajax JSON)

<? XML version = "1.0" encoding = "UTF-8"?> <Beansxmlns = "http://www.springframework.org/schema/beans" 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. basicdatasource "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> <property name =" username "value =" azc2012 "> </property> <property name =" password "value =" azc2012 "> </Property> <property name = "maxactive" V Alue = "300"> </property> <property name = "maxidle" value = "20"> </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. SPR Ingframework. JDBC. core. jdbctemplate "abstract =" false "lazy-init =" default "autowire =" default "> <property name =" datasource "> <ref local =" sfdatasource "/> </Property> </bean> <! -- Test configuration start --> <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 =" ajaxjdbctemp Late "> </property> <! -- If you use ajaxjdbctemplate, You need to generate its get and set methods at the DaO layer, and do not inherit the jdbcdaosupport class; if you directly use <property name = "datasource" ref = "datasource"> </property>, you do not need to declare at the DaO layer to directly inherit the jdbcdaosupport class and call the method for this class to access the database --> </bean> <! -- Test configuration ended --> <! -- Ajax JSON configuration start --> <bean id = "loginjsonaction" class = "com. Test. Action. ajaxloginaction" Scope = "prototype"> </bean> <! -- Ajax JSON configuration ends --> </beans>

6) login. jsp page (jquery. js and login. js (self-written JS) need to be introduced ))

<% @ 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. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <HTML> 

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 form of passing parameters // pass. operators can be retrieved from data. get the value of info in action in message $ ("# 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.