Build the SPRINGMVC framework from scratch and the simplest Hello World Instance __ Framework

Source: Internet
Author: User
Tags mysql in
1Java Environmental Requirements:

1.1JDK 1.6

Download is: Jdk1.6.0_45-windows-x64.exe

Installation path:


Unzip the JDK after downloading, my decompression path is: C:\Program Files (x86) \java

Add environment variables,

A, property name: Java_home

Property value: C:\Program\Files\Java\jdk1.6.0_02

B, property name: PATH

Attribute value:;%java_home%\bin;%java_home%\jre\bin

C, attribute name: CLASSPATH

Property value:.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar

Note that you want to add. Represents the current path, in addition,%java_home% refers to the previously specified java_home

Add Java_home,


Then continue with the new classpath and PATH environment variables:

Don't forget the last number, which means the current path.

Add after the PATH environment variable


At this point, the Java JDK is well configured.


2 Installing Eclipse

Note that the JDK and Eclipse versions are not related to the version of the operating system on your computer,

The main JDK and eclipse requirements are the same version, either 32-bit or 64-bit,

Otherwise, starting eclipse will make an error.

I downloaded is: eclipse-jee-luna-sr1a-win32_20150313.zip

The direct decompression can be used. Extract directory:


You can open eclipse by double-clicking the. exe executable file.


3.tomcat

Server Tomcat 6.0

Download apache-tomcat-6.0.43.tar.gz

Direct decompression can be used, do not install, extract the directory:


Environment variables are configured as JDK, adding a tomcat path to the environment variable is fine:

catalina_base=h:\apache-tomcat-6.0.43
catalina_home=h:\apache-tomcat-6.0.43


Next, configure Tomcat in eclipse:

Adds a new server runtime configuration. Click on the Eclipse menu, "window"/"Preferences", and expand "Server"/"Runtime Environments",

This way, Tomcat is configured.


4.mysql

Next start installing the database

Download the. zip version first and extract it directly: Mysql-5.6.26-winx64.zip

After download decompression, decompression path: H:\mysql

Next, modify the configuration file My-default.ini


In the environment variable, add the MySQL path behind the path

Avoid having to enter a full path every time the command in the bin directory is executed.

Next, check to see if the database was installed successfully:

To run cmd in admin mode:

Enter the Mysql/bin directory and execute the command mysqld-install

This allows you to start MySQL, and then execute the command: net start MySQL to connect to the database:



Enter command after successful connection: Mysql-u root-p

Will enter the database as root user, the default password is empty, directly press ENTER can



Input command: show databases; You can view the database

Enter command: Use test to indicate that the test database is used

and creates a new table in the test database with the Create command.

The name of the table is YY, with 3 fields, ID, name, mail.

Note: 1 database is plural, 2 each SQL statement should be in English semicolon ";" End.



Inserts a record into the YY table with an INSERT INT statement:

Then use the SELECT statement to query:


This indicates that the database has been successfully installed and can be used properly.


It is obviously not convenient to manage the database in command-line mode,

Next you can download a software that has the UI to manage MySQL: navigate

Download software: Navicat for Mysql.rar

Follow the prompts after installation, run navigate can.


First create a new connection, and then set the host and port number, and so on.


You can then manage the database in navigate:


The data in the database can be clearly seen on the above.

You can also modify the data directly above, add data, and other operations.

The database is now installed.


Then the next step is to connect MySQL in Eclipse.

First you need to download the MySQL driver Mysql-connector-java before you can successfully connect eclipse to the database

In the MySQL official website only. Mis, personal preference to. zip version, in csdn can download,

Download: Mysql-connector-java-5.1.5.zip, then unzip directly.

Inside there is a jar bag: Mysql-connector-java-5.1.5-bin.jar

This is the jar pack driver that eclipse must have to connect to MySQL.


Put this jar bag in the Tomcat installation directory Common/lib

It would be nice to add the jar bag after the project has been built.

So the software is basically configured well.


Ready to create a project and build a framework to implement Hello World


5.Dynamic Web Project

Next, start a new dynamic Web Project

The direct new one is good, here the project is named Springmvc.

Click Finish a project is built.

The directory is as follows:



Next, take the Mysql-connectro-java-5.1.5-bin.jar in the previous step

Copy to Web-inf/lib directory


Then select the project name, right mouse button, select Properties at the bottom-> Java build path->library-> Add Library-> User Library->add External J ARs ...




This connects the MySQL and Eclipse drivers.

The rest is to set the connection parameters in the configuration file so that eclipse can access the database.

Here, we have the Java environment, ECLIPSE,MYSQL,TOMCAT configuration.


6.SpringMVC Frame

The next step is to start building a simple SPRINGMVC framework.

First you need to have the jar package ready.


Copy these jar packages to the Web-inf/lib directory,

Next, create a new index.jsp file in the WebContent directory

<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "
    pageencoding=" iso-8859-1 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


Create a new two profile in the Web-inf directory, web.xml and Spring-servlet.xml respectively

The Web.xml file code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name>Spring3MVC</display-name> &LT;SERVL Et> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web. Servlet. Dispatcherservlet</servlet-class> <!--Load-on-startup: Initializes the servlet when the container is started;--> <load-on-sta rtup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring&lt ;/servlet-name> <!--Url-pattern: Indicates which requests are given to spring Web MVC processing, and "/" is used to define the default servlet mappings. --> <!--can also be used as "*.html" to block all requests that have HTML extensions.
       --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--Since this request has been submitted to the spring WEB MVC framework for processing because This we need to configure the spring configuration file,--> <!--default Dispatcherservlet will load the Web-inf/[dispatcherservlet servlet name, which is the spring]-above Servlet.xml configuration file. --> <!--spring-servlet.xml--> </web-app>



The Spring-servlet.xml code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context=
  "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans
  Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:component-scan Base-package= "Net.spring.controller"/> <!--Configure Viewresolver--> <!--in the spring configuration file Internalresourceviewresolver: Used to support servlet, JSP view parsing; Viewclass:jstlview means that JSP template pages need to use JSTL tag libraries. The classpath must contain JSTL related jar packages, prefix and suffix: the prefix and suffix of the Lookup View page (the prefix [logical view name] suffix), such as the logical view passed in named Hello, the JSP view page should be stored in the "web-inf/ jsp/hello.jsp "--> <bean id=" Viewresolver "class=" Org.springframework.web.servlet.view.UrlBasedViewResolver "> &Lt;property name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/> <property name= "Prefi" X "value="/web-inf/jsp/"/> <property name=" suffix "value=". jsp "/> </bean> </beans>



Create a new hello.jsp file under the JSP directory

<pre name= "code" class= "Java" ><%@ page language= "java" contenttype= "text/html"; Charset=iso-8859-1 "
    pageencoding=" iso-8859-1 "%>
<%@ 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" >
< html>


Create a new package under the Java Resources folder: Net.spring.controller,

Create a new Helloworldcontroller.java file under the package directory

The code is as follows:

Package Net.spring.controller;
 
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.ModelAndView;
 
@Controller public
class Helloworldcontroller {
 
 @RequestMapping ("/hello") public
 Modelandview HelloWorld () {
 
  String me = "Hello world, Spring 3.0!";
  Modelandview: Contains the model data and logical view names that you are trying to implement//message the name of the
  data, the names used in the view to refer to
  //me are the corresponding values
  //hello are the names of the corresponding views.
  Modelandview Modelandview = new Modelandview ();
  Modelandview.addobject ("message", me);
  Modelandview.setviewname ("Hello");
  return modelandview;
  
  The above 4 statements are equivalent to one sentence:
  //return new Modelandview ("Hello", "message", Me);
 }


Finally, the entire project's catalogue is this:



In this way, the framework is already set up and can be implemented to run the project on local server Tomcat.

When you start running, select the item, right-click, and select Run as-> run on Server

Then select



This allows the project to be allowed.

Enter address in browser: http://localhost:8080/springmvcdemo/

You can see the welcome interface for INDEX.JSP Rendering:


Click Say Hello hyperlink, you can jump to the hello.jsp page


Here, our project is basically successful.

Now, Hello World only implements Web access based on a simple SPRINGMVC framework, which can be followed by database access.



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.