📜 ⬆️ ⬇️

Compact Java servlet for mobile web


The main area of ​​programming for me is software development for the automation of accounting in trade. I was able to use Java servlets for this in 2009, when, along with the latest version of Openbravo POS, released for the desktop, was a restaurant module for PDA . The main ideas then for Openbravo POS developers were, in order not to complicate the desktop version of the application, to bring the narrow business logic into a separate small application, the main highlight of which was a compact web interface for accessing from any device, and not just where Java can run c swing. At that time, it was assumed that the servlet will not only work with the same database as the desktop version, but also the container for it will be integrated into the application on the desktop, after its launch, the user automatically got access to the POS in the range of Wi-Fi Fi network. Within the community, this idea has not received further development, but since 2012 I’ve continued to use the principles laid down for my customers, and in this article I’ll tell readers how to use the Stripes Framework in conjunction with jQuery Mobile and ORMLite to get a tool for the rapid development of small servlets oriented on the mobile web.

Tools


Usually, when you are looking for a development tool, you try to find something universal that can be studied once and reused for a long time. In 2012, this is exactly what I ran into when I tried to remake the source code of the restaurant module Openbravo POS. It was based on the Struts 1 framework, to learn how to write XML schemas for which I was not at all happy, but for good luck, just at that moment, I came across an overview of the Java ecosystem technologies from RebelLabs . One of the sections of this review was devoted to the popularity of web frameworks among developers, this 10-ku with 2% honorary included the Stripes . Since its development, I began to learn the basics of developing Java servlets. It also helped me that by that time on Habré in the Sandbox there was already an overview article about this framework.

In the Stripes, everything is subject to the logic of the reaction to the controllers (ActionBean), which are responsible for gaining access to the data model for further display to the user through JSP page templates. Since I needed a mobile web interface, for the visual part, I chose jQuery Mobile , the main plus of which is the ability to practically not use JavaScript to use only HTML5 markup to build a dynamic response to the controller's actions.

By the third element responsible for working with the data model, I first chose a very simple Persist ORM / DAO , but when its functionality was not enough, I changed it to a more powerful ORMLite . The main factor in choosing was the ease of use of the already existing representation model and the absence of the need to write SQL queries, as in the case of Stripes, only by annotations.
')

Model View Controller


The key in the Stripes is 100% focus on the MVC concept (Model-View-Controller). And the main goal of creating this framework in 2005 was to create a more lightweight implementation of the MVC concept for Java EE than was implemented in the then-popular framework Struts. By version 1.5 in 2008, this goal by the authors of Stripes was fully achieved and this framework began to gain popularity among developers.

When designing the structure of the source code of a servlet, you should try not to go beyond the paradigm of Model-View-Controller, which is not very difficult to do for most tasks when creating CRUD applications. For example, in the source code for the product directory, it will look like this:

Data source, source SQL table
CREATE TABLE PRODUCTS ( ID VARCHAR(255) NOT NULL, NAME VARCHAR(255) NOT NULL, CODE VARCHAR(255) NOT NULL, PRICESELL DOUBLE NOT NULL, CATEGORY VARCHAR(255) NOT NULL, PRIMARY KEY (ID), CONSTRAINT PRODUCTS_FK_1 FOREIGN KEY (CATEGORY) REFERENCES CATEGORIES(ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE UNIQUE INDEX PRODUCTS_CODE_INX ON PRODUCTS(CODE); CREATE UNIQUE INDEX PRODUCTS_NAME_INX ON PRODUCTS(NAME); 


ORMLite annotated data model
 @DatabaseTable(tableName = "PRODUCTS") public class Product { public static final String ID = "ID"; public static final String NAME = "NAME"; public static final String CODE = "CODE"; public static final String PRICESELL = "PRICESELL"; public static final String CATEGORY = "CATEGORY"; @DatabaseField(generatedId = true, columnName = ID) private UUID id; @DatabaseField(columnName = NAME, unique = true, canBeNull = false) private String name; @DatabaseField(columnName = CODE, unique = true, canBeNull = false) private String code; @DatabaseField(columnName = PRICESELL, canBeNull = false) private BigDecimal pricesell; @DatabaseField(foreign = true, columnName = CATEGORY, foreignColumnName = ProductCategory.ID, foreignAutoRefresh = true, canBeNull = false) private ProductCategory productCategory; // ... //   get  set. // ... } 



At the beginning, using ORMLite, the structure of the data model and its relation to the relational database (the value of the tableName and columnName fields) are tableName . Then, through the getters and setters, the model provides access to the other classes and methods of the servlet.

Controller Stripes for I / O data
 public class ProductCreateActionBean extends ProductBaseActionBean { private static final String PRODUCT_CREATE = "/WEB-INF/jsp/product_create.jsp"; @DefaultHandler public Resolution form() { //    . return new ForwardResolution(PRODUCT_CREATE); } public Resolution add() { // ... //    ,    . // ... } @ValidateNestedProperties({ @Validate(on = {"add"}, field = "name", required = true, trim = true, maxlength = 255), @Validate(on = {"add"}, field = "code", required = true, trim = true, minlength = 8, maxlength = 13), @Validate(on = {"add"}, field = "priceSell", required = true, converter = BigDecimalTypeConverter.class), @Validate(field = "productCategory.id", required = true, converter = UUIDTypeConverter.class) }) @Override public void setProduct(Product product) { super.setProduct(product); } } 


The controller intercepts the data transmitted by the user and processes it according to the specified annotations. For example, executing the Resolution form() , receiving the value of the key field productCategory.id , it passes it through UUIDTypeConverter , checking whether it meets valid criteria for the UUID.

JQueryMobile JSP Template with Stripes
 <!--   --> <stripes:layout-component name="content"> <stripes:errors /> <stripes:messages /> <stripes:form action="/ProductCreate.action?add"> <div> <stripes:hidden name="product.productCategory.id" value="${actionBean.product.productCategory.id}"/> </div> <ul data-role="listview" data-inset="true"> <li class="ui-field-contain"> <stripes:label name="label.Product.name" for="productName" /> <input name="product.name" id="productName" type="text" data-clear-btn="true"> </li> <li class="ui-field-contain"> <stripes:label name="label.Product.code" for="productCode" /> <input name="product.code" id="productCode" type="text" data-clear-btn="true"> </li> <li class="ui-field-contain"> <stripes:label name="label.Product.price" for="productPrice"/> <input name="product.priceSell" id="productPrice" type="number" step="0.01" value="0.00" data-clear-btn="true"> </li> <li class="ui-body ui-body-b"> <fieldset class="ui-grid-a"> <div class="ui-block-a"> <sdynattr:submit name="add" data-theme="a"/> </div> <div class="ui-block-b"> <sdynattr:reset name="clear" data-theme="b"/> </div> </fieldset> </li> </ul> </stripes:form> </stripes:layout-component> <!--   --> 



In a page template, jQuery Mobile markup can also be used to pre-validate input data, for example, in a visual form, offering the user on a mobile device different keyboards for text and numbers (in the input the type parameter is "text" or "number" ).

What is suitable for


At the end of 2013, RebelLabs published a new review of the development prospects for frameworks in the future 2014 . The Stripes did not enter this review, since most of the attention was paid to more polar frameworks than it, such as Spring, Grails or Vaadin. But it seems to me that if you use jQuery Mobile and ORMLite together with him, then he will now find his niche in the Java development toolkit. In at least 4 of the 7 applications proposed by RebelLabs, you can feel quite confident with it:
  1. CRUD applications - this was higher, if everything is done consistently and in the same way, it is very difficult to do badly. The main thing in designing is to try to make every controller action as complete as possible, but not overflowing.
  2. Mobile applications - here the main load lies on jQuery Mobile, for Java programmer a definite plus that, in addition to HTML5, you can not touch JavaScript and CSS.
  3. Application prototypes are the main thing for which I use this bundle, having typed 10 servlets in the portfolio, you can easily separate the individual components of each of them for different business processes.
  4. Porting desktop applications - and this is where I started, and it is for this application that I can recommend, first of all, the fusion of these three frameworks.



This is the result, a new interface with jQuery Mobile version 1.4 in 2014, in comparison with the interface that was in 2009 with the PDA module Openbravo POS.

As an example of the practical implementation set forth in the article, a small CRUD servlet project for the product catalog: github.com/nordpos-mobi/product-catalog

Source: https://habr.com/ru/post/156781/


All Articles