Build the Spring MVC 4 development environment eight steps away

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".


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/51/69/wKiom1RjUsXTEvpEAAJU88LY5z0840.jpg "title=" Webapp.png "alt=" Wkiom1rjusxtevpeaaju88ly5z0840.jpg "/>


(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</artifactid>      <version>3.8.1< /version>      <scope>test</scope>    </ Dependency>        <dependency><groupid> org.springframework</groupid><artifactid>spring-core</artifactid><version>${ spring.version}</version></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-webmvc</artifactid><version>${spring.version}</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId> spring-beans</artifactid><version>${spring.version}</version></dependency>< dependency><groupid>org.springframework</groupid><artifactid>spring-context</ artifactid><version>${spring.version}</version>&Lt;/dependency><dependency><groupid>jstl</groupid><artifactid>jstl</artifactid ><version>1.2</version></dependency><dependency><groupid>taglibs</groupid ><artifactId>standard</artifactId><version>1.1.2</version></dependency>       </dependencies>  <build>    < Finalname>favspringmvcrestful</finalname>  </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-name><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.context.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><servlet-name>springmvc</servlet-name><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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/ Schema/mvc/spring-mvc-4.1.xsd "><context:component-scan base-package=" Com.favccxx.favsoft.favjson.controller "></context:component-scan><mvc:annotation-driven></ Mvc:annotation-driven>    <bean id= "Viewresolver"  class= " Org.springframework.web.servlet.view.UrlBasedViewResolver "&GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;<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 access URL:HTTP://LOCALHOST:8080/FAVSPRINGMVCRESTFUL/GREETING?NAME=%E7%BE%8E%E5%A5%B3, the operation effect is as follows:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/51/6A/wKiom1RjVN7ANY6wAACjCtLOZ0w950.jpg "title=" Hello.png "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:75px; "alt=" Wkiom1rjvn7any6waacjctloz0w950.jpg "/>

This example is just a simple example of the Spring MVC 4 primer, and if you want to learn more about Spring MVC, keep an eye on this blog and welcome comments from readers.



This article is from the "Dust Wind with the Sky" blog, please be sure to keep this source http://favccxx.blog.51cto.com/2890523/1575885

Build the Spring MVC 4 development environment eight steps away

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.