java main启动spring_gradle 搭建springMVC项目,main函数启动
1
在intellij idea中創建gradle項目,完成后創建src文件夾以及下面的文件夾及文件,如下圖所示
2
web.xml內容
<?xml version="1.0" encoding="UTF-8"?> ? ?!-- Spring的log4j監聽器 --> ? ? ? ? ? ?org.springframework.web.util.Log4jConfigListener ? ? ? ? ? ? ? ? ? ?contextConfigLocation ? ? ? ?classpath:applicationContext-*.xml ? ? ? ? ? ? ? ?dispatcher ? ? ? ?org.springframework.web.servlet.DispatcherServlet ? ? ? ? ? ? ? ? ? ?加載/WEB-INF/spring-mvc/目錄下的所有XML作為Spring MVC的配置文件 ? ? ? ? ? ?contextConfigLocation ? ? ? ? ? ?/WEB-INF/spring-mvc/*.xml ? ? ? ? ? ? ? ?1 ? ? ? ? ? ? ? ?dispatcher ? ? ? ?*.htm ? ? ? ? ? ? ? ?login.html ? ?
3
dispatcher-servlet.xml內容
4
build.gradle內容/
// Apply the java plugin to add support for Javaapply plugin: 'java'apply plugin : 'jetty'apply plugin:"war"ext { ? ?springVersion = "4.0.7.RELEASE"}// In this section you declare where to find the dependencies of your projectrepositories { ? ?// Use 'jcenter' for resolving your dependencies. ? ?// You can declare any Maven/Ivy/file repository here. ? ?maven { url 'http://maven.oschina.net/content/groups/public/'}; ? ?jcenter()}[compileJava,compileTestJava,javadoc]*.options*.encoding = "UTF-8"// In this section you declare the dependencies for your production and test codedependencies{ ? ?compile "org.eclipse.jetty:jetty-server:9.0.0.M3" ? ?compile "org.eclipse.jetty:jetty-servlet:9.0.0.M3" ? ?compile "org.eclipse.jetty:jetty-webapp:9.0.0.M3" ? ?compile "org.springframework:spring-webmvc:$springVersion" ? ?compile "org.springframework:spring-tx:$springVersion" ? ?compile "com.fasterxml.jackson.core:jackson-databind:2.3.1" ? ?compile "commons-fileupload:commons-fileupload:1.3.1" ? ?compile "org.hibernate:hibernate-core:3.6.10.Final" ? ?compile "org.hibernate:hibernate-core:3.6.10.Final" ? ?compile "javax.servlet:javax.servlet-api:3.1.0" ? ?compile "javax.servlet.jsp:jsp-api:2.2.1-b03" ? ?compile "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1" ? ?compile "org.apache.taglibs:taglibs-standard-impl:1.2.1" ? ?testCompile "org.springframework:spring-test:$springVersion" ? ?testCompile "junit:junit:4.11"}jettyRun{ ? ?webAppSourceDirectory file("$projectDir/src/main/webapp") ? ?httpPort 8080 ? ?contextPath project.name ? ?scanIntervalSeconds 0 ? ?reload "automatic"}
5
在intellij idea 里面的控制臺運行gradle,將依賴下載下來
6
Main.java文件內容
package com.yang;import org.eclipse.jetty.server.Connector;import org.eclipse.jetty.server.Handler;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.handler.DefaultHandler;import org.eclipse.jetty.server.handler.HandlerList;import org.eclipse.jetty.server.handler.ResourceHandler;import org.eclipse.jetty.servlet.ServletContextHandler;import org.eclipse.jetty.servlet.ServletHolder;import org.eclipse.jetty.webapp.WebAppContext;import java.io.File;/** * Created by yangguangyue on 15/12/23. */public class Main { ? ?public static void main(String[] args) throws Exception{ ? ? ? ?Server server = new Server(8090); ? ? ? ?WebAppContext webAppContext = new WebAppContext(); ? ? ? ?webAppContext.setContextPath("/"); ? ? ? ?String webappDirLocation = "src/main/webapp/"; ? ? ? ?String absolutePath = new File(webappDirLocation).getAbsolutePath(); ? ? ? ?webAppContext.setResourceBase(absolutePath); ? ? ? ?webAppContext.setDescriptor(absolutePath + "WEB-INF/web.xml"); ? ? ? ?webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader()); ? ? ? ?webAppContext.setConfigurationDiscovered(true); ? ? ? ?webAppContext.setParentLoaderPriority(true); ? ? ? ?server.setHandler(webAppContext); ? ? ? // server.setHandler(handlers); ? ? ? ?server.start(); ? ? ? ?server.join(); ? ?}}
7
然后再Main.java里面右鍵運行,在瀏覽器中訪問:
http://localhost:8090//index.htm,
會請求TestController中的index方法,之后轉發到test.html中
END
總結
以上是生活随笔為你收集整理的java main启动spring_gradle 搭建springMVC项目,main函数启动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java mp4 视频时间戳_MP4文件
- 下一篇: c++ 命名规则 private_【译】