轉載請註明出處:
http://blog.csdn.net/aa464971/article/details/78250259
Github地址:
https://github.com/xiandanin/SpringSample
創建新項目
- 創建一個新項目,
Maven
-Create from archetype
,選中maven-archetype-webapp
這個archetype
- 輸入GroupId和ArtifactId
- 輸入項目名稱並選擇項目存放的路徑
配置項目結構
- 進入項目結構配置界面,
File
-Project Structure
- 選中
Modules
; - 在
main
下創建java
文件夾; - 在
webapp
-WEB-INF
下創建views
文件夾
- 標記
java
文件夾爲Sources
- 標記
resources
文件夾爲Resources
- 最終的項目結構如圖
引入Spring
打開pom.xml,引入Spring
<!--jstl-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
如果提示是否需要開啓自動導入,選擇Enable Auto-Import
,否則更改了pom.xml不會自動更新
配置web.xml
<web-app>
<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>
配置Spring
在WEB-INF
下新建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">
<!--掃描包路徑-->
<context:component-scan base-package="com.example" />
<!-- 開啓註解 -->
<context:annotation-config />
<mvc:annotation-driven />
<!-- 靜態資源(js、image等)的訪問 -->
<mvc:default-servlet-handler />
<!--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/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
配置訪問路徑與視圖
- 創建一個類命名HomeController,並註解
@Controller
,這個類就會被當成Controller - 再加上訪問路徑的註解
@RequestMapping("/home")
- 在HomeController內創建一個hello方法,在方法上註解
@RequestMapping("/hello")
,這樣當訪問http://localhost:8080/spring-helloworld/home/hello
的時候,就會執行這個方法 - 最後
return "hello"
,這樣執行完hello方法後會跳轉到hello.jsp(hello.jsp需要建在WEB-INF
-views
下)
配置Tomcat
添加一個Tomcat服務
+
- Tomcat Server
- Local
切換到Deployment
,+
- Artifact...
添加當前項目的war,並設置項目訪問路徑爲spring-helloworld
運行Tomcat
運行完成後,在瀏覽器訪問http://localhost:8080/spring-helloworld/home/hello
,就進入到hello.jsp了
*關於使用JSP
如果需要在jsp使用el表達式
和taglib
,那還需要在pom.xml引入相應的庫
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
並且在jsp的第一行加入isELIgnored = false
,否則el表達式不會生效
<%@page isELIgnored="false" %>