以前斷斷續(xù)續(xù)的接觸了一些后端的東西,什么SSH 啊之類的。模模糊糊也會(huì)搭建冒黑。自從轉(zhuǎn)到移動(dòng)端之后就很少玩起來了。現(xiàn)在聽說都是基于Gradle的玩法了勤哗。所以我今天也來試試抡爹。
工具:
工欲善其事,必先利其器芒划,我這里用的是IntelliJ IDEA2016 冬竟。關(guān)于工具的注冊碼,網(wǎng)上一大堆民逼,可以搜一搜
步驟:
一. 先從創(chuàng)建工程開始 File -> New Project
這里勾選 Gradle 記得一定要勾選Java && Web 這樣可以標(biāo)識(shí)為Web工程,然后我們Next泵殴。
二.創(chuàng)建我們的 包名(GroupId) 工程名(Artifactid) Version (版本號(hào))
Next
三.Gradle 的配置和 相關(guān)項(xiàng)勾選(建議CheckBox 全勾選)
大家注意到,我上面圖片前三個(gè) CheckBox 我都勾選了
第一個(gè)Use auto-import因?yàn)檫@里我遇到了一個(gè)坑. 不勾選的話拼苍,Gradle 里面的包資源不加載笑诅。所以勾選一下本身也不會(huì)導(dǎo)致其他問題。
第二個(gè) Create directories for empty content roots automatically 勾選會(huì)幫你構(gòu)建相應(yīng)的web 目錄
下面的Gradle 配置如果本地有疮鲫,就用自己的吆你,沒有就默認(rèn)就好了。
然后 Next
四.配置工程的目錄和工程名字俊犯。
然后Finish 如下圖工程目錄妇多,其實(shí)到這里,我們的SpringMvc工程搭建才剛開始燕侠,之前都是簡單的配置砌梆。
重點(diǎn)介紹一下目錄的層級作用默责。
. src -> main -> java 這個(gè)層級下面定義包和Java類。
. src -> main -> resources 配置文件都可以放在這個(gè)下面 例如Spring 咸包,Mybatis的等直接用classpath:/xxxxxConfig.xml方式調(diào)用
.src -> main -> webapp 主要我們web 的配置和資源(Page桃序,js等)都在這個(gè)目錄下
但是這里發(fā)現(xiàn)在webapp 下面 沒有 WEB-INF文件夾和 web.xml
這里我推薦先放一下,我們先來配置 Tomcat 烂瘫,主要是因?yàn)槲沂紫扰渲脀eb.xml 的時(shí)候我遇到一個(gè)坑媒熊。坑的原因是我配置完以后接著配置Tomcat的時(shí)候運(yùn)行出錯(cuò)坟比,后來找出原因是芦鳍,Tomcat 給我再web.xml/下面又創(chuàng)建了 web.xml很神奇。所以建議先來配置Tomcat葛账。
步驟如下:
1.菜單欄 Run -> Edit Configurations 我們選擇 + 柠衅,然后選擇 Tomcat Server,如果是本地Tomcat 選擇 Local
2.部署我們的項(xiàng)目
3.然后我們就可以看到可運(yùn)行的界面了
開始搭建 Spring MVC籍琳。
前面說到我們先配置完Tomcat 再配置 web.xml 菲宴。因?yàn)橐粋€(gè)web 項(xiàng)目,這必須要先存在∏骷保現(xiàn)在開始如下步驟
還是 File -> Project Structure -> 選擇左邊菜單Modules 具體見圖解喝峦。
基于Gradle 配置Spring 相關(guān)依賴。
group 'com.za.patch'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
repositories {
mavenCentral()
}
def springVersion = "4.3.9.RELEASE"
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'jstl:jstl:1.2'
compile("mysql:mysql-connector-java:6.0.6")
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-orm:$springVersion"
compile "org.springframework:spring-jdbc:$springVersion"
compile "org.springframework:spring-instrument- tomcat:$springVersion"
compile "org.springframework:spring-instrument:$springVersion"
compile "org.springframework:spring-framework-bom:$springVersion"
compile "org.springframework:spring-expression:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-context-support:$springVersion"
compile "org.springframework:spring-context:$springVersion"
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-aspects:$springVersion"
compile "org.springframework:spring-test:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework:spring-webmvc-portlet:$springVersion"
}
接著我們開始創(chuàng)建Spring 配置文件呜达,我們將文件創(chuàng)建在 src/main/resources 命名為:mvc-servlet.xml
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.xx.xxx.controller"/>
<!-- 靜態(tài)資源(js谣蠢、image等)的訪問 -->
<mvc:default-servlet-handler/>
<!-- 開啟注解 -->
<mvc:annotation-driven/>
<!--ViewResolver 視圖解析器-->
<!--用于支持Servlet、JSP視圖解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
再回到web.xml 我們這里配置相關(guān)的請求控制 和監(jiān)聽器和 加載Spring 的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
最后我們寫一個(gè) Controller 類 測試一下查近。
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by stan_zy on 17/6/22.
*/
@Controller
@RequestMapping(value = "/patch" , method = RequestMethod.GET)
public class PatchController {
@RequestMapping(value = "/downLoadPatch" , method = RequestMethod.GET)
public String downLoadPatch(ModelMap modelMap){
modelMap.addAttribute("msg","Spring Mvc");
return "index";
}
}
這里說下三個(gè)地方眉踱。
*RequestMapping(value = "/patch" 標(biāo)識(shí)為請求Controller的 Name。
*@RequestMapping(value = "/downLoadPatch" 標(biāo)識(shí)為請求的方法
*return "index";返回一個(gè)頁面
頁面代碼:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${msg}</title>
</head>
<body>
<h1>${msg}</h1>
</body>
</html>
測試地址:http://localhost:8888/patch/downLoadPatch
結(jié)束霜威。??勋锤。