Quickly build the Spring MVC 4 development environment

Source: Internet
Author: User
Tags apache tomcat

Spring MVC as a springframework product, since the birth of the day, has been the attention of a wide range of developers, now spring MVC development in Java is thriving, and now if developers say, do not understand spring MVC, may be people laughed. Sensational words will not say, first tell you how to build the spring MVC development environment.


(i) Working environment preparation:

JDK 1.7

Eclipse Kepler

Apache Tomcat 8.0


(ii) Create a new Maven project in Eclipse, in the archetype type, select "Maven-archetype-webapp".


(iii) configuration of Pom.xml.

<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.favccxx.favsoft</groupId> <artifactId> Favspringmvcrestful</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</ version> <name>favspringmvcrestful Maven webapp</name> <url>http://maven.apache.org</url > <properties> <spring.version>4.1.1.RELEASE</spring.version> </properties> < dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</artifact id> <version>3.8.1</version> <scope>test</scope> </dependency> <d       Ependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependen cy> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-web mvc</artifactid> <version>${spring.version}</version> </dependency> <dependency&gt        ; <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <ver sion>${spring.version}</version> </dependency> <dependency> <groupid>org.springfra Mework</groupid> <artifactId>spring-context</artifactId> <version>${spring.version}& lt;/version> </dependency> <dependency> <groupId>jstl</groupId> <ar         tifactid>jstl</artifactid> <version>1.2</version> </dependency> <dependency> <groUpid>taglibs</groupid> <artifactId>standard</artifactId> <version>1.1.2</versi on> </dependency> </dependencies> <build> <finalname>favspringmvcrestful</final Name> </build></project>

(iv) in Web-inf/web.xml, configure Spring MVC forwarding.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <display-name>favspringmvcrestful</display-name> < Filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web . filter. Characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-nam            e> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </        init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.cont Ext. contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>springmvc </servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class > <init-param> <param-name>contextConfigLocation</param-name> <param- Value>classpath*:spring-context.xml</param-value> </init-param> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> &LT;SERVLET-NAME&GT;SPRINGMVC&LT;/SERVLET-NAME&G        T <url-pattern>/</url-pattern> </servlet-mapping></web-app>

(v) Under the Resources directory, create spring-context.xml, support annotations, page path resolution, and so on.

<?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:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <context:component-scan base-p Ackage= "Com.favccxx.favsoft.favjson.controller" ></context:component-scan> <mvc:annotation-driven ></mvc:annotation-driven> <bean id= "Viewresolver" class= "org.springframework.web.servlet.view.UrlBase Dviewresolver "> <property name=" Viewclass "VAlue= "Org.springframework.web.servlet.view.JstlView"/> <property name= "prefix" value= "/web-inf/views"/> <property name= "suffix" value= ". jsp"/> </bean></beans>


(vi) Create a new Hellocontroller class and use annotations to complete the call of the spring MVC class.

Package Com.favccxx.favsoft.favjson.controller; Import Java.util.hashmap;import Java.util.Map; Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.servlet.ModelAndView; @Controllerpublic class Hellocontroller {         @RequestMapping ("/greeting") public    Modelandview Greeting (@ Requestparam (value= "name", defaultvalue= "World") String name) {         System.out.println ("Hello" + name);         map<string, object> map = new hashmap<string, object> ();         Map.put ("UserName", name);         return new Modelandview ("/hello", map);    }      }


(vii) Creation of/WEB-INF/VIEWS/HELLO.JSP for the presentation of data.


<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> <! DOCTYPE html>


(eight) in the browser, enter the URL of the access:Http://localhost:8080/favspringmvcrestful/greeting?name=%E7%BE%8E%E5%A5%B3, showing Hello Springmvc, indicates that spring The MVC development Framework was built successfully.

Quickly build the Spring MVC 4 development environment

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.