Eclipse+tomcat+maven Configuration Spring MVC Graphics tutorial

Source: Internet
Author: User
Tags parent directory

I. Basic Environment (ECLIPSE+TOMCAT+JDK+MAVEN)

1. Install JDK:

Download and install the JDK (the process is simpler to skip).

Configure environment variables: As required by the Java default installation path:

Java_home:c:\program files\java\jdk1.6.0_07 (Note that there is no semicolon behind the java_home path.) )

Path:%java_home%\bin;

Classpath:%java_home%\lib\dt.jar;%java_home%\lib\tools.jar;

Entering Javac under the console indicates a successful installation.

2. Install Eclipse:

This time using Eclipse 4.2, code-named Juno. (No installation version, other versions of caution, tested Helios version, according to this document configuration will have errors).

Just extract to the specified directory.

3. Install Tomcat:

Install plugin First: Download address: http://www.eclipsetotale.com/tomcatPlugin.html#A3. I'm using a tomcatpluginv33.zip. Directly unzip the plugin into the Eclipse installation directory-plugins directory.

Then put the tomcat file into the C-packing directory (optional).

4. Association

Eclipse Associated JDK, Tomcat:

In preferences-java-installed JREs, add-Select the JDK installation directory in JRE home. (Special note: Not the JRE file, the JDK.) If the original JRE is to be replaced with JDK, the following figure

In Eclipse's preferences-server-runtimeenvironmen, add-chooses the associated version of Tomcat (select 7.0 here), browse-selects the Tomcat root, and the JRE chooses the installed JRE.

Then select the Tomcat version you installed in the Tomcat option and select Tomcat root, OK. Input in Browser: http://localhost:8080/. You can check to see if Tomcat was successfully installed.

5. Install Maven:

Install the plugin first: Enter the MAVEN lookup in Eclipse's marcketplace, select Maven Integration Foreclipse, and follow the wizard installation to restart Eclipse. There's maven in your new project and prefrences.

Then extract the Maven file to the C-packing directory (optional). (Maven file please download to the website, Link: http://www.apache.org/dyn/closer.cgi/maven/maven-3/3.0.4/binaries/apachemaven-3.0.4-bin.zip)

Last associated action: Select the Maven file directory-conf file-Settings.xml in Preferences's maven-user settings. Ok.

Two: Use and configure the environment

1. New MAVEN Project:

File-new-other-maven-maven Project---Direct next--Enter WebApp in Filte (here for Web projects): Select a second, as shown in:

After the next step. The following figure:

The Group ID typically writes a large project name. The artifact ID is the child project name. (I write iflytek,sms here separately), click Finish. Maven's Web project is built.

To create a good MAVEN project:

2. Build the Maven General directory structure.

In a MAVEN-managed project, the file directory is generally fixed, and we build a generic webmaven below

The structure of the target directory. Build and run a sample demo of Hello.

(1) Create source folder folders:

Create a new folder under the Workspace-project name-src-main: java. Similarly, create a new test folder in the parent directory src directory, and create a new folder under test: java. In Eclipse right-click Refresh, the directory is out. (a problem occurs if you create a new source folder directly).

(2) New spring configuration file: (mvc-dispatcher-servlet.xml)

Find Src-main-webapp-web-inf in the project tree, and create a new folder in this directory named:

pages, used to store JSP files later (this will make the project look a little clearer). At the same time in Web-inf

Record to create a new file named: Mvc-dispatcher-servlet.xml

Edit Mvc-dispatcher-servlet.xml File:

<beans xmlns = "Http://www.springframework.org/schema/beans"

Xmlns:context = "Http://www.springframework.org/schema/context"

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

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

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

<context:component-scanbase-package= "com"/>

<mvc:annotation-driven/>

<bean

class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name= "prefix" >

<value>/WEB-INF/pages/</value>

</property>

<property name= "suffix" >

<value>.jsp</value>

</property>

</bean>

</beans>

Where the <context:component-scanbase-package= "com"/> represents a package read at compile time (a new package under Classpath, In the next few pages of the document I am a new com.sms.controller so write this, scan all files in the COM directory,<mvc:annotation-driven/> representative annotation driver, <value>/web-inf/ Pages/</value> represents the path of the front-end controller looking for a JSP.

(3) Edit web.xml:

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:javaee= "Http://java.sun.com/xml/ns/javaee"

xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee Http://java.sun.com/xml/ns/j2ee/webapp_

2_4.xsd "id=" webapp_id "version=" 2.4 ">

<display-name>spring Web MVC application</display-name>

<servlet>

<servlet-name>mvc-dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>mvc-dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

The important configuration is the <param-value>/web-inf/mvc-dispatcher-servlet.xml</param-value> in <context-param>; It indicates the directory of the spring configuration file. (Of course, it can be configured in a classpath:xxx way, and multiple profiles can also be separated by ",")

The <url-pattern>/</url-pattern> in <servlet-mapping>, which represents the front-end interceptor that will intercept all files.

The special attention here is <servlet-name>:mvc-dispatcher, because to correspond to the <param-value>/web-inf/mvc-dispatcher-servlet.xml. For example, if <servlet-name> is Mydispatcher, then the corresponding <param-value> must be mydispatcher-servlet.xml.

(4) Modify the Pom.xml (Note: modify):

On the basis of the original pox.xml, the modification between <dependencies></dependencies> is as follows:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>3.1.2.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.1.2.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>3.1.2.RELEASE</version>

</dependency>

Add the Tomcat plug-in on the next line of <finalName>: That is, add the following:

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>tomcat-maven-plugin</artifactId>

<version>1.1</version>

</plugin>

</plugins>

After the change, I became the following:

<

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.