Eclipse uses MAVEN to build the Spring MVC Graphics tutorial _java

Source: Internet
Author: User

in this paper, we introduce Eclipse uses MAVEN to build the detailed steps for spring MVC for your reference, as follows

1. Environment configuration

a). Java 1.7

b). Eclipse Luna

c). Maven3.2.5

d). Spring 4.1.4

2. Create Maven Project

a). Open Eclipse,file->new->project->maven->maven Project


b). Next step


c). Select the project you created as WebApp, and next


d). Fill in the project's group ID and artifact ID. Under normal circumstances, the group ID write the reverse of the domain name, artifact ID write the project name. The final point is complete.


e). After the initial construction, the project directory structure is as follows


f). In the General project directory, in the Java Resources directory, there are src/main/java,src/main/test/java,src/main/test/resources three source folders that need to be created manually. The following steps will cover how to make up these three directories.

3, modify the basic settings of the project

a). Right-click the project name->properties->java build path and tap the Source tab.

b). Tip Hello/src/main/java (missing) and Hello/src/test/java (missing). In the general project directory, in the Java Resources directory, there will also be src/main/test/resources this source folder. The missing is deleted first, then recreated, and the missing direct creation. Right-click the key to delete and Add.


c). Modify the complete, the effect of the following figure


D). Next modify the configuration of the libraries, the JRE uses version 1.7. Select the JRE System library->edit and replace the version.


e). And then modify the configuration in order and export, mainly to adjust the display sequence of these four directories, tune to their favorite order


f). Then modify project facets to first modify Java to 1.7.


Dynamic Web module cannot be directly modified here to 3.0, you need to open the engineering directory with a. Settings folder, open Org.eclipse.wst.common.project.facet.core.xml, and make the following modifications:

<installed facet= "Jst.web" version= "3.0"/>

Restart Eclipe to see that the change is in effect.

4, the configuration of Maven in eclipse

a). Window->properties->maven, tick download repository index updates on startup


5. Configuration of simple spring MVC

a). Open the Pom.xml file in the project and click on the Dependencies tab and click Add to add new dependencies

b). If you know the dependent group ID and artifact ID, you can fill it out directly, if not clear, you can enter the keyword to query, or to the http://search.maven.org site query


c). Dependencies that need to be added are: SPRING-WEBMVC, version 4.1.4. Release. The complete Pom.xml file reads as follows:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.springstudy</groupId> <artifactid>study</ artifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> &LT;NAME&G T;study Maven webapp</name> <url>http://maven.apache.org</url> <properties> <sp ring.version>4.1.4.release</spring.version> </properties> <dependencies> <dependency > <groupId>junit</groupId> <artifactId>junit</artifactId> &LT;VERSION&GT;3.8.1&L t;/version> <scope>test</scope> </dependency> <dependency> <groupid>org. Springframework</groupid> <artIfactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>study</finalName> </build> </projec 
 T>

D).   Open Src/main/webapp/web-inf/web.xml file, and finally modify it as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" x mlns= "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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "study" version= "2.5" > <display-name>archetype Created Web application</display-name> &LT;DESCRIPTION&G T;SPRINTMVC Environment Build </description> <!--load Spring configuration file--> <context-param> &LT;PARAM-NAME&G T;contextconfiglocation</param-name> <param-value>classpath:/configs/spring-*.xml</param-value > </context-param> <!--spring listening--> <listener> <listener-class>org.s  Pringframework.web.context.contextloaderlistener</listener-class> </listener> <!--Spring MVC configuration --> <servlet> <Servlet-name>dispatcher</servlet-name> <servlet-class> 
 
     Org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--the configuration file name and path for custom spring MVC--> <init-param> <param-name>contextConfigLocation</param-name> <param-value> Classpath:configs/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</ Load-on-startup> </servlet> <!--spring MVC request suffix--> <servlet-mapping> < Servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mappin g> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-l 
 Ist> </web-app>

e). In the Java resources/scr/main/resources directory, create a configs folder to store the configuration path declared in Web.xml

f). In the Java resources/scr/main/resources/configs directory, create the Spring-servlet.xml, which reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:util= "Http://www.springframework.org/schema/util" xsi: schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spri Ng-beans-4.1.xsd Http://www.springframework.org/schema/context http://www.springframework. Org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee http: 
 
             Www.springframework.org/schema/jee/spring-jee-4.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC Http://www.springframework.org/schEma/mvc/spring-mvc-4.1.xsd Http://www.springframework.org/schema/util Http://www.springfra Mework.org/schema/util/spring-util-4.1.xsd "> <context:annotation-config/> <context:compo Nent-scan base-package= "Com.springstudy.controller"/> <mvc:annotation-driven/> <mvc:resour Ces mapping= "/styles/**" location= "/styles/"/> <mvc:resources mapping= "/scripts/**" location= "/scripts/"/&gt 
 
   ; <mvc:resources mapping= "/images/**" location= "/images/"/> <bean class= "Org.springframework.web" . servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf/views/"/>" 
 Lt;property name= "suffix" value= ". jsp"/> </bean> </beans>

g). Create controller package, in Spring-servlet.xml file, <context:component-scan base-package= "Com.springstudy.controller"/ > A path has been specified

h). In the Src/main/webapp/web-inf directory, create the views file, in the Spring-servlet.xml file, <property name= "prefix" value= /"/> has specified the view file path

i). To create the first controller file Hellocontroller.java, the complete file contents are as follows:

Package Com.springstudy.controller;  
 
Import Org.springframework.stereotype.Controller; 
 
Import org.springframework.web.bind.annotation.RequestMapping; 
 
Import Org.springframework.web.servlet.ModelAndView;  
 
@Controller public 
 
class Hellocontroller { 
 
 
 
   @RequestMapping ("/hello") public 
 
   Modelandview Hello () { 
 
     Modelandview MV =new Modelandview (); 
 
     Mv.addobject ("Spring", "Spring MVC"); 
 
     Mv.setviewname ("Hello"); 
 
     return MV; 
 
   } 
 

j). Add the src/main/webapp/web-inf/views/hello.jsp file as follows:

<! DOCTYPE html> 
 
 
 

6. Publish the project to Tomcat

a). Add Tomcat 7 to eclipse;

B. After Tomcat is added, double-click to set the server locations settings in the Overview tab;

I. Select the Use Tomcat installation (takes control of Tomcat installation)

II. Change the content of deploy path to: WebApps

Iii. Preservation

c). Right-Tomcat,add and Remove ..., adding study


d). Start Tomcat;

e). Browser open Http://localhost:8080/study/hello, Access successful! The following figure:

The operation is over!

This is what eclipse uses Maven to build all of spring MVC, hoping to give you a reference and a lot of support for the cloud-dwelling community.

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.