首先我們先使用配置文件的方式創建一個Spring項目。
大家在學習的時候不要圖一時方便直接使用idea依賴maven一鍵式創建,那樣其中的很多配置細節可能會注意不到,特別是對spring配置不熟悉的朋友,很不利於學習。而且希望在一次次創建spring的過程中希望每次都能提升對spring的認識。
好的言歸正傳,我這裏使用的intellij idea創建spring項目。intellij idea 是一個好工具,希望還在使用eclipse的朋友切換過來,一方面是這個軟件很強大,另一方面現在是微服務和分佈式的時代。
第一步:下載intellij idea
https://www.jetbrains.com/idea/
第二步:安裝
省略。。。
第三步:破解
百度吧。國人很強大!啥都有破解版的!
第四步:創建項目
新創建spring項目,這裏我們使用Springmvc當前默認的是4.3.18版本下面configure可以選擇其他版本,我用默認的就行了,直接next就好了。
這裏是項目名稱和存放位置,項目名稱就hello吧。然後finish等待創建。這期間他會自動下載spring依賴包,等下就好了。
創建完成後我們看下目錄,我這裏改了下項目名HelloDemo,規範點。
第五步:加入tomcat服務器
添加tomcat 在deployment中添加項目war文件
第六步:開始配置Spring相關文件
首先是web.xml,主要用來啓動框架分發請求等等,下面是idea自動生成的內容
<?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_4_0.xsd"
version="4.0">
<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>*.do</url-pattern>
</servlet-mapping>
</web-app>
然後配置applicationContext.xml
首先我們先創建一個包com.oldma.test,和一個TestController
然後配置Spring掃描等相關內容,命名空間一定別漏 否則會有各種錯誤
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<!-- SpringMVC只負責controller相關的Bean的註冊 -->
<context:component-scan base-package="com.oldma.test" />
<!-- 擴充了註解驅動,可以將請求參數綁定到控制器參數 -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
第七步:編寫代碼
好的 然後我們創建一個hello.jsp 和控制器的內容
TestController.java
package com.oldma.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class TestController {
@RequestMapping("/index.do")
public String getIndex(){
return "hello";
}
}
以及hello.jsp 內容隨便。
啓動tomcat,瀏覽器輸入 localhost:8080/index.do
成功!
注:在創建項目時啓動時可能碰到
1,org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
這個tomcat6的問題,用7以及以上就可以了
2,ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
是spring沒有引入 在project structure中fix就行了