最近在看spring實戰,準備先搭建一個hello world出來!。網上的教程或多或少不靠譜,下面是個人步驟。html
操做系統: win10
ide版本: 2017.2java
本文原始的版本是在我的學習寫的,因此不少東西寫的不夠詳細。
本次從新概括總結,下面開始具體操做:web
建立一個empty project
File -> new -> project -> empty projectspring
說明一下,在實際開發中咱們喜歡一個項目一個project,可是學習之中,爲了方便來回切換,因此咱們每個demo都寫成module的形式。tomcat
建立 module(在此以前須要建立entity project)
在新版本中,會提示你是下載勾選的mvc的依賴。mvc
而後填寫module名 點擊確認 module建立完畢app
在module的web-WEB-INF下新建 classes和lib文件夾(新版本中的lib在根目錄下) eclipse
IDEA默認是不會像eclipse那樣在web-inf下生成lib文件夾和classes文件夾的的,須要咱們手動的建立,並將咱們根目錄下的lib目錄下的內容複製到web-inf/lib文件夾內。
jsp
回到主界面,配置tomcat 點擊run->edit configurations‘
點綠色的十字圖標 建立tomcat並配置 ide
選擇本身的tomcat位置
點擊deployment -> 綠色+號 -> 選擇咱們的項目
(小插曲)若是沒有artifact的話請百度,若是artifact在運行中報錯的話見下
打開file->projectstructure->artifact,將available element加入到左邊
web,xml
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
修改dispatcjer-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" 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"> <!--對應src下的報名,須要本身新建--> <context:component-scan base-package="com" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <!-- 這個配置是配置JSP頁面的位置,按照你本身的配置來配 --> <!--jsp文件夾須要本身手動建立--> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
3.在jsp目錄下新建hello.jsp
<%-- Created by IntelliJ IDEA. User: Ying Date: 2018/2/28 Time: 16:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hello mvc</title> </head> <body> ${msg} </body> </html>
4.在src中編寫controller類
須要和dispatcher-servlet.xml配置的路徑一致
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("msg","Spring 3 MVC hello world"); return "hello"; } }
5.點擊右上角的綠色三角形進行運行,最後進行測試
第一個小demo到此結束!