Tangyuan Web, Control Layer Components in the Tangyuan Framework
Tangyuan Web, Control Layer Components in the Tangyuan Framework
In this article, we offer an introduction to this open source framework and show you some basic code to get your app off the ground.
Join the DZone community and get the full member experience.
Join For FreeJumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.
1. Introduction
Tangyuan-web is the control layer component in the Tangyuan framework. It takes the component and plug-in as the thought, and takes the XML configuration as the core. With little to no Java code, you can complete the development of the control layer.
2. Features
- For developers, it's simple and efficient.
- Supports stand-alone mode, distributed mode, and mixed mode.
- During the development of the control layer, each link is abstractly defined, and the formation of a standardized, complete life cycle is enabled.
- Stratified development model. Based on the definition of the life cycle, you can develop it on a layer-by-layer basis.
- Componentization and pluggable. Follow the Tangyuan framework for unified architecture.
Source Address: https://github.com/xsonorg/tangyuan2
Official website: http://xson.org/
3. Lifecycle
The above picture describes the controller's entire lifecycle. For each blue box in the picture, there is a separate processing section, where:
- The Data assembly, Before Handler, After Handler processes are used by tangyuan-web components in the AOP module to provide functional support.
Get cache
andSet cache
are provided by the tangyuan-cache component. ForGet cache
, it appears twice in the figure above and is represented by a dashed box. It is processed beforeBefore Handler
or processed afterBefore Handler
, through the controller configuration file. This can be set in the c node's cacheInAop attribute can;Set cache
andGet cache
use the same processing mechanism.- For the Execute link, if it is a custom controller, it works on behalf of the controller method call; and if it is a simple controller, the request parameters from the controller are forwarded to the backend services, and obtain the results of service response.
4. Complete Example
a. Add dependent JAR:
<dependency>
<groupId>org.xson</groupId>
<artifactId>tangyuan-web</artifactId>
<version>1.2.0</version>
</dependency>
b. Add the web component to the tangyuan configuration file tangyuan.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tangyuan-component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/component.xsd">
<!--Add web component -->
<component resource="component-web.xml" type="web" />
</tangyuan-component>
c. Configure the tangyuan-web component in the component-web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/web/component.xsd">
<!--Controller plugin-->
<plugin resource="controller/controller.xml"/>
<plugin resource="controller/controller-news.xml"/>
</web-component>
d. Configure the controller in the plugin file controller.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-controller xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/web/controller.xsd">
<!-- Query the news list -->
<c url="/news/newslist" validate="news/newslist" transfer="{service}/news/newslist" />
</web-controller>
e. Configure web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<listener>
<listener-class>org.xson.tangyuan.web.TangYuanContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xco</servlet-name>
<servlet-class>org.xson.tangyuan.web.XCOServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xco</servlet-name>
<url-pattern>*.xco</url-pattern>
</servlet-mapping>
</web-app>
Through the above five steps, we have built a web application, based on tangyuan-web components. Then the front-end can use /news/newslist.xco
to visit our web system.
5. Features and Examples
5.1 A Row of XML Configuration Implements a Controller Definition
<!-- Query the news list -->
<c url="/news/newslist" transfer="{service}/news/newslist" />
If you use URL auto-mapping mode, you do not even have to manually configure the controller in XML.
5.2 Support for Multiple Modes
- Single system mode: all controllers and services are in one system.
- Distributed mode: controllers and services belong to different systems.
- Mixed mode: this is the combination of the above two modes.
Example
<!-- Distributed mode -->
<c url="/news/newslist" transfer="{service}/news/newslist" />
<!-- Single system mode -->
<c url="/news/newslist" transfer="/news/newslist" />
Description
The {service}
in the example represents the domain name of the system where the service is located. The single system mode does not need to specify the domain name of the service because the controller and the service are in the same system.
5.3 Permission Verification
Set the permission flag
<c url="/news/newslist" permission="y"/>
Define the permission interceptor
public class MyPermissionFilter extends AbstractPermissionFilter {
@Override
public boolean permissionCheck(String permission, RequestContext requestContext) {
if (null == permission) { // No need to log in
return true;
} else { // Need to log in
// TODO Verify user permission
return true;
}
return false;
}
@Override
public void authFailed(RequestContext requestContext) {
// TODO The processing logic after authentication failure
}
}
Configure the permission interceptor
<filter>
<filter-name>PlatformUserFilter</filter-name>
<filter-class>org.xson.demo.MyPermissionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PlatformUserFilter</filter-name>
<url-pattern>*.xco</url-pattern>
</filter-mapping>
Description
In the above example, when the controller is defined, its permission flag permission="y"
is set, and then it's passed through the interceptor to its authority to judge and deal with.
5.4 Data Conversion
For XCO requests, the data conversion will convert it to XCO objects; for GET requests or form submissions, the tangyuan-validate component can also be converted to XCO objects.
Related information:
XCO: https://dzone.com/articles/introduction-to-the-xco
XCO-JS: https://dzone.com/articles/xco-js-a-data-based-js-framework
5.5 Data Verification
Define validation rules
<?xml version="1.0" encoding="UTF-8"?>
<validate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/validate/rule.xsd" ns="user">
<ruleGroup id="addUser" desc="add user">
<item name="userName" type="string" require="true" desc="user name" message="Illegal user name">
<rule name="interval_length" value="3,6"/>
<rule name="match">^[a-zA-Z0-9\u4E00-\u9FA5]+$</rule>
</item>
<item name="age" type="int" require="true" desc="age" message="Illegal user age">
<rule name="interval" value="10, 150"/>
</item>
</ruleGroup>
</validate>
Define the controller
<c url="/user/addUser" validate="user/addUser" transfer="{service}/user/addUser" />
Description
Define the validation rule in the tangyuan-validate component, specify the validation rule in the controller's definition, such as validate="user/addUser"
, so that the validation of the requested data can be achieved. On the tangyuan-validate component, we will describe this in detail later.
5.6 Use Cache
Define the cache
<cache id="cache01" type="local">
<property name="strategy" value="time" />
<property name="survivalTime" value="10" />
<property name="log" value="true" />
</cache>
Define the controller
<c url="/news/newslist" transfer="news/newslist" cacheUse="id:cache01; key:${url}${arg}; expiry:10"/>
Description
Define the cache in the tangyuan-cache component, specify the use of the cache in the controller's definition, such ascacheUse="id:cache01; key:${url}${arg}; expiry:10"
, so that you can increase the use of the cache in the controller's lifecycle. 5.7 AOP
Defines the AOP implementation class
public class MyDataAssembler {
// Assemble user information
public void assembleUserInfo(RequestContext context) {
XCO xco = (XCO) context.getArg();
XCO tokenUser = context.getAttach().get("tokenUser");
xco.setXCOValue("tokenUser", tokenUser);
}
}
Configure the controller AOP
<!-- Instantiate the implementation class -->
<bean id="myDataAssembler" class="org.xson.tangyuan.demo.MyDataAssembler" />
<!-- Define the controller -->
<c url="/demo/placeOrder" transfer="demo/placeOrder" />
<!-- Configure the controller involved in the AOP -->
<assembly call="{myDataAssembler}.assembleUserInfo">
<include>/*</include>
</assembly>
Description
Through the above configuration, in the access to /demo/placeOrder
controller, in the data assembly link, will be able to send the user's token information package to the request object.
Conclusion
Through this article, we have a preliminary understanding of tangyuan-web components. If you are interested in this, I've provided a link below that you can use to get more detailed information.
tangyuan-web technical documentation: http://www.xson.org/project/web/1.2.0_en/
Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}