Website development: The perfect combination of struts and hibernate

Source: Internet
Author: User
Keywords Struts

Intermediary transaction SEO diagnosis Taobao guest Cloud host technology Hall

In the recent course design, we are required to use the Struts framework and the Hibernate framework to complete a student elective system. Many students complain about not learning the two frameworks and complain about why they don't use the MVC pattern because they already know the MVC.

So I have to say that there are two modes of JSO Web development.

Mode1

Simply put, the Mode1 pattern is also divided into two situations, one that is developed entirely using JSP and the other is developed using Jsp+javabean. The only advantage of both schemes is that it is suitable for small web development, such as a simple personal homepage, and using the MODE1 mode is your preferred option.

Of course, if your project is small enough to use pure JSP to develop, so that your maintenance will be easier. Because it eliminates the hassle of modifying JavaBean, you need to recompile to ". Class" files.

And the shortcomings of Mode1 can not be neglected, in the development process, a large number of logic processing code in the JSP and JavaBean, which led to increased complexity of the javabean, increased relevance (also called coupling), we are in the pursuit of perfect development is "high cohesion low coupling", So in the development of large-scale projects, we will still leave the Mode1 mode aside.

  

MODE1 schematic

Mode2

Mode2 is a servlet as the main body, through the servlet to accept customer requests, call different JavaBean processing, return the results to the JSP page, so clear division of labor, efficiency, and facilitate project maintenance, is the first choice for large-scale website construction. In Mode2, there is a design pattern called MVC. The design pattern is divided into model layer, display layer and control layer.

  

MVC design Pattern

So understanding MVC and struts

If you find it hard to understand what MVC and struts are all about, you can argue that MVC is a defined standard interface (interface), and that struts is the perfect implementation of the MVC interface. Besides STRUTS,EJB is also an implementation of MVC.

The combination of struts and hibernate

It is not difficult to understand that struts is used to get form data in the entire "Student selection system" design process, hibernate is used to persist objects to the database. The basic design process is:

One: interface design, first of all you need to the front page structure perfect, give users a good impression.

  

This is the front landing interface I designed.

Second: Consider the persistence layer: In the current project, "Student elective system" when students mainly, each student can choose more than one subject, so there is a one-to-many relationship, in the hibernate use of tags to map the relationship. The export mapping is related to the database. You can persist object relationships to a database.

Three: Struts process: through struts to obtain form data, and then by calling Hibernate HQL statements for data validation, is the key to landing verification. After the whole system has been modified, I will post the specific code for your reference. For the verification of the foreground data, I did not use JavaScript and was using the Validaate validation framework of struts, if you do not understand this, you can refer to <>.

About hibernate and MySQL coding issues

Coding problems are the most maddening problem, using hibernate in Chinese characters, and the database does have a bunch of "???" Annoying. What we need to do is to encode the unity. and "UTF-8" encoding advantage I do not say, it can support the noon code display, so we are in the construction of the database, the last manually set mode encoding, CREATE database test DEFAULT Charset=utf8.

Then what needs to be modified is the hibernate encoding. Programme I:

In the "Hibernate.cfg.xml" file, you should write the "URL" configuration as follows:

XML Code

<property name= "Connection.url" >jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding= Utf-8</property>

Programme II:

1, the MySQL installation when setting the character set Utf-8 (including the database character set, and the table character set), JDBC driver with the latest.

2.hibernate configuration file (Hibernate.cfg.xml), plus properties

XML Code

&lt;property name= "Connection.useunicode" &gt;true&lt;/property&gt; &lt;property name= " Connection.characterencoding "&gt;UTF-8&lt;/property&gt;

3. Web.xml Set Filter

XML Code

&lt;filter&gt; &lt;filter-name&gt; Set Web creator Character Encoding &lt;/filter-name&gt; &lt;filter-class&gt; Com.util.setencodefilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;defaultencoding&lt;/ param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &nbsp; &lt; filter-mapping&gt; &nbsp; &lt;filter-name&gt; &nbsp; Set Web creator Character Encoding &nbsp; &lt;/filter-name&gt; & nbsp; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &nbsp; &lt;/filter-mapping&gt;
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.