Simulate. NET WebForm in JSP development

Source: Internet
Author: User
Tags config

WebForm is an event-driven, control state can be automatically maintained between HTTP requests, and the use of post code is a good way to separate the appearance of the page from the logical control of the page, a change from the previous HTML, server segment code, JavaScript Hybrid Web development. Stucts provides a large number of custom tags, built by tag, form, bean, action, and configuration files, as a way of web development with an excellent MVC pattern. But compared with its webform, the stucts more complex, the need to work together more elements, the effect of solving problems less than webform significant (only personal views).

In real-world development, often need to deal with a number of form controls in a page, and to deal with the page may be caused by multiple events, after the event triggered, and then request the same page, and need to maintain the state between the request, in the page to deal with all these, real annoying. Inspired by WebForm, I used JSP for the development, borrowed some of its ideas. In essence, we want to make the page display code and page control code separation, to do this is not difficult, there are many ways.

You can define a page processor (Pagehandler) for your pages, similar to the WebForm code, whose interface is basically the following:

public class Pagehandler
{
protected HttpServletRequest request;
protected HttpServletResponse response;
protected JspWriter out;
protected PageContext PageContext;
Protected HttpSession session = NULL;
protected ServletContext application = NULL;
protected ServletConfig config = null;
protected String event_action = null; Page Events
protected String event_params = null; Page parameters
Get the basic components of an action page
Public Pagehandler (PageContext page)
{
This.pagecontext = page;
This.request = (httpservletrequest) pagecontext.getrequest ();
This.response = (httpservletresponse) pagecontext.getresponse ();
This.pagecontext = page;
out = Pagecontext.getout ();
application = Pagecontext.getservletcontext ();
Config = Pagecontext.getservletconfig ();
Session = Pagecontext.getsession ();
try{
Request.setcharacterencoding ("gb2312");/Set page encoding
}
catch (Exception e)
{
E.printstacktrace ();
}
}
Initialize the parameters of the page, the specific page processor class can override this
method to initialize a page
protected void OnLoad () throws Exception
{
}
Process according to the event specified by the page
Private final void Eventbind () throws Exception
{
Event_action is obtained from the hidden field named Event_action from the page, which means the event's claim,
When this event is triggered, he looks for a method with the same name as Event_action in the page processor class
To invoke.
if (event_action!= null &&!event_action.equals (format.empty))
{
Event_params = Request.getparameter ("parameters"); Event parameter parameters, from the page
The hidden field named parameters gets
if (paramtypes[0] = = null)
{
Paramtypes[0] = Class.forName ("java.lang.String");
}
Object paramvalues[] = new OBJECT[1];
Paramvalues[0] = Event_params;
Method method = null;
Try
{
method = This.getclass (). Getdeclaredmethod (Event_action, paramtypes);
Method.setaccessible (TRUE);
}
catch (Exception e)
{
throw new Userexception ("system lacks the processing mechanism for your request: + event_action);"
}
if (method!= null)
{
Method.invoke (this, paramvalues); Call Web Time
}
}
}
Working with pages
public void process () throws Exception
{
Try
{
Event_action = Request.getparameter ("action"); Get page Events
OnLoad ()//initialization at page load
Eventbind ()//handling events
}
catch (Exception e)
{
E.printstacktrace (); ///////////////
Format.alert (out, "An unknown error occurred:" + format.getstring (E.getmessage ()));
}
}
}

Related Article

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.