Introduction to the MVC pattern

Source: Internet
Author: User
Tags stmt

Brief introduction:

People who have studied basic Java Web Development have learned how to write a basic servlet, how to write a JSP, and how to update the content displayed in the browser. But we've written our own applications in general there is no reason, for a small web site such writing no problem, but we need to write large Web projects, our existing writing mode will cause the Web application scalability is poor, And one problem cannot pinpoint where the problem is.

Java is a language with a wide range of application design patterns. At present, the 23 design patterns proposed by the mainstream can be applied in the Java language Programming program. The most widely used design pattern in Java Web applications is the MVC pattern, and most of the current mainstream web frameworks are based on MVC design patterns.

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

The main components of a Java Web application:

MVC is the abbreviation for Model-view-control, the model-view-controller. It is a server that exists in the
The model of the expression layer, which separates the application and changes the height coupling between applications.

Model (model) is the part of your application that handles application data logic. Typically, model objects are responsible for accessing data in the database.

View (view) is the part of the application that handles the display of data. Typically views are created from model data.

Controller (Controller) is the part of the application that handles user interaction. Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

Java Web Solutions:

(1) View: JSP or HTML page, displayed to the user's page, for the user to operate. It can use JS,CSS and other methods to beautify.

Index.html

<body>

<form action= "login.do" method= "POST" > <!--submitted form, action "login.do", making request is post-->

<p> account: <input type= "text" name= "id"/></p> <!--the "account" entered is labeled "ID" after the submission, and

<p> Password: <input type= "text" name= "password"/>

<p><input type= "Submit" value= "Login" > </p> <!--Form Submit button, display name "Login"-

</form>

</body>

(2) Controller: in Web. XML, the action is connected to the servlet to control the transmission of the data.

Xml

<servlet>

<servlet-name>MyServlet</servlet-name><!--Define the name of the servlet--

<SERVLET-CLASS>CC. mymvc.servlet.myservlet</servlet-class><!--corresponds to the filename that the servlet belongs to-

</servlet>

<servlet-mapping> <!--Establish a mapping between servlet and action--

<servlet-name>MyServlet</servlet-name> < name of servlet in!--map, defined--

<url-pattern>/login.do</url-pattern> <!--corresponding action--

</servlet-mapping>

Servlet.java

public void DoPost (HttpServletRequest request, httpservletresponse Response)//method for processing post requests
Throws Servletexception, IOException {
Boolean b=false;
Mypojo mypojo=new Mypojo ();//New object for Mypojo class Mypojo
Get the parameters contained in the form in the JSP file based on the name of the indicator
String id=request.getparameter ("id");
if (b) {//If the verification result is true, jump to the login Success page
Response.sendredirect ("loginsuccessful.jsp");
}
else {//If the validation result is false, jump to the login failure page
Response.sendredirect ("loginfailed.jsp");
}

(3) Model: Set up the data model, operate the database, combine the data in the database with the actual project.

public class UpdateDB {

Connection Conn;

Statement stmt;

ResultSet result;

public void DoUpdate (String sql) {

getconnection getedconnection = new getconnection ();

conn = Getedconnection.getconnection ();

try {

stmt = Conn.createstatement ();

Stmt.executequery (SQL);

} catch (SQLException e) {

E.printstacktrace ();

}

}}

Java Web Common MVC framework:

    1. Struts Framework:

Struts is one of the earliest Java open source frameworks and is an excellent implementation of MVC design patterns. Struts defines a common controller (director) that isolates the model (and the view) with a configuration file (usually Struts-config.xml), encapsulating the user request with the concept of action, making the code clearer and easier to read. Struts also provides simplified coding tools such as automatically populating the requested data into objects and page labels. Struts can develop large Java Web projects.

    1. Hibernate framework

Hibernate is an open source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that maps Pojo to database tables, is a fully automated ORM framework, and Hibernate automatically generates SQL statements, automatically executes, So that Java programmers can use object programming thinking to manipulate the database at will. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence.

    1. Spring Framework

One of the biggest aims of spring is to make the development of the Java EE easier. At the same time, spring is different from a single-layer framework such as struts and hibernate because spring is committed to providing a unified, efficient way to construct the entire application, and to combine the single-layer framework with the best combination to create a coherent system. It can be said that spring is a framework that provides a more complete development environment, which provides enterprise-class services for Pojo (Plain old Java object) objects.

Introduction to the MVC pattern

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.