1、Spring框架css
源碼地址:https://github.com/spring-projects/spring-frameworkhtml
構建工具:Gradle,Gradle教程:https://www.w3cschool.cn/gradle/java
Gradle基於Groovy語言,Groovy教程:https://www.w3cschool.cn/groovy/git
JSR標準相關的資料: https://jcp.org/en/jsr/allgithub
2、Spring框架Moduleweb
官網文檔:https://docs.spring.io/spring/docs/4.3.17.RELEASE/spring-framework-reference/htmlsingle/#overview-modulesspring
3、使用Maven構建demo-springmvc項目spring-mvc
File -> New Project -> Maven(勾選Create from archetype,同時選擇maven-archetype-webapp) -> Next 服務器
GroupId:com.examplemvc
ArtifactId:demo-spring
-> Next -> Next ->
Project name:demo-spring
-> Finish
對比上面兩圖,發現不一樣點只在於有沒有web結構,並且默認的web結構仍是須要修改的。 繼續...
添加spring mvc框架支持,直接在pom.xml文件中dependencies下添加spring mvc的依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.17.RELEASE</version> </dependency>
替換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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
WEB-INF下添加applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
dispatcher-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"> <!--spring基本註解支持 --> <context:annotation-config/> <!--mvc註解支持--> <mvc:annotation-driven/> <!--靜態資源映射--> <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/> <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/> <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/> <!--jsp模板視圖支持--> <bean id="defaultViewResolver" 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"/> <property name="exposeContextBeansAsAttributes" value="true"/> </bean> <!--自動掃描裝配--> <context:component-scan base-package="com.example.demo"/> </beans>
寫個Controller跑起來看看
在 src -> main 下建兩個文件夾,java和test,建立完成後,分別設置資源類型
java文件夾設置爲 sources,test文件夾設置爲 tests
建立一個測試controller
配置測試服務器
點擊啓動,訪問 localhost:8080/user/get
會顯示 ??
緣由:spring mvc默認輸出字符集 iso-8859-1,須要將輸出字符集調整爲 utf-8
再次啓動,訪問 localhost:8080/user/get,顯示正常。