In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you what the ActFramework getting started guide is, the content is very detailed, interested friends can refer to, hope to be helpful to you.
ActFramework is a high-performance Java full-stack framework for the development of traditional MVC applications or RESTful services. Compared with other existing MVC/RESTful frameworks, ActFramework has the advantages of expressiveness, simplicity and ease of use.
This is a self-statement by ActFramework.
Expressive and concise and easy to use, is its advantages, but also my favorite place.
This article will take you to experience and start a basic MVC project. (Controller,Service,Dao standard structure)
Since it is a full-stack framework, from MVC to ORM, this article will demonstrate it through ActFramework + Hibernate (JPA). The template rendering engine uses the default Rythm.
PS:ActFramework projects cannot use lombok or other non-Java JVM languages for the time being. PS: most of the content of this article coincides with the content of the ActFramework document. This article can be used as a guide to get started with a quick understanding of the framework. See the documentation for more details. Code address in this article: GitHub,Gitee
A preliminary understanding of creating a new project
First, let's set up a Hello ActFramework project. ActFramework provides a complete Maven ecology, and we can quickly start an ActFramework foundation project with Maven.
Let's give a hint to our buddies who are not yet familiar with IDEA.
Add a new expansion item for Maven
Org.actframeworkarchetype-quickstart1.8.23.0
Then fill in the basic information of your project.
Then configure your project information according to your needs. Basically, no additional configuration is required in addition to the path. Finally, create the project and wait for Maven to expand automatically. PS: students are advised to name the project according to the general naming rules of Java. Please don't learn from me.
Be familiar with the structure
This is the basic structure of an ActFramework project. Let's not look at a standard Maven project structure, test, and let's focus on the main folder. Java, as we all know, is the code path. By default, a class AppEntry will be generated for you to start the project.
This is not the big head, the big head is under the resources folder.
Resources |-- conf this is the folder where configuration files are stored |-- the path where rythm is generally used to store template files
Apart from these, the rest will be left alone for the time being. There will also be some subfolders under the conf folder, and there will also be app.properties under these subfolders. These are also configuration files, and of course not all of them will be here.
The difference between it and the app.properties under conf is that the app.properties under the subfolder is used to deal with different environments (such as development environment, test environment, production environment). Also according to the standard routine, the configuration of app.properties in the conf directory will be enabled for configurations that are not available in app.properties in the current environment.
Template directory. Of course, rythm is not necessarily the final template directory. By default, ActFramework will follow the / resources/ template engine id/Controller class name / method name. Suffix name to find the template file. Of course, you can also specify it manually. Because the expanded project has an Action in the AppEntry class, it also comes with a template file located under resources/rythm/com/IceCreamQAQ/test/AppEntry home.html by default. After that, what template you need to add, just follow this rule and be convenient.
Start the project
Turn your attention to the default generated startup class AppEntry.
Public class AppEntry {@ GetAction public void home (@ DefaultValue ("World") [@ Output] (https://my.oschina.net/output1314) String who) {} public static void main (String [] args) throws Exception {Act.start ();}}
Main method, easy to understand, startup method. Students who have used Spring Boot may understand this startup mode more easily. But if the students from J2EE may not quite understand.
ActFramework does not adopt the traditional J2EE, but a standard Java program that implements the HTTP protocol based on Java SE. In short, ActFramework does not need (in the standard sense) Web containers (such as Tomcat). ActFramework itself is a Web container. Of course it doesn't matter. However, it takes a little bit of adaptation to jump out of the habit of HttpServletRequest and HttpServletResponse.
By executing this main method, we can start the ActFramework project.
| _ | _ | | _ | | / | | _ | _) /\ |\ / | | _ / | _ | _\ _ / | /--\\ _ |\ / -\ | _\ / /\ _ / |\ |\ powered by ActFramework r1.8.23-d4dee version : v1.0-SNAPSHOT scan pkg: com.IceCreamQAQ.test base dir: C:\ project\ IceCreamQAQ\ Act\ HelloActFramework pid: 6972 profile: dev mode: DEV OS: WINDOWS jdk: Java HotSpot (TM) 64-Bit Server VM 1.8 zen: Complex is better than complicated. 2019-06-25 22 INFO 41 INFO 26649 INFO a.At @ [main]-loading application (s)... 2019-06-25 22 22 Switzerland 41V 26661 A.A. App @ [main]-App starting. 2019-06-25 22 INFO INFO o.xnio@ [Thread-0]-XNIO version 3.3.8.Final 2019-06-25 22 14 INFO o.x.nio@ [Thread-0]-XNIO NIO Implementation Version 3.3.8.Final 2019-06-25 22 14 INFO 27111 WARN a.DbServiceManager @ [main]-DB service not initialized: No DB plugin found 2019-06-25 22 14 14 41 WARN a.m.MailerConfig@ [jobs-thread-3]-MailerConfig@ Will use mock smtp to send email 2019-06-25 22 INFO @ [main]-network client hooked on port: 5460 2019-06-25 22 22 INFO 27694 INFO a.Act @ [main]-network client hooked on port: 5463 2019-06-25 22 14 INFO a.Act @ [main]-CLI server started on port: 5461 2019-06-25 22 14 INFO @ [main]-app is ready in 1746ms at: .168.0.105: 5460 2019-06-25 22 INFO 41 INFO 28280 INFO a.A. ApiManager @ [jobs-thread-3]-start compiling API book 2019-06-25 22 purl 41mer 28346 ApiManager @ [jobs-thread-3]-Manager @ [ApiManager]-ApiManager @ [ApiManager]
At this point, launch the browser and open http://127.0.0.1:5460 to see the default home page.
Controller and Action
The startup class AppEntry is itself a Controller. ActFramework's Controller does not require any annotations, nor does it require inheritance or implementation of any class. As long as Controller contains Action, it is a Controller. (the author said at this time, but I don't know whether the one with interceptor is Controller, but it's not a big problem, it doesn't affect it.)
Let's take a look at AppEntry's home method.
@ GetActionpublic void home (@ DefaultValue ("World") @ Output String who) {}
The method is not long, but it is a very standard Action. The @ GetAction annotation indicates that this is a Get method. Similarly, there are @ PostAction, @ PutAction, @ DeleteAction. There is also an @ Action annotation that defines all or specifies the route for the HTTP request method. @ DefaultValue represents the default value when who is null. @ Output means that this parameter will be taken to the template.
Students who have used Spring may be able to understand the exact meaning of the who variable and the source of the value. Here is a brief explanation of the value source of the who variable. Variables within the parameters of the Action method are generally matched with the request parameters. If the variable name is who, it matches the request parameter named who. Students who do not believe in evil can try to visit http://127.0.0.1:5460/?who=ActFramework to verify the results.
Of course, parameters can use not only String, but also Integer, and so on, as well as Java entity classes. ActFramework distinguishes the content of the request based on the request header. Form requests, or application/json or whatever, are treated equally. @ RequestBody is not required.
Meanwhile, ActFramework supports matching url paths, so let's write a simple Action.
@ GetAction ("test/ {name}") public String test (String name) {return name;}
Spring students may be so moved that they don't have to write annoying @ PathVariable at last! At the same time, ActFramework will determine how to render the page based on the return value type of your method. If your method is void, ActFramework will automatically look for the template file. If it is another type, ActFramework will choose an appropriate way to render your return value. In other words, we don't need @ ResponseBody anymore!
At this time, do not rush to click your restart button, directly visit the http://127.0.0.1:5460/test/wow to try! Is it wow?! The hot loading of ActFramework does not require you to do anything at all (although I still habitually click to restart). All you have to do is code and check the results.
Students who use Spring may be so moved that they don't have to wait for a restart for a minute or two. At this point, I would like to complain that IDEA starts the build project, plus the startup Tomcat finally counts the Spring startup. Oh, my God, this startup time. With Spring Boot, there has been a slight improvement. Once the project is big, it still takes more than a minute to start, 1.1 minutes or 1.1 minutes, but the work efficiency is not available here.
Some students may want to say that this alone is not enough. Sometimes you still need Request and Response.
Don't worry, just write H.Request and H.Response in the parameters of the method. In fact, under the Http agreement, everyone is pretty much the same. I will not copy this piece. It is recommended to check the document directly (in Gitee, it is convenient for domestic students to check).
At the same time, ActFramework provides many very convenient methods. For example, you can choose import static act.controller.Controller.Util.*; or Controller extends Controller.Util to eat. Of course, you can choose not to use it.
Rythm template engine @ args String who Hello World-ActFramework Hello @ who
Powered by ActFramework @ act.Act.VERSION.getVersion ()
This is the template for the home method of the AppEntry generated by default.
The biggest difference Rythm seems to have compared to other template engines is the declaration variables and variable types that he needs to display. @ args String who represents the declaration of a variable of type String named who. The String here is java.lang.String. Then when you need to output the contents of this variable, just write the @ + variable name.
Some students may want to ask, what if I want to declare an entity variable. It's very simple, you can @ args Entity name, that would be fine. You don't really think it's that simple, do you? It's not that simple, but it won't be that hard. You just need the pilot package. @ import com.IceCreamQAQ.test.entity.Entity
Some students may wonder, am I writing Java? Do you want a guide bag? That's right! That's right! You are writing Java. Rythm template file, when loaded, will be dynamically generated by Rythm into a Java Class, and through this class to complete the rendering. So, you look like you're writing a template, but you're actually writing Java.
All the code you write after @ will be changed to Java code. Similarly, if you want to use a static method of a utility class, you can @ com.IceCreamQAQ.test.util.Utils.method () or you can write parameters in parentheses (or you can use the previously declared variable, so you don't need to add the @ symbol).
@ if logical judgment statement with the structure of
@ if (boolean) {/ / output} else {/ / otherwise output}
Of course, the else block is optional. It is important to note, however, that the contents in curly braces are output directly, rather than being converted to Java code. So to operate within large bracket blocks, you still need the help of the @ symbol.
Let's do it! Establish a structure
We have learned all the basics, and then it's time to serve the main course.
First of all, let's build some bags. Controller, dao, entity, service, service.impl
The goal of this article is to complete the basic user login and registration.
Add Maven dependency
Org.actframework act-hibernate com.zaxxer HikariCP mysql mysql-connector-java 5.1.47 write entity classes
Create a User.java entity class
Entity@Table (name = "user") public class User implements SimpleBean {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) public Integer id; @ Column public String password; @ Column public String email; @ Column public String phone; public static String getPasswordHash (String password) {return Act.crypto () .passwordHash (password);}}
A standard JPA entity class, students who don't know what JPA is, you can learn about Baidu and Google. I don't want to elaborate too much here. Because ActFramework is not compatible with lombok but we don't bother to write Getter and Setter, we only need to implement the act.util.SimpleBean framework to automatically generate Getter and Setter for you.
Act.crypto (). PasswordHash (String password) is a general password processing scheme provided by the framework. Of course, you like what plan to use, here do not force, but I am lazy, what use.
It's just that this generation time is after the application is started, not at compile time for lombok. Therefore, when coding in the program, you can only set the member variable to public, and obtain and assign values in the form of user.name. According to the author, xxx = user.name; is converted to xxx = user.getName () at run time, and user.name = xxx; is converted to user.setName (xxx) at run time.
But I have not carefully verified this, curious students can dump jvm to verify it.
Write a data access layer (Dao)
Write UserDao.java
Public class UserDao extends JPADao {}
The Dao layer is easier to write and only needs to be inherited from act.db.jpa.JPADao. More Dao I suggest that I write a suitable BaseDao extends JPADao for my business. BaseDao gives us some basic additions, deletions, modifications and queries for abstract class Act. To be more complex, we need to use EntityManager.
Write a business layer (Service)
Write UserService.java
@ AutoBindpublic interface UserService {int register (User user); User login (Integer id,String password);}
The @ AutoBind annotation means that when ActFramework starts, the implementation class is scanned and automatically associated for dependency injection.
Write UserServiceImpl.java
Public class UserServiceImpl implements UserService {@ Inject private UserDao dao; @ Override @ Transactional public int register (User user) {dao.save (user); return 0;} @ Override @ Transactional public User login (Integer id, String password) {User user=dao.findById (id); if (Act.crypto (). VerifyPassword (password, user.password) return user; else return null;}}
Act.crypto (). VerifyPassword (String password, String passHash) the general password processing scheme mentioned above here is the password authentication scheme provided by the framework.
@ Inject is a dependency injection annotation. We don't need to add any additional comments to Dao to inject Dao.
Unlike Spring, ActFramework does not require any additional comments to indicate that this class needs to be managed by the framework. ActFramework is more inclined to help you manage all the instances of your classes, which is technically unlikely. But ActFramework can help you manage instances of most classes.
Write the control layer (Controller)
Write UserController.java
Public class UserController extends Controller.Util {@ Inject private UserService service; @ GetAction ("register") public void register () {} @ GetAction ("login") public void login () {} @ PostAction ("doRegister") public Result doRegister (User user) {try {user.password=User.getPasswordHash (user.password); service.register (user) } catch (Exception e) {e.printStackTrace (); return renderHtml ("alert ('\\ u6CE8\\ u518C\\ u5931\ u8D25\\ uFF01'); window.history.back ();");} return renderHtml ("alert ('\ u6CE8\\ u518C\\ u6210\ u529F\ uFF01uid:" + user.id+ "'); location.href='login';") } @ PostAction ("doLogin") public Result doLogin (Integer uid,String pwd) {try {User user=service.login (uid,pwd); if (username null) return renderHtml ("alert ('\ u767B\\ u5F55\\ u6210\\ u529F\\ uFF01'); location.href='register';"); return renderHtml ("alert ('\ u767B\\ u5F55\ u5931\ u8D25\\ uFF01')) Window.history.back (); ");} catch (Exception e) {e.printStackTrace (); return renderHtml (" alert ('\ u767B\\ u5F55\\ u5931\ u8D25\\ uFF01'); window.history.back (); ");}
With Controller.Util consumption, we can simply write a prompt message. Of course, I suggest you use the page to display information instead of my pop-up box. It looks low, but it's just a demonstration. Who cares? Isn't it?
Write a template
Write rythm/com/IceCreamQAQ/test/controller/UserController/register.html
Register
Write rythm/com/IceCreamQAQ/test/controller/UserController/register.html
Log in
Two extraordinarily simple pages with almost nothing to do with functionality. Simple and clear, Jane can easily understand (running away is actually lazy, but it's okay to meet the demonstration, right?
Write configuration
Write a data source configuration file
The data source configuration file is located in / resource/conf/db.properties, of course, you can also use multi-environment configuration, but I am lazy.
Db.impl=act.db.hibernate.HibernatePlugindb.driver = com.mysql.jdbc.Driverdb.url = jdbc:mysql://127.0.0.1:3306/Test?useSSL=false&useUnicode=true&characterEncoding=utf-8db.username = testdb.password = 12345678
A straightforward configuration file. In fact, ActFramework supports multiple data sources well, you just need to add a serial number after db, such as db1.url. Of course, it doesn't make much sense to talk about it now. We'll talk about it later.
Write Hibernate configuration file
In fact, the configuration file for Hibernate in JPA mode is hibernate.properties located in the classpath directory. In other words, the hibernate.properties configuration file should be placed directly under / resource, not / resource/conf. That's why I said above that not all the configuration files are here. Write / resource/hibernate.properties
Hibernate.show_sql=true starts testing
After writing this, the simple registration and login function has been done. What? You asked me for a data sheet? I want to go. At this point, we need to start the project (after adding the dependency, we need to restart the project, and the hot deployment cannot load the dependency dynamically. ) then open the browser and go play!
Http://127.0.0.1:5460/register
This is the end of the introductory part. After eating this part of the snack, do you have a certain understanding of ActFramework? The next advanced part is the main course. Don't go away. It will be more wonderful when I finish filling the hole.
Advanced level
In the advanced part, we will make some basic improvements to the mini project and complete some basic functions. At the same time, learn more about ActFramework.
The goal of the advanced part: full function for this project. Target function: each user can post articles, which are visible only to themselves. (memorandum)
Of course, I won't show all the code in this part. I will only show some of the key code to guide you to ActFramework. The details are up to you! After all, it's the Boss you kill that gives you a sense of achievement.
This is the end of what is the introduction guide to ActFramework. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.