Play with spring gossip MVC Framework and the "moderate" approach (I)

Source: Internet
Author: User
(Except for some specific plots with specific time points of the story, this story is purely fictitious. If there are similarities, it is a coincidence .) If you are interested in this article, read the first two articles in this series: play with spring STRUTS + hibernate + spring add deletion and modification query examples and play with spring hibernate + webwork + spring add deletion and modification query examples, otherwise you will not understand. In this article, the program runs the effect, first faster: http://www.easyjf.com/html/bbs/20060516/3391914101561546.htm? EJB id = 8666340670359399 Starring:Easyjweb, the moderate way, spring.
   Supporting Role:Struts, webwork, velocity, JSP, hibernate, and J2EE programmers. In the previous two examples, we mainly introduced spring's golden and vulgar combination and gorgeous and elegant combination to develop B/S applications. Among the several "Wheels" we use, Hibernate is recognized as the best ORM system today, so it is not highly controversial. However, Struts is not necessarily the case. Struts is also a much-used framework on the market (although many technical experts say it is not). However, many people are very inconspicuous about struts, I always think that there is no gorgeous IOC, and I cannot write elegant and beautiful code. Some configuration and formbean are very troublesome. Therefore, webwork or tapestry is much better than others. On the Internet, sometimes some people may even look at struts learners with contempt.
Is webwork really strong? The market is the king. I can't help wondering if I have introduced some IOC and interceptor functions and integrated them with some common components. The existence is reasonable, so there is no need for fans to compete for this PK. Exactly. Now struts and webwork development teams have been merged (why? Of course, it is to deal with common enemies. But what should we do for fans of tapestry, JSF, and spring MVC? Haha, isn't it very interesting? I don't want to invent the wheel, and I have invented another beautiful wheel to give it to us, so that no one of us in the third world can leave it alone, if you are not careful about it, you will be faced with a select phobia. It seems that as a programmer, the body and spirit must be strong. Otherwise, who can be able to get up! This is not the case. The customer is desperately waiting to see the progress of the project, and we have not decided on any framework!
  
A little dizzy. Today, I don't want to use a framework that everyone is familiar with as an example, so I don't have to worry about how many fans compete. (I have to translate them once to see that their PK is also depressing, sometimes, when you are not careful, you can run it !). This article will replace struts and webwork in the above example with a domestic open-source framework you have never seen. This framework is the easyjweb framework developed by the easyjf open-source team in China. Speaking of easyjweb, I think fans of the various MVC frameworks mentioned above will surely join in to form a super lineup that requires technical skills and qualifications. Because easyjweb is made in China, and others are imported from the original version. In addition, the easyjweb test version was only sent to version 0.5. Many places are still immature, and the technical skills of several developers are not very good, the writing is also so mediocre and simple, and you will be caught with the braid accidentally. The easyjweb development team is eager to introduce this framework to you, so that you can provide suggestions and improvements.
Since it is still incomplete, why should I use easyjweb instead of others? The reason is the same as that of other frameworks, because I am a super fan of easyjweb and I don't support anyone else. To put it bluntly, this is like a girl with big eyes and long hair in the village, in terms of skin, appearance, character, habits, and other aspects, branny is more suitable for being a wife than that in the United States.Besides, I have also used a lot of frameworks, large and small, and I feel that easyjweb is the most convenient to use. It is easy to understand the working principles of the framework with less code, the functions provided are also frequently needed in our project. It is ridiculous and sad that some domestic counterparts and media platforms saw the easyjweb word, which was made in China at first glance. In other words, they said they were making advertisements and writing articles were not allowed to be posted. Even if I hadn't seen Huang PIV in the street or a passer-by with black eyes on the street, I thought I was in a western capitalist country, this resistance to things of the socialist countries of the masses. It's also an open-source project. You can share struts's experiences. Why don't you share easyjweb's experiences? (Look, who said they wouldn't send it to you? I want to send it. I want to get the gold coins !) Why? Now the WTO has been around for so many years. You translate and collect the dynamics of foreign small items all day long. Those who do not know that you are the propaganda tongue of a capitalist country in China.
Of course, this is just a few special cases. Most predecessors and platforms still support us. Without the encouragement and guidance of many Chinese predecessors and the support of many professional technical platforms similar to those of the Java research organization, I am afraid easyjweb has been a long time ago and we will not see this article today. Dizzy, gossip is a little far away. Let's start with the question below. Today, I will discuss how to replace the MVC layer in the previous two examples of "add, delete, modify, and query" without moving other layers. This is also an art in the multi-layer structure that I understand. It is also a wonderful place for everyone to hear about components or components. What did the elders say? In the era of configuration programming, building software is like building blocks, with several boxes, frames, and components built together. A huge system is piled up by us. If some of the components need to be changed for various reasons, you can change the components without any changes. The flexibility and maintainability of J2EE are reflected in these aspects. Due to the flexibility of Java, it sometimes brings us a feeling that is unique to art workers. This is what I gave up Microsoft. net.
Because easyjweb is used in this example, fans of other MVC frameworks can skip the subsequent technical details and directly jump to the subsequent discussions about the "moderate means, otherwise, you will be fascinated by easyjweb. Next, let's take a look at the procedure. You can refer to the MVC section in the previous two examples: 1. Write the action to process user requests.Similar to struts, easyjweb has an action, but because of the interface method used, this action does not need to inherit from the framework-specific action. You only need to implement iwebaction. In this example, to make it easier for our actions to directly inherit an action base class provided to us in easyjweb tools (you do not need to inherit it), the following is usermanageaction. java code.
Package com. easyjweb. Action; import java. util. arraylist;
Import java. util. collection;
Import com. easyjf. util. commutil;
Import com. easyjf. Web. actioncontext;
Import com. easyjf. Web. globals;
Import com. easyjf. Web. module;
Import com. easyjf. Web. page;
Import com. easyjf. Web. webform;
Import com. easyjf. Web. Tools. abstract=action;
Import com. easyjf. Web. Tools. ipagelist;
Import com. easyjf. Web. Tools. listquery;
Import com. easyjf. Web. Tools. pagelist;
Import com. easyjf. example. Business. iuser;
Import com. easyjf. example. Business. iuserservice; import org. springframework. Web. Context. webapplicationcontext;
Import org. springframework. Web. Context. Support. webapplicationcontextutils; public class usermanageaction extends act1_action {
Private iuserservice userservice;
Public iuserservice getuserservice (){
Return userservice;
}
Public void setuserservice (iuserservice userservice)
{
This. userservice = userservice;
}
Public object dobefore (webform form, module ){
Webapplicationcontext WAC = webapplicationcontextutils. getrequiredwebapplicationcontext (actioncontext. getcontext (). getservletcontext ());
This. userservice = (iuserservice) WAC. getbean ("userservice ");
Return NULL;
}
Public page doinit (webform form, module ){
Return doquery (Form, module );
}
Public page donew (webform form, module ){
Return New Page ("edit", "/useredit.html", globals. page_template_type );
}
Public page doadd (webform form, module ){
Page forward = NULL;
Iuser OBJ = (iuser) form2obj (form );
If (userservice. Save (OBJ ))
{
Form. addresult ("MSG", "data added successfully! ");
Forward = doquery (Form, module );
}
Else
{
Form. addresult ("MSG", "Data addition failed ");
Forward = new page ("edit", "/useredit.html", globals. page_template_type );
}
Return forward;
}
Public page doupdate (webform form, module ){
Page forward = NULL;
Iuser OBJ = (iuser) form2obj (form );
If (userservice. Update (OBJ ))
{
Form. addresult ("MSG", "data modification successful! ");
Forward = doquery (Form, module );
}
Else
{
Form. addresult ("MSG", "data modification failed ");
Forward = new page ("edit", "/useredit.html", globals. page_template_type );
}
Return forward;
} Public page doedit (webform form, module ){
Page forward = NULL;
Iuser OBJ = (iuser) form2obj (form );
If (OBJ! = NULL)
{
Form. addpo (OBJ );
Forward = new page ("edit", "/useredit.html", globals. page_template_type );
}
Else
{
Form. addresult ("MSG", "No data found! ");
Forward = doquery (Form, module );
}
Return forward;
} Public page dodel (webform form, module ){
Iuser OBJ = (iuser) form2obj (form );
If (userservice. Del (OBJ ))
{
Form. addresult ("MSG", "data deleted successfully! ");
}
Else
{
Form. addresult ("MSG", "data modification failed ");
}
Return doquery (Form, module );
}
Public page doquery (webform form, module ){
Int currentpage = commutil. null2int (Form. Get ("page "));
Int pagesize = commutil. null2int (Form. Get ("pagesize "));
If (currentpage <1) currentpage = 1;
If (pagesize <1) pagesize = 15;
String scope = "1 = 1 ";
Collection paras = new arraylist ();
String ordertype = commutil. null2string (Form. Get ("ordertype "));
String orderfield = commutil. null2string (Form. Get ("orderfield "));
String username = commutil. null2string (Form. Get ("queryusername "));
String Tel = commutil. null2string (Form. Get ("querytel "));
If (! Username. Equals (""))
{
Scope + = "and username like? ";
Paras. Add ("%" + username + "% ");
}
If (! Tel. Equals (""))
{
Scope + = "and Tel like? ";
Paras. Add ("%" + Tel + "% ");
}
If (orderfield. Equals ("") // sort by user name by default
{
Orderfield = "username ";
Ordertype = "DESC ";
}
If (! Orderfield. Equals (""))
{
Scope + = "order by" + orderfield;
If (! Ordertype. Equals ("") Scope + = "" + ordertype;
}
Ipagelist plist = new pagelist (New listquery (userservice. Query (scope, paras )));
Plist. dolist (pagesize, currentpage ,"","");
// Save the query result
Commutil. saveipagelist2webform (plist, form );
Return New Page ("list", "/userlist.html", globals. page_template_type );
}
Public object form2obj (webform form ){
String cid = (string) form. Get ("CID ");
Iuser user = NULL;
If (CID! = NULL &&(! CID. Equals ("") user = userservice. Read (CID );
If (user = NULL) user = userservice. newuser ();
Return form. Topo (User );
}
} 2. Create a template pageSimilar to the JSP page in the struts example, the template page here uses the velocity script engine. Therefore, there are no related syntaxes in JSP, and there are no dizzy custom tags. There are only four methods to sum up all the tags. You can refer to an article on the easyjf open source team's official website entitled: an Analysis of the elegant design and examples of the view layer in the MVC framework.
The template is no longer stored in the root directory, but in the Web-INF/easyjwebdirectory. The sample is two templates. A useredit.htmltemplate is used to display the user's access table list, and another userlist.html file is the user list page. All content of the template can be downloaded from the official website of the easyjf open-source team. The complete code in this example contains the template. : Http://www.easyjf.com/html/bbs/20060516/3391914101561546.htm? EJB id = 8666340670359399 3. Modify the Web. xml file as follows:<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app>
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationcontext. xml </param-value>
</Context-param>
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>
<Filter>
<Filter-Name> charsetfilter </filter-Name>
<Filter-class> com. easyjf. Web. charsetfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-Name> ignore </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> charsetfilter </filter-Name>
<Servlet-Name> easyjf </servlet-Name>
</Filter-mapping>
<Servlet>
<Servlet-Name> easyjf </servlet-Name>
<Servlet-class> com. easyjf. Web. actionservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> easyjf </servlet-Name>
<URL-pattern> *. EJF </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> easyjf </servlet-Name>
<URL-pattern>/EJF/* </url-pattern>
</Servlet-mapping>
</Web-app> the configuration file is similar to the previous two examples. It only adds some Chinese filtering and processing content. easyjweb has considered the frequently encountered Chinese problems. We can compare struts with easyjweb, we have two fewer steps, one is the definition of actionform bean, the other is the configuration struts-config.xml file. In fact easyjweb has a configuration file struts-config.xml similar to the easyjf-web.xml, but we use the default configuration in this example. Complete sample code download: http://www.easyjf.com/html/bbs/20060516/3391914101561546.htm? Ejid = 8666340670359399 of course, the reason why we can replace the system MVC layer without modifying other code is that the IOC container of spring manages other layer components for us. Of course, the two examples only use the simplest and most commonly used functions of spring.

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.