Winter framework efficient and flexible request processing framework

Source: Internet
Author: User

To gain a deep understanding of the underlying Java and the principles of Hibernate and struts2, I think I have written a framework.

This framework integrates the functions of Hibernate and struts2. It is an independent framework without relying on its jar package.

Operating principle:

We know that struts2 works with filters. So is my framework.

One of them is that the method is different from struts2 and is more flexible.

First of all, I did not look at the source code of struts2. I just wrote this framework on my own.

The main purpose is to reduce tedious operations and improve overall development efficiency.

The winter framework can operate databases directly and automatically generate SQL statements without the need to write an SQL statement.

Take a look at the simple configuration:

Step 1:

First, add a filter to the Web. xml file.

 <filter>  <filter-name>request</filter-name>  <filter-class>org.pan.filter.RequestFilters</filter-class>  </filter>    <filter-mapping>  <filter-name>request</filter-name>  <url-pattern>*.action</url-pattern>  <url-pattern>*.jsp</url-pattern>  <url-pattern>*.pan</url-pattern>  <url-pattern>*.php</url-pattern>  <url-pattern>*.do</url-pattern>  <url-pattern>*.aspx</url-pattern>  <url-pattern>*.html</url-pattern>  </filter-mapping>

In this example, a suffix is specified for the filter. Only the specified suffix is specified.

Then create the request. xml file in the WEB-INF folder

Multiple configurations are provided here,

A request node corresponds to a page, and the class corresponds to the class to be processed. You can choose not to specify a class. If you do not specify a class, you must specify a return page, which will be directly forwarded to the specified page.

Played the role of a forwarder

You can also specify a custom method. By default, execute

Configuration:

<? XML version = "1.0" encoding = "UTF-8"?> <! -- <! Doctype pages public "// unknown/" "request. DTD "> --> <pages> <request-encode> UTF-8 </request-encode> <response-encode> UTF-8 </response-encode> <! -- Homepage --> <request page = "index. JSP "class =" com. pan. action. indexaction "> <result name =" success ">/index. JSP </result> <result name = "error">/error. JSP </result> </request> <request page = "login. JSP "class =" com. pan. action. loginaction "> <result name =" success ">/login. JSP </result> <result name = "error">/fail. JSP </result> </request> <! -- PHP --> <request page = "test. PHP "class =" com. pan. action. myaction "> <result name =" success ">/test. JSP </result> <result name = "error">/fail. JSP </result> </request> <! -- Do request --> <request page = "test. do "class =" com. pan. action. myaction "> <result name =" success ">/test. JSP </result> <result name = "error">/fail. JSP </result> </request> <request page = "index.html" class = "com. pan. action. indexaction "> <result name =" success ">/test. JSP </result> </request>
Package COM. pan. action; import Java. util. list; import Org. pan. SQL. sqlsession; import Org. pan. SQL. dao. isession; import Org. pan. support. actionsupport; public class indexaction extends actionsupport {private string username; Public void setusername (string username) {This. username = username;} @ overridepublic string execute () {// todo auto-generated method stubsystem. out. println (username + "request username"); // Save the object isession session = new sqlsession (); Order object = New Order (); object. setaddress ("Beijing"); try {list <order> orders = session. findbyexample (object, true); system. out. println (orders. get (0 ). getcount () + "quantity"); system. out. println (orders. get (0 ). getaddtime () + "time");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} addactionmessage ("username", username); addactionmessage ("welcome", "Welcome to winterframework"); Return success ;}}

<Request page = "login. aspx "class =" com. pan. action. indexaction "> <result name =" success ">/test. JSP </result> </request> <request page = "login. xxoo "class =" com. pan. action. indexaction "> <result name =" success ">/test. JSP </result> </request> <! -- Value configuration request does not configure the return page --> <request
Page = "123.html"> <result name = "success">/test. jsp </result> </request> </pages>

Then configure your own action processing class:

Must be integrated: actionsupport class, which is not forcibly inherited in struts2. Here I must base and implement the Method

EXAMPLE Class:

Package COM. pan. action; import Java. util. list; import Org. pan. SQL. sqlsession; import Org. pan. SQL. dao. isession; import Org. pan. support. actionsupport; public class indexaction extends actionsupport {private string username; Public void setusername (string username) {This. username = username;} @ overridepublic string execute () {// todo auto-generated method stubsystem. out. println (username + "request username"); // Save the object isession session = new sqlsession (); Order object = New Order (); object. setaddress ("Beijing"); try {list <order> orders = session. findbyexample (object, true); system. out. println (orders. get (0 ). getcount () + "quantity"); system. out. println (orders. get (0 ). getaddtime () + "time");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} addactionmessage ("username", username); addactionmessage ("welcome", "Welcome to winterframework"); Return success ;}}

There are many ways to set the value on the page,

The Framework provides this method by default, which is to put the value into the session.

addActionMessage

The tag provided by the framework can be used to set the value on the page.

Example:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://winter.baletu.com/" prefix="w" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML>

Main operation process:

The Web. xml configuration filters are then copied and scheduled by requestfilters. java.

The Controller is called when there are few codes in the filter:

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {// request controller requestcontroller controller = new requestcontroller (request, response, chain); try {controller. dofilter ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}

Then go to the request controller to see:

Package Org. pan. controller; import Java. util. list; import javax. naming. configurationexception; import javax. servlet. filterchain; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. pan. bean. request; import Org. pan. bean. result; import Org. pan. code. configuration; import Org. pan. code. act Ionmanage; import Org. pan. support. actionsupport; import Org. pan. util. resultuitl; import COM. sun. crypto. provider. rsacipher;/*** request controller ** @ author Pan **/public class requestcontroller {private httpservletrequest request; private httpservletresponse response; private filterchain chain; Public requestcontroller (servletrequest request, servletresponse response, filterchain chain) {This. request = (httpservletrequ Est) request; this. response = (httpservletresponse) response; this. chain = chain;}/*** process request */Public void dofilter () throws exception {string filepath = request. getservletpath (). replace ("/", ""); // The Name Of The current file // get the jump object and some operation methods through the configuration file configuration = new configuration (request ); request RT = configuration. find (filepath); If (RT! = NULL) {// call the return value of the request object manager to determine the page to be forwarded. // if no class path is configured, it is directly forwarded to the result page as a forwarder if (RT. getclasspath (). equals ("") {result rs = resultuitl. findresult (RT. getresults (), actionsupport. success); If (rs = NULL) {chain. dofilter (request, response);} else {request. getrequestdispatcher (RS. getpath ()). forward (request, response) ;}} else {actionmanage = new actionmanage (request, response, Rt. getclasspath (), Rt. getmethod (); string re Sult = actionmanage. getresult (); // search for the page list corresponding to the returned value <result> Results = RT. getresults (); Boolean isdofilter = false; // whether the for (result result2: Results) {If (result2.getname (). equals (result) {// obtain the corresponding path string Path = result2.getpath (); // if it is not the current page, forward or jump to If (! Path. equals (filepath) {// forward is used by default. Otherwise, data is lost. isdofilter = true; request. getrequestdispatcher (PATH ). forward (request, response) ;}}}} else {chain. dofilter (request, response );}}}

The request manager called, Configuration Manager, and Action Manager.

The Configuration Manager reads the configuration information of request. xml and passes it to the Action Manager,

The Action Manager instantiates the user action, and then calls the converter to convert the field type and assign values.

Finally, execute the method defined by the user action, Mark secuess, input, error, etc., and then forward it. If it is the current page, it will not forward, but will not process it,

To other interceptors for processing.

Basically, the configuration and running principles are similar to those of struts2. This is two days to complete the development.

If you need source code, contact me. Q: 599194993

Looking for someone to continue to complete this framework, the next step is to support the Hibernate and spring frameworks. Intentionally leave information

Note: Please indicate the source for reprint

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.