On what is MVC design pattern __spring

Source: Internet
Author: User

What is MVC.

The MVC full name is Model view Controller, an abbreviation for Models-View-Controller (Controller), an example of software design, which organizes code using a business logic, data, and interface to display a separate method. Clustering business logic into a single component, without having to rewrite the business logic while improving and customizing 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.


Other words:

Model is the part of an application that handles the logic of application data.
Typically, the model object is responsible for accessing data in the database. View is the part of your application that handles the display of data.
Usually the view is created from the model data. The Controller (Controller) is the part of the application that handles user interaction.
The controller is usually responsible for reading data from the view, controlling user input, and sending data to the model.
MVC is a framework pattern that makes it mandatory to separate the input, processing, and output of an application. The MVC application is divided into three core components: models, views, controllers. Each of them handles its own task. The most typical MVC is the JSP + servlet + JavaBean pattern

Model: Commonly used JavaBean to achieve, through a variety of classes to the database data acquisition, and encapsulated in the object.

View: Common JSP to achieve, through the direct observation of the JSP page to show the data we get from the database.

Controller: Common servlet to implement, through the servlet to obtain the JavaBean wrapped objects (the data stored in the database), and then send data transfer to the JSP interface.


There may be people here who want to know what JavaBean is:

JavaBean is a reusable component written in the Java language. To write JavaBean, the class must be concrete and public, and have a constructor without parameters. JavaBean exposes member properties to the internal domain by providing a public method that conforms to the consistent design pattern, which is obtained by the long and class methods. As we all know, attribute names conform to this pattern, and other Java classes can discover and manipulate the properties of these javabean through an introspection mechanism.

JavaBean generally consists of three parts:

1. Property

2. Methods

3. The event

In fact, JavaBean is a Java class that encapsulates properties and methods to become an object that has a function or process a business, referred to as a bean. Because JavaBean is based on the Java language, JavaBean does not rely on the platform, with the following characteristics: 1. Can realize the reuse of code 2. Easy to write, easy to maintain, easy to use 3. Can be used on any platform that has a Java running environment, without recompiling the summary: vulgar, JavaBean is a simple class, but this class has a set (), Get () method, and the attributes in the class are privatized (private), The method is public, and there is a parameterless construction method, and if you want to set up a constructor with parameters, then write the parameterless constructor again, because the default constructor is overwritten with the constructor method with the parameters.
Examples are given in Classic mode (JSP + servlet + JavaBean): Scenario: We display information from the table in the database on the JSP page. This is the data for the database:
We need to create a dynamic Web project, and then pour two jar packages into this project.

2 ways to pour into a project:

1. Set up the MAVEN project, pour the jar package into the Pom.xml file <dependency></dependency>

<!--https://mvnrepository.com/artifact/javax.servlet/jstl-->
<dependency>
    <groupId> javax.servlet</groupid>
    <artifactId>jstl</artifactId>
    <version>1.2</version >
</dependency>

<!--Https://mvnrepository.com/artifact/mysql/mysql-connector-java-->
<dependency>
    <groupId>mysql</groupId>
    <artifactid>mysql-connector-java </artifactId>
    <version>5.1.39</version>
</dependency>
2. Download Jar pack, then pour into webcontent/web-inf/lib directory (don't lead the wrong place)


Msyql-connector-java-5.1.39.jar: Connecting to the database using the.

Click to open the link

Jstl-1.2.jar: Used for data iterative output of a database on a JSP page.

Click to open the link

On what is JSTL and its usage, bloggers recommend this blog post:

Click to open the link




Model (models)

(Create two packages, a model package to establish a connection database and get data, a Modeluser package to build the object user that holds the data)


Mysqlconnection.java:

Package Com.ricky.model;

Import Java.io.FileInputStream;
Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.util.Properties;

public class Mysqlconnection {
	/**
	 * connects to the MySQL database, returns a connection
	 * @return/public
	static Connection Getconnectiion () {
		Connection conn=null;
		Properties P=new properties ();
		try {
			p.load (new FileInputStream ("e:/Code god/mvc/src/jdbc.properties"));
			Class.forName ("Com.mysql.jdbc.Driver");
			Conn=drivermanager.getconnection (p.getproperty ("IP"), P.getproperty ("username"), P.getproperty ("password"));
			System.out.println (p.getproperty ("IP"));
		} catch (IOException e) {
			e.printstacktrace ();
		} catch (ClassNotFoundException e) {
			e.printstacktrace ();
		catch (SQLException e) {
			e.printstacktrace ();
		}
		Return conn
	}
}

Getuser.java:

Package Com.ricky.model;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;

Import java.util.List;

Import Com.ricky.modelUser.User; The public class GetUser {/** * is used to get information from the database and then store the information in a user object * @return return a list of user objects/public static List<user
		> Getuserlist () {list<user> userlist=new arraylist<user> ();
		Connection conn=mysqlconnection.getconnectiion ();
		Statement Stat=null;
		ResultSet Rs=null;
			try {stat=conn.createstatement ();
			Rs=stat.executequery ("SELECT * from user");
				while (Rs.next ()) {String id=rs.getstring ("id");
				String username=rs.getstring ("username");
				String password=rs.getstring ("password");
				System.out.println (id+ "" +username+ "" +password);
				User User=new User (Id,username,password);
			Userlist.add (user);
		} catch (SQLException e) {e.printstacktrace ();
				}finally{try {rs.close ();
				Stat.close (); Conn.Close ();
			catch (SQLException e) {e.printstacktrace ();
	} return userlist;
 }
}


User.java:

Package com.ricky.modelUser;

public class User {
	private String ID;
	Private String username;
	private String password;
	Public String GetId () {return
		ID;
	}
	public void SetId (String id) {
		this.id = ID;
	}
	Public String GetUserName () {return
		username;
	}
	public void Setusername (String username) {
		this.username = username;
	}
	Public String GetPassword () {return
		password;
	}
	public void SetPassword (String password) {
		this.password = password;
	}
	Public User (String id,string username,string password) {
		this.id=id;
		This.username=username;
		This.password=password
	}
}


View (views)

(Create a simple JSP page to display the data)


Here you need to refer to Jstl in the JSP file (there is a blog post with a detailed description of the usage):

<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>


USERLIST.JSP:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> ;!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

Controller (Controller)

(Create Userservlet to accept database data object user, control data object output to JSP page)

Userservlet.java:

Package Com.ricky.controller;
Import java.io.IOException;

Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Com.ricky.model.GetUser;

Import Com.ricky.modelUser.User; /** * Servlet Implementation class Userservlet/@WebServlet ("/userservlet") public class Userservlet extends HTTPSERVL
       
    Et {private static final long serialversionuid = 1L;
    /** * @see httpservlet#httpservlet () * * * Public userservlet () {super (); /** * @see Httpservlet#doget (httpservletrequest request, httpservletresponse response) * * protected void doget (Ht Tpservletrequest request, HttpServletResponse response) throws Servletexception, IOException {list<user> userLis
		T=getuser.getuserlist ();
		Set a UserList property to upload to the following JSP page Request.setattribute ("UserList", userlist); ReqUest.getrequestdispatcher ("/userlist.jsp"). Forward (request, response); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated
	Method stub doget (request, response);
 }

}


Run Userservlet:



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.