Three minutes learn to use SPRINGMVC to build minimum system (Super detail)

Source: Internet
Author: User
Tags java web

Objective

Java WEB Development You must have heard of SPRINGMVC's name, as the most widely used Java framework, it has so far maintained a strong vitality and a broad user base.

This article describes how to use Eclipse to build SPRINGMVC minimum system, the so-called minimum system, is enough to make the project run successfully under the SPRINGMVC framework, and can do some simple things (such as access to the page) of the system.

Don't say much, let's get started. All source code and jar packages will be given at the end.

Body

    1. Create a new project

Paste_image.png

We use Eclipse to create a new project and select Dynamic Web Project.

Click Next

Paste_image.png

Project name is written in Springmvc, this is the name of our project, others do not change, click Finish directly.

Paste_image.png

OK, the project is ready.

Next, be sure to change the character set of the project to UTF-8

Right-click Project--properties

Paste_image.png

Change to UTF-8, click OK.

    1. Writing web. xml

When we opened the Webcontent/web-inf directory, we found that there was only one Lib directory, which is the place to store various jar packages. We know that a Web project has to have an XML file.

Since it's not, let's write one ourselves.

Right-click Web-inf--new--file and create a new Web. xml file.

Click Finish

Fill in the following contents.

Springmvc

This completes the basic configuration, I mean, now this project is already a standard Web project.

    1. To verify that the Web project is successfully built

To verify the correctness so far, we created a new JSP file under the WebContent directory.

Name is index.jsp.

Paste_image.png

The contents are as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> Congratulations, the Web project has been successfully built!

We will now deploy this project to Tomcat to verify that it is possible to run.

Right-click on the project--debug As--debug on Server

Click Finish directly

After a while, the console starts printing log information, and when we see this information, it means that Tomcat has been started.

Paste_image.png

Let's open the browser and enter the following information in the address bar

http://localhost:8088/springmvc/index.jsp

The port number for Tomcat configuration on my computer is 8088, depending on your own tomcat, which may be 8080.

Paste_image.png

Visible, the ability to successfully access the page, which shows that we have so far the operation is correct.

    1. Integrated SPRINGMVC

We add the following configuration to the Web. xml file

3.1 Configuring listeners

Org.springframework.web.context.ContextLoaderListenerorg.springframework.web.util.IntrospectorCleanupListener

3.2 Configure filter to solve post garbled problem

encodingorg.springframework.web.filter.characterencodingfilterencodingutf-8encoding/*

3.3 Configuring the Springmvc dispatcher to intercept all requests

Springmvcorg.springframework.web.servlet.dispatcherservletnamespacedispatcher-servletspringmvc/

In this configuration, we have specified that the dispatcherservlet associated XML file name is called Dispatcher-servlet.

Note that the path here is relative to Web. XML, which means that the file is also in the root directory of the Web-inf.

Therefore, we need to create a new Dispatcher-servlet.xml file in the root directory of the Web-inf.

Paste_image.png

At this point, the Web. xml file was written.

3.4 Writing Dispatcher-servlet.xml

The role of Dispatcher-servlet.xml is to configure the Springmvc dispatcher.

The configuration is as follows:

Depending on the configuration, there are three areas to note.

It scans all Java classes under the COM.SPRINGMVC package, but when it comes to annotations, such as @controller, @Service, @Autowired, they are added to the Spring Bean factory.

All the static resource files, such as JS, CSS, images all need to be placed in the/resources directory, this directory now we have not built.

All of the display pages, such as JSP files, need to be placed in the/web-inf/pages directory, which we have not built now.

OK, we'll add the corresponding directory.

The first is the directory of the Java files.

Paste_image.png

We're in this place right-click, create a new COM package, build a SPRINGMVC package inside, or use. Way to build together.

Paste_image.png

Click Finish

Paste_image.png

According to Springmvc's layering, we built three packages under the SPRINGMVC package, namely Controller, service, DAO

Paste_image.png

In this case, when our project is started, SPRINGMVC will scan the three packages, extracting all the annotated classes inside, and putting them into the spring container (or spring's bean Factory), managed by the spring container. That's why you've never been to a new controller.

Next, we'll build a directory of static resources.

Create a new resources folder under the WebContent directory.

Then, by the way, Js,css,img's folder is built, and this is where our static resource files are stored.

Paste_image.png

Finally, we have a pages folder in the Web-inf directory, which serves as a storage directory for the display pages.

Paste_image.png

Copy the previous index.jsp in.

Paste_image.png

This is almost the same as the configuration.

    1. Guide Package and Verification

We put the jar package into the Lib directory:

Paste_image.png

Then start the project and verify that the build so far is correct.

Open the Servers view and click on the icon like a beetle.

Paste_image.png

The error message was found as follows:

Paste_image.png

Error:

Could not open ServletContext resource [/web-inf/applicationcontext.xml]

It says we're missing a applicationcontext.xml under the Web-inf. This file, originally, we have less on the configuration of the Springbean factory, it means that we have to specify, when the spring container starts, what will need to automatically load things?

So, we add the applicationcontext.xml.

Paste_image.png

Inside we don't configure anything, start Tomcat again.

Paste_image.png

This time no error.

    1. Configure Viewcontroller

We know that any resource under the Web-inf directory cannot be accessed directly through the URL of the browser, ensuring security. That's why we put pages in that directory.

In order to differentiate, we have also created a separate pages folder to save these pages.

Paste_image.png

Now, in order to access this page, we need to use the SPRINGMVC page jump mechanism.

We'll create a new Viewcontroller under the controller package.

Paste_image.png

Click Finish

Paste_image.png

Viewcontroller Code:

Packagecom.springmvc.controller;importjavax.servlet.http.httpservletrequest; importorg.springframework.stereotype.controller;importorg.springframework.web.bind.annotation.requestmapping; Importorg.springframework.web.servlet.ModelAndView; @ControllerpublicclassViewController {@RequestMapping ("/view ") Publicmodelandview view (HttpServletRequest request) {String path = request.getparameter (" path ") +" "; Modelandview Mav = new Modelandview (); Mav.setviewname (path); Returnmav; }}

I just need to put the page that I want to visit in path and pass it through the URL.

Because the Java class was added, we restarted Tomcat.

When the boot is complete, enter in the Address bar:

Http://localhost:8088/springmvc/view?path=index

Results:

Paste_image.png

It's okay, let's see what he did wrong.

message/springmvc/web-inf/pagesindex.jsp

What the hell is pagesindex.jsp?

Originally, in the dispatcher-servlet.xml, we wrote less a "/"

Paste_image.png

Just add it up on the line.

Paste_image.png

After saving, we still need to restart Tomcat because the XML configuration file has been modified.

When the boot is complete, continue!

Paste_image.png

It worked.

    1. Introducing Static Resources

For example, I have a picture in the Resources/img directory, how to introduce to index.jsp?

Paste_image.png

Background:url (http://localhost:8088/springmvc/resources/img/bg.jpg); background-size:100%100%;

Indeed, this is a way. However, it has a drawback is that the root path is written dead, we certainly do not want this.

In fact, we can get the project root path in Viewcontroller, and then pass to the JSP page is OK.

Paste_image.png

We put the debug information "Congratulations, the Web project has been successfully built!" "Delete it."

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>body{background:url (${contextpath}/resources/img/bg.jpg); background-size:100%100%;

}

${contextpath} can fetch the ContextPath value that the controller passes over.

Willing to understand the framework of technology or source of Friends direct autumn: 2670716182

Three minutes learn to use SPRINGMVC to build minimum system (Super detail)

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.