1. 程式人生 > 程式設計 >使用idea搭建一個spring mvc專案的圖文教程

使用idea搭建一個spring mvc專案的圖文教程

最近在學習spring的相關知識,順便記下筆記,與大家共享。

spring執行流程如下圖所示:

使用idea搭建一個spring mvc專案的圖文教程

【流程】

user傳送request,DispatcherServlet(等同於Controller控制器),控制器接收到請求,來到HandlerMapping(在配置檔案中配置),HandlerMapping會對URL進行解析,並判斷當前URL該交給哪個controller來處理,找到對應的controller之後,controller就跟server、Javabean進行互動,得到某一個值,並返回一個檢視(ModelAndView過程),Dispathcher通過ViewResolver檢視解析器,找到ModelAndView物件指定的檢視物件,最後,檢視物件負責渲染返回給客戶端。

下面將通過一個具體的例項來講述操作流程。

【開發環境】

win7

jdk1.8.0_144

Tomcat8.5.16

IntelliJ IDEA 2017.2

【具體操作如下】

1.建立spring mvc工程,設定project name。這裡取名字為springmvc_hello。

使用idea搭建一個spring mvc專案的圖文教程

2.建立完成後,結構如下圖示。

使用idea搭建一個spring mvc專案的圖文教程

3.現在開始修改基本的配置檔案,其中web.xml是用來啟動DispatcherServlet的。新建之後的web.xml檔案是有初始設定的,我們只需根據自己的需要稍加修改就好。系統自帶的配置如下:

使用idea搭建一個spring mvc專案的圖文教程

這裡只需要修改一個地方,將url-pattern地方修改成“/”。

3.現在開始修改dispatcher-servlet.xml檔案,這個檔案主要用來配置HandlerMapping。修改完成後,如下所示:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<beanname="/welcome.html"class="com.test.controller.WelcomeController"></bean>
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
</beans>

其中,class="com.test.controller.WelcomeController"表示的是src下的路徑。

這段程式碼 <property name="prefix" value="/WEB-INF/jsp/"/>,其中value="",也表示路徑。

下面開始建立包com.test.controller,在包裡面新建class,名字為:WelcomeController.java。在WEB-INF下建立資料夾jsp,在jsp中建立檔案welcome.jsp檔案。這個檔案的名稱需要與controller中,WelcomeController類的方法中的返回值return new ModelAndView(“welcome”)一致。

建立完成後結構如下所示:

使用idea搭建一個spring mvc專案的圖文教程

4.在類WelcomeController中,書寫如下程式碼:

package com.test.controller; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.Controller; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
/** 
 * Created by: zs 
 * Date-Time: 2017/8/9 19:05 
 */ 
public class WelcomeController implements Controller { 
 
  @Override 
  public ModelAndView handleRequest(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception { 
    //System.out.println("welcome"); 
    return new ModelAndView("welcome"); 
  } 
} 

5.另外,在welcome.jsp檔案中的程式碼為:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
  <title>Title</title> 
</head> 
<body> 
hello!!! 
</body> 
</html> 

6.配置Tomcat,選擇edit configurations.

使用idea搭建一個spring mvc專案的圖文教程

新增選項

使用idea搭建一個spring mvc專案的圖文教程

點選“local”會看到如下介面:

使用idea搭建一個spring mvc專案的圖文教程

選擇deployment,點選“+”

使用idea搭建一個spring mvc專案的圖文教程

使用idea搭建一個spring mvc專案的圖文教程

返回之後,點選OK按鈕。到此配置成功了。

7.現在RUN程式,開始訪問吧。

訪問地址為:http://localhost:8080/welcome.html

使用idea搭建一個spring mvc專案的圖文教程

總結

到此這篇關於使用idea直接建立一個spring mvc專案的圖文教程的文章就介紹到這了,更多相關idea建立spring mvc專案內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!