Use JFinal/Jsmart framework for development experience (2)

Source: Internet
Author: User

First, start with the Controller, log on to the user, route the data to the Controller class, and perform corresponding processing in the Action.

  • 1. Logon:

Address: http: // localhost: 8080/Jdemo/index. jsp

Jump to: http: // localhost: 8080/Jdemo/users/loginUsers

Code of the Action class:


@ Before (ControllerInterceptor. class) public class UsersController extends Controller {private IUsersService us = new UsersServiceImpl ();/*** User Logon Action */@ Before ({LoginValidator. class, ActionInterceptor. class}) public void loginUsers () {String uName = getPara ("USERS. UNAME "); String uPswd = getPara (" USERS. UPSWD "); Users u = us. getUsers (uName, uPswd); System. out. println (u); if (null! = U) {setSessionAttr ("CURRENT_USER", u); renderJsp ("/users/home. jsp");} else {setAttr ("loginUsersMsg", "the user name or password is incorrect! "); RenderJsp ("/index. jsp ");}}}

You can use the Annotation non-intrusive method to add the Controller-level Interceptor to the class, and add the Action-level interceptor and logon verification to the Action.

Add a Global Interceptor to the configuration class.


@Override    public void configInterceptor(Interceptors me) {        me.add(new GlobalInterceptor());    }

The following figure shows the information displayed on the console after the logon action is completed, reflecting a request response process.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324K2a-0.png "title =" big rich big expensive .png "data-pinit =" registered "/>

Analysis description:

Step 1: Global interceptor;

Step 2: Enter the Controller-level interceptor;

Step 3: Set the verification class for the loginUsers method, and enter Step 4 for the verification method)

Step 4: Enter the Action-level interceptor;

Step 5: send the request information to the Service and Model through Action for data processing and return to the view;

Step 6: each level of interceptor exits in the stack order.


  • 2. View All Users:

Address: http: // localhost: 8080/Jdemo/users/showUsersList

Go to: http: // localhost: 8080/Jdemo/users/home. jsp

Action Code:

/*** Action for obtaining information of all Users */public void showUsersList () {@ SuppressWarnings ("unchecked") List <Users> listUsers = us. getUsersAll (); setAttr ("LIST_USERS", listUsers); renderJsp ("/users/home. jsp ");}

The execution process ends. Console information:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324G205-1.png "title =" ashboard .png "data-pinit =" registered "/>

The information displayed on the console shows that there is no Action-level Interceptor. Similarly, the showUsersList () method does not contain the Action-level interceptor ).

  • 3. upload a file:

Jsmart/JFinal provides convenient upload of 10 MB files.

The method for uploading files is very simple. Call the Controller method directly, getFile (...);

See examples:


/*** Action for uploading user data */public void uploadImage () {String uId = getPara ("uId"); String realPath = getPara ("realPath "); string uploadPath = ServiceUtils. newSaveDir (realPath ); //************************************** ****** // ************ obtain the File in the form and upload it now ********//****** ************************************ UploadFile ufile = getFile ("uImage ", uploadPath); if (null! = Ufile) {ServiceUtils. renameFile (ufile, uId); setSessionAttr ("UPLOAD_MSG", "uploaded successfully! ");} Else {setSessionAttr (" UPLOAD_MSG "," Upload Failed! ");} Redirect ("/users/showUsersList ");}

The method provided by the author of JFinal to modify the Upload File Name coincides with that provided by me, which also provides a way to add a timestamp for the Upload File.

In the above Code, ServiceUtils. renameFile (UploadFile uploadFile, <Type> newName) is implemented.


Public static void renameFile (UploadFile uf, String newName) {File uf. getFile (); String srcName = src. getName (); String dstName = src. getParent () + File. separator + newName + srcName. substring (srcName. indexOf ('. '); File dst = new File (dstName);/*** Delete the renamed File if it exists! */If (dst. exists () {dst. delete ();} src. renameTo (dst );}

For File download, The JFinal/Jsmart framework provides a very useful method, renderFile (File f). This method directly generates a specified download File for the page, which is very simple and convenient.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324M393-2.gif "/> the JFinal/Jsmart framework is flexible and focuses on solving practical problems, the plug-in mechanism provides the basis for extension.

The workflow of the Framework is shown in the preceding two examples: 1, 2, file download, and upload, as shown in Example 3.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324M393-2.gif "/> about data access is very ibatis, done by configuring SQL, Model, and database table ing.

  1. Table ing

    Package com. broncho. jsmart. model. users; import com. jsmart. ext. plugin. tablebind. tableBind; import com. jsmart. plugin. activerecord. model;/*** Users table data Model ** @ author Broncho * @ time 09:17:18 on January 1, July 17, 2013 */@ TableBind (tableName = "USERS", pkName = "UID ") public class Users extends Model <Users> {/*****/private static final long serialVersionUID = 5631774099405889205L; /*** shared object of Users Data Model */public static final Users dao = new Users ();}


  2. Database Access

    Configure the SQL File

    <?xml version="1.0" encoding="UTF-8"?><sqlGroup name="USERS">    <sql id="queryCurrentUsers">        SELECT * FROM USERS WHERE UNAME=? AND UPSWD=?    </sql></sqlGroup>

    Query instances:

    @Override    public Users getUsers(String uName, String uPswd) {        List<Users> ulist = null;        ulist=Users.dao.find("USERS.queryCurrentUsers", new Object[]{uName,uPswd});        Users u = null;        if (null != ulist && ulist.size() != 0) {            u = ulist.get(0);        }        return u;    }

    650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324J944-4.gif "/> note: the name of the SQL configuration file, xxx-sql.xml

    The SQL configuration file uses the Group-Id method to manage SQL statements, so that you can use the configured SQL statement using GroupName. Id.

    650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11324M393-2.gif "/> JFinal is really doing the features described, and each feature can be seen from the existing framework, learning is easy, for example, the same is true for Action and Struts2, for database access at the Model layer, and for the ibatis framework. It also shows that the conventions are better than the configurations, and are scalable, pluggable, and lightweight, no dependency. JFinal is more like a simplified and simplified framework. It is more like a framework developed by developers to escape the tedious and ease the Learning Burden.

 

This article is from the "wild horse red dust" blog and will not be reposted!

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.