Transformation continues: eclipse integration tomcat development spring mvc project configuration overview, tomcatmvc

Source: Internet
Author: User

Transformation continues: eclipse integration tomcat development spring mvc project configuration overview, tomcatmvc

 

In the environment configuration in the previous article, you can only develop a javase project based on maven. To develop a web project, you must configure tomcat and spring mvc. The integration list is as follows.

 

I. Tomcat installation

In. net web development, Microsoft once again showed you one-stop mosaic configuration. You only need to click a button to launch the mosaic configuration... A page with a bootstrap framework is displayed in front of you.

Java does not have such a good thing, basically it is HD without code.

 

1.

Release 9.0, here I select Version 8.5.

 

 

 

2. Configure Environment Variables

<1> On windows, it is usually installed in the: C: \ Program Files \ Apache Software Foundation \ Tomcat 8.5 directory by default. There is

Apache Tomcat 8.5 Tomcat8 service items, even if the installation is complete.

<2> On the centos platform, the project must be deployed on linux, wget url, decompress the package, and run startup. sh.

[root@localhost myapp]# cd tomcat[root@localhost tomcat]# lsbin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work[root@localhost tomcat]# cd bin[root@localhost bin]# lsbootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh            tool-wrapper.shcatalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar       version.batcatalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native.tar.gz  version.shcatalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tool-wrapper.bat[root@localhost bin]# ./startup.shUsing CATALINA_BASE:   /usr/myapp/tomcatUsing CATALINA_HOME:   /usr/myapp/tomcatUsing CATALINA_TMPDIR: /usr/myapp/tomcat/tempUsing JRE_HOME:        /usr/mysoft/java/jdk1.8Using CLASSPATH:       /usr/myapp/tomcat/bin/bootstrap.jar:/usr/myapp/tomcat/bin/tomcat-juli.jarTomcat started.[root@localhost bin]# 

 

We can see from the above that tomcat has been started. By the way, the default port of Tom is 8080. You can verify it through netstat-tln and check whether the startup is normal.

 1 [root@localhost bin]# netstat -tln 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address           Foreign Address         State       4 tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6 tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7 tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      8 tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      9 tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     10 tcp6       0      0 :::8009                 :::*                    LISTEN     11 tcp6       0      0 :::111                  :::*                    LISTEN     12 tcp6       0      0 :::8080                 :::*                    LISTEN     13 tcp6       0      0 :::22                   :::*                    LISTEN     14 tcp6       0      0 ::1:631                 :::*                    LISTEN     15 tcp6       0      0 ::1:25                  :::*                    LISTEN     16 tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN  

2. Integration of eclipse and tomcat

The integration of the two is relatively simple. Generally, you can do the following three things.

1. Specify your tomcat version in eclipse windows> references> server> enveriment runtime, for example

For apache tomcat 8.5, click 'Next' to specify the tomcat installation path and your jre running version.

 

2. Right-click the web Project and choose Properties> Java Build Path> Libraries> Add Library> Server Runtime> Add tomcat.

Switch to order and export and select tomcat.

 

After creating a web project, you will find that The project has an 'error message ': the superclass "javax. servlet. http. HttpServlet" was not found on The Java Build Path.

 

This is because your web project has not noticed the jsp Container tomcat. In this case, you need to right-click the Web Project and add the tomcat library in the Java Build Path in the Properties panel.

Let's take a look at the title. After everything is done, we can solve this problem.

 

3. Add a jstl template engine

This is optional. If the jstl template is missing when the spring mvc project you created is running, you can reference it in pom. xml.

1         <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->2         <dependency>3             <groupId>javax.servlet</groupId>4             <artifactId>jstl</artifactId>5             <version>1.1.2</version>6         </dependency>

 

Iii. Install spring mvc

So far, the integration of tomcat and eclipse has come to an end. The next step is to install and configure spring mvc. You can pull it in the maven repository, just like

All the necessary jar packages such as spring-core and spring-aop will be installed for you.

 

1. web. xml configuration

There is a web. xml file under src-> main-> webapp-> WEB-INF, which is equivalent to web. config in. net. In asp.net mvc, mvchandler is used

Request take over. This mode is also applicable in spring mvc. For example, the Servlet to take over is DispatcherServlet, and the detailed configuration of web. xml is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 5 <display-name> Archetype Created Web Application </display-name> 6 7 <! -- Url request interceptor --> 8 <servlet> 9 <servlet-name> spring </servlet-name> 10 <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name> spring </servlet-name> 15 <url-pattern>/< /url-pattern> 16 </servlet-mapping> 17 18 <! -- Character Set filter --> 19 <filter> 20 <filter-name> encodingFilter </filter-name> 21 <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> 22 <init-param> 23 <param-name> encoding </param-name> 24 <param-value> UTF-8 </param-value> 25 </init-param> 26 <init-param> 27 <param-name> forceEncoding </param-name> 28 <param-value> true </param-value> 29 </init-param> 30 </filter> 31 <filter-mapping> 32 <filter-name> encodingFilter </filter-name> 33 <url-pattern>/* </url -pattern> 34 </filter-mapping> 35 36 </web-app>

 

2. spring-servlet.xml Configuration

We know that spring is actually a big bean container, and the configuration and management of classes can be lost to spring. Because spring mvc adopts the 'annotation mode' here, we need to define the 'bundle'

Scan range. The definition of the file name here should be clear: <$>-servlet. xml, where $ is web. <servlet-name> spring </servlet-name> In xm

This file is placed in the WEB-INF folder to facilitate merging and reading tomcat containers when loading, detailed configuration is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" 4 xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: mvc = "http://www.springframework.org/schema/mvc" 5 xsi: schemaLocation = "http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/s Pring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd11 http://www.springframework.org/schema/mvc12 http://www.springframework.org/schema/mvc/spring-mvc.xsd "> 13 14 <! -- Configure the scan package --> 15 <context: component-scan base-package = "com. datamip. qncrm. controller"> </context: component-scan> 16 17 <! -- View parser --> 18 <bean19 class = "org. springframework. web. servlet. view. internalResourceViewResolver "> 20 <property name =" viewClass "value =" org. springframework. web. servlet. view. jstlView "/> 21 <property name =" prefix "value ="/WEB-INF/views/"> </property> 22 <property name =" suffix "value = ". jsp "> </property> 23 </bean> 24 25 </beans>

 

3. Create an mvc view folder to store all view pages.

In the spring-servlet.xml file configuration section of the 'view resolution', you can see that all the jsp pages are placed under the views folder, here I create a new index. jsp file, details are as follows:

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4 

 

4. Create an mvc controller.

Under the src/main/Java directory in java Resources, I can create a new HomeController. java. The details are as follows:

1 package com. datamip. qncrm. controller; 2 3 import org. springframework. stereotype. controller; 4 import org. springframework. web. bind. annotation. requestMapping; 5 import org. springframework. web. bind. annotation. requestMethod; 6 7 @ Controller 8 public class HomeController {9 10 // route matching, using index. jsp page rendering 11 @ RequestMapping (value = "/home/index", method = RequestMethod. GET) 12 public String Index () {13 return "index"; 14} 15}

 

Okay. Basically, the spring mvc configuration is over. Next we Run As-> Run As Server on the qncrm project directory, and the execution is successful ~~~, Compared with one site in. net

Style configuration has indeed increased a lot of difficulty, but one sentence is good, it is hard to make a technical programmer into no technology. If you understand the urlRoutingModule and MvcHandler of asp.net mvc,

The principles are the same. I hope this article will help you. ZIP file download

Related Article

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.