spring簡單介紹:https://www.cnblogs.com/package-java/p/10368672.html
1、創建一個Maven Project項目
點擊下一步
點擊下一步
2、創建好項目之后可以看到pom.xml報錯,接下來添加Tomcat運行環境,然后,生成`web.xml`文件
3、添加依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.tedu.spring</groupId> <artifactId>speing_liu</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- springmvc的依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- 測試的依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
4、在web.xml文件中進行配置
由於希望SpringMVC框架能接收到相關的請求,所以,首先應該對`DispatcherServlet`進行配置:
關於DispatcherServlet的包名,可以在任意Java類中聲明DispatcherServlet變量,由Eclipse完成導包,則在import語句中就有了該類的包名。
以上配置中,`<url-pattern>`中配置的是`*.do`,表示SpringMVC框架將接收所有以`.do`作為資源名后綴的請求,而並不處理例如`.html`、`.jpg`等請求,如果希望接收並處理所有請求,可以使用`/*`或其它使用了通配符的配置。
為了使得Spring的運行環境是正常的,應該保證在項目部署到Tomcat的第一時間就加載Spring的配置,要實現這樣的效果,可以使得:`DispatcherServlet`是默認啟動的(配置`<load-on-startup>`節點),並且,它啟動時加載Spring的配置文件(`DispatcherServlet`的父類`FrameworkServlet`中有`contextConfigLocation`屬性,表示初始化時加載哪個配置文件)!
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>SPRINGMVC-01-HELLO</display-name> <servlet>
<!-- 配置SpringMVC DispatcherServlet(前端控制器) --> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<!-- 指定Spring MVC啟動所需加載的配置文件 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
至此,項目的運行效果為:啟動時,會直接初始化`DispatcherServlet`,並且在初始化時,還會自動加載`spring.xml`配置文件。
如果需要檢查配置是否成功,可以:使得某個類被Spring管理,且在構造方法中添加輸出語句!如果啟動項目時可以看到輸出語句,則成功!
5、src/min/resources下創建spring.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 組件掃描 --> <!-- base-package:根包 --> <context:component-scan base-package="cn.tedu.spring.controller" />
</beans>
6、接收並處理請求
在實際開發中,並不會使用專門的配置文件去記錄請求路徑與處理請求的控制器的映射關系,即:不配置`HandlerMapping`(當然,這並不影響SpringMVC的工作流程,只是從代碼上不這樣體現而已)。
可以直接創建控制器類,例如`cn.tedu.spring.HelloController`,需要該類在組件掃描范圍之內,且應該添加`@Controller`注解,然后,在類中自定義處理請求的方法。
關於處理請求的方法,訪問權限應該是`public`,返回值類型暫時使用`String`,方法的名稱可以自行定義,參數暫時留空,即不添加任何參數,然后,在方法之前添加`@RequestMapping("路徑")`配置請求路徑:
@Controller public class HelloController { // 處理請求的方法 // 權限:public // 返回值:String(暫定) // 方法名:自定義 // 參數列表:無(暫定) @RequestMapping("hello.do") public String showHello() { System.out.println("HelloController.showHello()"); return null; } }
至此,`hello.do`的請求已經可以被以上自定義的`showHello()`方法處理,在瀏覽器輸出`http://localhost:8080/SPRINGMVC-01-HELLO/hello.do`后,雖然在瀏覽器中無法正確的顯示內容,但是,在Eclipse控制台可以觀察到`showHello()`方法已經被正確的調用。
7、在spring.xml中配置視圖解析器
處理完請求后,方法返回的`String`類型數據表示的就是視圖名,例如返回`"helloworld"`,接下來,框架會根據視圖名找到視圖解析器,從而確定最終的視圖組件,典型的視圖解析器是`InternalResourceViewResolver`:
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 組件掃描 --> <!-- base-package:根包 --> <context:component-scan base-package="cn.tedu.spring.controller" /> <!-- ViewResolver:視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/" /> <!-- 后綴 --> <property name="suffix" value=".jsp" /> </bean> </beans>
`InternalResourceViewResolver`的工作模式是:將**前綴 + 控制器方法返回的視圖名 + 后綴**拼接起來,以項目的`webapp`為根目標,找到對應的頁面文件
結合此前方法返回的字符串值為`"helloworld"`,則視圖解析器會找到`webapp/WEB-INF/helloworld.jsp`文件作為最終顯示的視圖組件!所以,創建對應的jsp文件,然后重新部署項目運行,即可看到最終運行效果。
將第6步的返回值改成helloworld:
@Controller public class HelloController { // 處理請求的方法 // 權限:public // 返回值:String(暫定) // 方法名:自定義 // 參數列表:無(暫定) @RequestMapping("hello.do") public String showHello() { System.out.println("HelloController.showHello()"); return "helloworld"; } }
8、創建jsp文件
添加Tomcat運行環境,不然jsp會報錯,參考:https://blog.csdn.net/testcs_dn/article/details/36455669
也可以看看這個:https://www.cnblogs.com/yadongliang/p/6484779.html
相關文獻:https://elf8848.iteye.com/blog/875830