In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Summary: technology selection (only part of the technology is listed) service framework: Dubbo, zookeeper, Rest service cache: Redis, ehcache message middleware.
Preface
This article introduces how to use eclipse to build the minimum system of SpringMVC step by step, 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 visiting pages).
Without much to say, let's get started. All source code and jar packages will be given at the end.
Other environments:
Operating system: Windos 10
Tomcat: v7.0
JDK: 1.7
Technology selection (only part of the technology is listed)
1. Backend
Service framework: Dubbo, zookeeper, Rest services
Cache: Redis, ehcache
Message Middleware: ActiveMQ
Load balancing: Nginx
Distributed files: FastDFS
Database connection pooling: Alibaba Druid 1.0
Core framework: spring framework
Security framework: Apache Shiro 1.2
View frame: Spring MVC 4.0
Server verification: hibernate Validator 5.1
Layout framework: SiteMesh 2.4
Workflow engine: Activiti 5.15
Task scheduling: quartz 1.8.5
Persistence layer framework: MyBatis 3.2
Log management: SLF4J 1.7, Log4j
Utility classes: Apache Commons, Jackson 2.2, Xstream 1.4, Dozer 5.3, POI
2. Front end
Js framework: jQuery 1.9.
CSS framework: Bootstrap 4 metronic
Client authentication: jquery Validation Plugin.
Rich text: CKEcitor
File management: CKFinder
Dynamic tabs: Jerichotab
Data sheet: jqGrid
Dialog box: jQuery jBox
Tree structure control: jQuery zTree
Other components: Bootstrap 4 metronic
3. Support
Server middleware: Tomcat 6, 7, Jboss 7, WebLogic 10, WebSphere 8
Database support: currently only provide support for MySQL database, but not limited to database, the next version upgrade multi-data source switching and database read-write separation: such as: Oracle, SqlServer, H2, etc.
Support development environment: Eclipse, MyEclipse, Ras, Idea, etc.
Classic introduction:
Source structure
More detailed source code reference sources
Text 1. Create a new project
Paste_Image.png
We create a new project with eclipse and select Dynamic Web Project (dynamic Web project).
Click Next
Paste_Image.png
Write springmvc in Project name, which is the name of our project. Do not change anything else, just click Finish.
Paste_Image.png
OK, the project will be completed.
Next, be sure to change the character set of the project to UTF-8
Right-click the project-properties
Paste_Image.png
Change to UTF-8 and click OK.
two。 Write web.xml
When we open the WebContent/WEB-INF directory, we find that there is only one lib directory inside, which is where all kinds of jar packages are stored. We know that a web project must have a web.xml file.
Since we don't have one, let's write one ourselves.
Right-click WEB-INF--new--file and create a new web.xml file.
Click Finish
Just fill in the following.
Springmvc
This completes the basic configuration. I mean, this project is now a standard web project.
3. Verify that the web project is built successfully
To verify the correctness so far, we create a new jsp file under the WebContent directory.
His name is index.jsp.
Paste_Image.png
The contents are as follows:
Congratulations, the web project has been successfully built!
Let's deploy this project to Tomcat now to verify that it can be run.
Right click on the project-- Debug As--Debug on Server
Click Finish directly
After a while, the console starts printing log messages, and when we see these messages, 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 of Tomcat on my computer is 8088, depending on your own Tomcat, it may be 8080 and so on.
Paste_Image.png
It can be seen that we have been able to successfully visit the page, which shows that what we have done so far is correct.
3. Integrated SpringMVC
We add the following configuration to the web.xml file
3.1 configure listeners
Org.springframework.web.context.ContextLoaderListener
Org.springframework.web.util.IntrospectorCleanupListener
3.2 configure the filter to solve the POST garbled problem
Encoding
Org.springframework.web.filter.CharacterEncodingFilter
Encoding
UTF-8
Encoding
/ *
3.3 configure a SpringMVC dispatcher to intercept all requests
Springmvc
Org.springframework.web.servlet.DispatcherServlet
Namespace
Dispatcher-servlet
Springmvc
/
In this configuration, we specify that the name of the associated XML file for DispatcherServlet is dispatcher-servlet.
Note that the path here is relative to web.xml, that is, the file is also in the root directory of WEB-INF.
So, we need to create a new dispatcher-servlet.xml file in the root directory of WEB-INF.
Paste_Image.png
At this point, the preparation of the web.xml file is over.
3.4.Writing dispatcher-servlet.xml
The purpose of dispatcher-servlet.xml is to configure the SpringMVC dispatcher.
The configuration is as follows:
According to the configuration, there are three points to pay attention to.
It scans all the Java classes under the com.springmvc package, but whenever it encounters annotations, such as @ Controller, @ Service, @ Autowired, it adds them to Spring's bean factory.
All static resource files, such as js, css, and p_w_picpaths, need to be placed in the / resources directory, which we haven't built yet.
All display pages, such as jsp files, need to be placed in the / WEB-INF/pages directory, which we haven't built yet.
OK, let's add the corresponding directory.
The first is the directory of the Java file.
Paste_Image.png
We right-click in this place, create a new com package, and then build a springmvc package in it, or use. The way to build it together.
Paste_Image.png
Click Finish
Paste_Image.png
According to the layering of SpringMVC, we build three packages under the springmvc package, namely controller, service, and dao.
Paste_Image.png
In this way, once our project is started, springmvc will scan the three packages, extract all the annotated classes inside, put them into the Spring container (or Spring's bean factory), and manage them uniformly through the Spring container. That's why you've never been to new a Controller.
Next, let's create a directory of static resources.
Create a new resources folder under the WebContent directory.
Then, by the way, set up all the js,css,img folders, and here we store our static resource files.
Paste_Image.png
Finally, we create a pages folder under the WEB-INF directory as the directory where the display pages are stored.
Paste_Image.png
Copy in the previous index.jsp.
Paste_Image.png
This is almost done with the configuration.
5. Package guidance and verification
We put the jar package in the lib directory:
Paste_Image.png
Then start the project to verify that the build so far is correct.
Open the Servers view and click on the icon as if the image is a beetle.
Paste_Image.png
It is found that there is an error. The error message is as follows:
Paste_Image.png
Error:
Could not open ServletContext resource [/ WEB-INF/applicationContext.xml]
It says that we are missing an applicationContext.xml file under WEB-INF. It turns out that we have lost the configuration of the SpringBean factory, which means that we have to specify what needs to be loaded automatically when the Spring container starts.
So, we add applicationContext.xml.
Paste_Image.png
We don't configure anything inside, start Tomcat again.
Paste_Image.png
Don't make a mistake this time.
5. Configure ViewController
We know that any resource in the WEB-INF directory cannot be accessed directly through the browser's url address, which ensures security. That's why we put all the pages in this directory.
To make a difference, we also set up a separate pages folder to save these pages.
Paste_Image.png
Now, in order to access this page, we need to use SpringMVC's page jump mechanism.
We create a new ViewController under the Controller package.
Paste_Image.png
Click Finish
Paste_Image.png
ViewController Code:
Package com.springmvc.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class ViewController {@ RequestMapping ("/ view")
Public ModelAndView view (HttpServletRequest request) {
String path = request.getParameter ("path") + ""
ModelAndView mav = new ModelAndView ()
Mav.setViewName (path); return mav
}
}
I just need to put the page I want to visit in path and pass it in through url.
Because the java class is added, we restart Tomcat.
When the startup is complete, enter:
Http://localhost:8088/springmvc/view?path=index
Results:
Paste_Image.png
It doesn't matter. Let's see what he says is wrong.
Message / springmvc/WEB-INF/pagesindex.jsp
What the hell is pagesindex.jsp?
It turns out that we are missing a "/" in dispatcher-servlet.xml.
Paste_Image.png
Just add it.
Paste_Image.png
After saving, we still need to restart Tomcat because we modified the XML configuration file.
After the startup is complete, continue!
Paste_Image.png
Succeed.
6. Introduce static resources
For example, if I put a picture in the resources/img directory, how can I introduce it to index.jsp?
Paste_Image.png
Background: url (http://localhost:8088/springmvc/resources/img/bg.jpg);background-size: 100%
Indeed, this is one way. However, one drawback is that the root path is dead, and we certainly don't want that.
In fact, we can get the project root path in viewController and pass it to the jsp page to OK.
Paste_Image.png
We put the debugging message "Congratulations, the web project has been successfully built!" Delete.
Body {background: url (${contextPath} / resources/img/bg.jpg); background-size: 100%
}
${contextPath} can get the contextPath value passed by Controller.
Welcome to study and study related technologies, friends who are willing to understand the framework technology or source code directly ask: 2042849237
For more details, source code reference: http:// × × / technology
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.