Gradle one minute to achieve SPRING-MVC

Source: Internet
Author: User
Tags dateformat locale testng jcenter build gradle

Premise:

1, the JDK is installed

2, There's IntelliJ Idea.

3, Gradle Installed

One-minute implementation Steps

1,mkdir SPRING-MVC;CD Spring-mvc
2,gradle Init
3,edit build.gradle File

/* * This build file is auto generated by running the Gradle ' init ' task * by ' Administrator ' at ' 16-10-2 pm 8:42 ' with Gr Adle 3.1 * * This generated file contains a commented-out the sample Java project to get you Started. * For more details take a look at the Java Quickstart chapter in the Gradle * User Guide available at Https://docs.gradle.        org/3.1/userguide/tutorial_java_projects.html */buildscript {repositories {jcenter ()} Dependencies { Classpath ' org.akhikhl.gretty:gretty:+ '}}ext {springversion = ' 4.3.3.RELEASE '}//apply from: ' https://raw.gi Thub.com/akhikhl/gretty/master/pluginscripts/gretty.plugin ' Apply plugin: ' org.akhikhl.gretty ' Apply plugin: ' Java ' Apply plugin: ' War ' apply plugin: ' eclipse ' apply plugin: ' idea '//' on ' section you declare where to find the Dependencie    S of your projectrepositories {//use ' jcenter ' for resolving your dependencies.    You can declare any maven/ivy/file repository Here. Jcenter () mavencentral () maVen {url ' http://repo.spring.io/release '}}//in This section you declare the dependencies for your production and test Codedependencies {//core Spring Compile ("org.springframework:spring-context: $springVersion") compile ("org.  Springframework:spring-core: $springVersion ") compile (" org.springframework:spring-webmvc: $springVersion ")//the Production code uses the SLF4J logging API at compile time compile ("org.slf4j:slf4j-api:1.7.21")//Declare the de    Pendency for your favourite the test framework you want to use in your Tests. TestNG is also supported by the Gradle Test Task. Just change the//testcompile dependency to Testcompile ' org.testng:testng:6.8.1 ' and add//' test.usetestng () ' to    Your build Script. Testcompile ("junit:junit:4.12")}
Build Gradle

4,mkdir src\main\java; mkdir src\main\resources; mkdir Src\main\webapp
mkdir Src\main\webapp\web-inf
mkdir src\test\java; mkdir src\test\resources
5, the project into idea, because Gradlew idea this command will generate some extra files, do not like garbage superfluous things, so directly import

6,web-inf Add the Web. XML and Spring-mvc-servlet.xml files below

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.1"xmlns= "http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">    <servlet>        <Servlet-name>Spring-mvc</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Spring-mvc</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping></Web-app>
Web. XML
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"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-conte Xt.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "
    >    <Mvc:annotation-driven></Mvc:annotation-driven>    <Context:component-scanBase-package= "org.springframework.samples"/>    <BeanID= "viewresolver"class= "org.springframework.web.servlet.view.InternalResourceViewResolver">        < propertyname= "prefix"value= "/web-inf/views/"/>        < propertyname= "suffix"value= ". jsp"/>    </Bean></Beans>
Spring-mvc-servlet.xml

7, according to the 6th step of Spring-mvc-servlet.xml configuration, you also need to create a views folder under the Web-inf folder, and a new file home.jsp

<%--  Created by IntelliJ Idea.  User:administrator  2016/10/2 0002  21:53  This template use File | Settings | File Templates. --%><%@ page contenttype= "text/html;charset=utf-8" language= "java"%>     Helloworld! 
home.jsp

8,homecontroller.java

 packageorg.springframework.samples;Importorg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.stereotype.Controller;Importorg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;Importorg.springframework.web.bind.annotation.RequestMethod;Importjava.text.DateFormat;Importjava.util.Date;Importjava.util.Locale;/*** Created by Administrator on 2016/10/2 0002.*/@Controller public classHomeController {Private Static FinalLogger Logger = Loggerfactory.getlogger (homecontroller.class); /**     * .     * Simply selects the home view to render by returning it Name.     * . */@RequestMapping (value= "/", method =Requestmethod.get) publicString Home (locale locale, model Model) {logger.info ("Welcome home! The client locale is {}. ", locale); Date Date=NewDate (); DateFormat DateFormat=dateformat.getdatetimeinstance (dateformat.long, dateformat.long, locale); String formatteddate=Dateformat.format (date); Model.addattribute ("servertime", formatteddate); return"home"; }}
Homecontroller.java

9,

Gradlew Tomcatrun

ten, http://localhost:8080/Spring-MVC/

Gradle one minute to achieve SPRING-MVC

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.