Objective:
As an Android developer, the network data are provided by web developers, each time they write an interface like praying, rather than ask others to do their own, this is written 800 years ago word, today put it in csdn, to prevent later forget can also see.
1. the data source for the interface comes from the database . (There is no way to provide methods to teach you how to check the data, go to the Web site to go to school, I think it is very good)
Http://www.w3school.com.cn/sql/sql_wildcards.asp
2. The framework of the interface I write is structs+spring+ibatis, to be able to interface first to learn how these three frameworks are used. Here is a diagram of the Web project structure that Myecplise developed:
We have a general understanding, then you create a new Javaweb project will have a Web. XML, this is the entrance, let's see what it contains:
The things in Web. XML I can not tell the very clear, to be attached to the source code, the company said each new project when the direct copy of the change is good, this is probably a common problem is more lazy, but one knocking trouble to understand all.
3. First say the interface, the order seems to be reversed, this is to login as an example:
Baseaction class inherits the Actionsupport Class (Xwork.jar), we write some basic output method in the Baseaction class, that is, the mobile phone is not to the Web request, the Web response to give mobile phone things, like when I login to the user name and password to the Web , the web will go to the server query whether there is this person, if there is or not, the web has to tell the phone side, the output format is generally given to the JSON format, which is the Web-baseaction class, This class also contains HttpServletRequest (the user name and password that the Web is sent to the web, which is taken from here), HttpServletResponse to their instance, and HttpSession object, The session is the browser cache, such as you login to other people's website, others can pass the session of your account record down. You can get it with gessession.
4, we write a loginaction inheritance baseaction, which writes a login method as follows:
/** * @see Mobile Login Interface */ @ResourceLoginservice Loginservice;//@Resource represents a reference resource Public void Startlogin() {map<string,object> map=NewHashmap<string,object> ();//Get Request ObjectHttpServletRequest request= This. Getrequest ();//Get the value of the key that the username has passed to us on the phone and load it up with a mapMap.put ("username", Request.getparameter ("username"));//Get the password is not empty it is used MD5 encryption with map installed, with MD5 for account security, registration is encrypted in the database, simple to prevent plagiarism if(Stringutils.isnotnull (Request.getparameter ("Password")) {String password = md5utils.md5_32 (Request.getparameter ("Password")); Map.put ("Password", password); }//Here is the service to remove the DAO layer, DAO layer dropped Ibatis (database layer) through the database query to get a message of this person, cannot find the return emptyString JSON = loginservice.loginmobile (map); Jsonobject jsonobj=NewJsonobject ();if(json==NULL) {json=""; Jsonobj.put ("State","false"); Jsonobj.put ("MSG","Login failed, user name or password error"); }Else{Jsonobj.put ("UserInfo", JSON); Jsonobj.put ("State","true"); Jsonobj.put ("MSG","Successful Landing"); }//Output here, the phone will get the value of the Web return This. Outjsonstring (Jsonobj.tostring ()); }
5.LoginService is the interface, which defines a series of methods, Loginservice for its object, and the interface has just called the Loginmobile method name, and its method body is implemented in Loginserviceimpl, Loginserviceimpl implements the Loginservice interface, the method body is as follows:
publicclass LoginserviceImpl implements Loginservice{ @Resource//引用dao资源//方法调用LoginDao 的Loginmobile方法 publicLoginmobile(Map<String,Object> params){ return loginDao.Loginmobile(params); }
6.LoginDao is also the interface class, his implementation class is the Logindaoimpl,logindao class declares a Loginmobile method name, its method body is implemented in Logindaoimpl. The specific implementation is:
Logindaoimpl class Inherits Abstacttemplatedao class ()
publicclass LoginDaoImpl extends AbstactTemplateDao implements LoginDao { publicLoginmobile(Map<String,Object> params){ //查询数据库,"login.loginmobile"中,login代表ibatis中name=login的文件,下的loginmobile方法(就是查语句,看) this.getSqlMapClientTemplate().queryForList("login.loginmobile",params); return list.toString(); }
Now you know the general flow, and summarize:
A login module We put it under a package, whether it is the Web or mobile phone landing are put here, a landing packet under four branches, an action layer, a service layer (business logic layer), a DAO layer (Database Operations layer), a entry (entity class, corresponding database table), Four levels, such as:
The representative of the arrow to configure, all the inherited Baseaction classes and their respective service implementation classes and DAO implementation classes are to be configured in their respective files, such as Loginaction will be configured in Applicationcontext-action as follows
<beansxmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-2.5.xsd "> <Bean name="Loginaction" class="org." U9APP.login.action.LoginAction " scope=" Prototype "></Bean></Beans>
The same is true of DAO's implementation class, which is registered in Applicationcontext-dao
<?xml version= "1.0" encoding= "UTF-8"?><beansxmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-2.5.xsd "> <!--Login Management -- <Bean name="Logindao" class="org." U9APP.login.dao.LoginDaoImpl " /></Beans>
Structs also has a similar configuration, while the previous one in Web. XML, the four files in spring are configured. Sping in the ApplicationContext is used to configure the Ibatis, there is a picture is wrong ha, and finally the interface you write the URL is so spelled up.
Http:192.168.0.100:8080/loginaction/startlogin.action?username= "123" &password= "123456"
which
192.168.0.100 your computer ip,8080tomcat port number, loginaction for applicationcontext-action in the class class= "org. U9APP.login.action.LoginAction "Its name value, Startlogin is the first-written login method name under the login class. Action,? After the parameter, Usname is the key, this is the web and the phone docking key,123 for the user name, 123456 for the password, this key can be replaced by other strings but you have to know the meaning and will be able to receive the phone.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Web to mobile phone-side write interface HttpServletRequest, HttpServletResponse