The mapping mechanism of Java __java

Source: Internet
Author: User
Tags reflection
The mapping mechanism of Java and the application of XML in Strutszhjong

1. Description of the problem
In the Java development of Web applications, we have to frequently use access to the database, but in the development may encounter many problems, such as user input string is not legitimate, insert data format is not, network connectivity interruption, such as a series of problems, Java provides the mechanism of exception handling, This greatly simplifies the developer's code to provide good code quality and program robustness, and in struts we typically have something basic to develop, For example, to have Struts-conf.xml file old procedures to configure the process, actionform old data transmission, action to deal with the process of business processing, although we can in the Actionform to verify the request data and legality, but in the background of the program in operation we do not know what the meaning of Unexpected situation, only in the Actionform verification can not eliminate the illegal situation, we can in the background to deal with, this time we draw out the Java mapping mechanism, of course, the use of the mapping mechanism in struts is not only to help us deal with the exception, Still, this can improve our development speed, but also less action and the number of actionform, although struts also has this aspect of the class, but I think it is not good enough, can not simplify our work. At the same time, in the development of a high security system, how to ensure the security of information has gradually become a developer's topic, the general way is directly to the request object to pass the table but to the background of the data, in struts used in the ACTIONFORML to complete, But we have no other way, the answer is yes, that is, XML, and I will discuss the Java mapping mechanism and XML application in Strutszhjong, if there is any wrong place please treatise.

Mapping mechanism of 2.java
Java does not support dynamic languages, although it is recently rumored that Sun will add dynamic language features to the next JVM, but now we have no way to apply, but fortunately, because the mapping mechanism in Java allows us to complete the dynamic program, In fact, the Java mapping mechanism is to meet the needs of our development in a timely manner, also for us to use dynamic language has laid a certain basis for thinking. Java mapping mechanism in our development we are to use. I want to identify a scene: join you to build a platform for an educational institution, Using Java and struts, in development we want to develop action for each page, in action we have to do the necessary business, now we want to develop the student management work, b including the increase of student information, modify and delete, We're going to build 3 action and 3 Actionform (the latter is a actionform), or we'll create an action and a actionform, of course we're going to inherit Dispatchaction, Using dispatchaction to develop struts is really handy, and I'm here to say that this class is really good, and I'm not against you not to use it, but here I just found a better way. To implement these three businesses we must be involved in the operation of the database, we cannot guarantee that the operation of the database will be abnormal, the conventional way is that we are here directly to catch the exception and return to the error page, where I do not intend to do so, because I think I recommend the method is a better way, To implement my framework, our first step would be to create a class with the following code:

Import java.lang.reflect.*;
Import java.io.IOException;
Import java.util.Properties;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;

Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import org.apache.struts.action.ActionMapping;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionErrors;
Import Org.apache.struts.action.ActionError;
public class Baseaction extends Action
{Public Actionforward perform (actionmapping mapping,
Actionform form,
HttpServletRequest request,
HttpServletResponse response)
Throws IOException, Servletexception
{
String methodname= "";
Try
{
String Actionmethod = Request.getparameter ("Actionmethod"); The customer came in to call that method
Call different methods depending on the action of the form.

Get Method Name
methodname = "Do" + Actionmethod;

Invoke the appropriate method with reflection
class[] param = {
Class.forName ("org.apache.struts.action.ActionMapping"),
Class.forName ("Org.apache.struts.action.ActionForm"),
Class.forName ("Javax.servlet.http.HttpServletRequest"),
Class.forName ("Javax.servlet.http.HttpServletResponse")};
object[] obj = {mapping, form, request, response};
Method method = This.getclass (). GetMethod (methodname, param);
Actionforward forward = (Actionforward) method.invoke (this, obj);
Savetoken (Request);

return forward;
}
catch (nosuchmethodexception ee)
{
Request.setattribute ("error", EE);
To pass the exception
return processforwardbyexception (mapping, EE, request, response);
}
}
}

In our shoes every action to inherit this class, and in the class directly to write their own method can be, but also can achieve a few JSP page common One action purpose, is not a bit like dispatchaction that, indeed very similar to Europe. But we found that We've caught an anomaly in all these categories, this means that when we implement our approach, we don't have to think about what the anomaly is, we just throw it, it reduces and a lot of the capture code, and we see that we put it in the request after the exception we caught, This can be directly in the JSP to get him, it is convenient to give users a hint of what is the reason for the operation did not perform successfully, is not cool.

So let's take a look at why we need to map that.
Not very good answer, join you have used PHP words, you'll understand the great potential of dynamic languages, and there's no mapping at all in PHP, although there's a mechanism, but I don't think he does, and in PHP he has a lot of functions and custom functions that we don't know when to call that function, Or to call the function when the form came to a parameter, we can do this to the Lai processing, look at the following code fragment
<?php
$moth =$_post ("moth");
We're going to call the name is $moth worth the way we just do it
$moth ();
?>
Assuming that our URL is http://127.0.0.1/test.php?moth=Show, then the show () method will be executed in the program, which is not always possible in Java, we only use the mapping mechanism, he is so inadvertently give us a lot of freedom , we don't need to write a bunch of if or switch to easily execute the code we want, increases the flexibility of the program, after we add a new class, just put it on the Web server, we will see the effect immediately, because the Web cache does not have this class, This solves the problem that when we put new things on the Web server, we can't react immediately, only restarting the server is the only way to clear the cache manually.

3 the application of XML in Web development.

Indeed, now the XML is very popular, Webservic development is to promote his development, and some people assert that XML will replace HTML, today I am not only concerned about the development of XML in Webservic, but on one side to talk about his value.

Or let's assume a scenario: Suppose a bank is going to do a system, and anyway she doesn't want the user to see the original data back in his background, how do we do that, and the separation of pages and data is a good choice, so we can use XML boldly. Of course this is a prerequisite, you have to understand the parsing of XML, whether you are using DOM or xsl The latter is XPath does not matter, anyway, is to achieve the separation of the page and data. General users can not simply see the source code and analysis of your return to the customer data.

4 Summary
    Java mapping mechanism and XML in web development not only This, Java mapping mechanism in some Kaiyuan framework reuse very much, the most famous spring,struts,hibnater all use of Java reflection mechanism, It can be seen that his function to the user a great degree of freedom, XML in the face of data encapsulation, format, transmission embodies the advantages of more, specific reference to the SOAP books to the XML has a more profound understanding, here is not described.  
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.