日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

java main启动spring_gradle 搭建springMVC项目,main函数启动

發布時間:2024/1/23 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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函数启动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。