SpringMVC篇(四)使用註解開發springmv
技術標籤:SpringMVC篇springjavaweb.xml
使用註解開發springmv
1、新建一個Moudle,springmvc-03-hello-annotation 。新增web支援!
2、配置pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
< include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory >
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources >
</build>
3、配置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_4_0.xsd"
version="4.0">
<!--1.註冊servlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通過初始化引數指定SpringMVC配置檔案的位置,進行關聯-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 啟動順序,數字越小,啟動越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有請求都會被springmvc攔截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
/ 和 /* 的區別:< url-pattern > / </ url-pattern > 不會匹配到.jsp, 只針對我們編寫的請求;即:.jsp 不會進入spring的 DispatcherServlet類 。< url-pattern > /* </ url-pattern > 會匹配 *.jsp,會出現返回 jsp檢視 時再次進入spring的DispatcherServlet 類,導致找不到對應的controller所以報404錯。
注意web.xml版本問題,要最新版!
註冊DispatcherServlet
關聯SpringMVC的配置檔案
啟動級別為1
對映路徑為 / 【不要用/*,會404】
4、新增Spring MVC配置檔案
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自動掃描包,讓指定包下的註解生效,由IOC容器統一管理 -->
<context:component-scan base-package="com.ni.controller"/>
<!-- 讓Spring MVC不處理靜態資源 -->
<mvc:default-servlet-handler />
<!--
支援mvc註解驅動
在spring中一般採用@RequestMapping註解來完成對映關係
要想使@RequestMapping註解生效
必須向上下文中註冊DefaultAnnotationHandlerMapping
和一個AnnotationMethodHandlerAdapter例項
這兩個例項分別在類級別和方法級別處理。
而annotation-driven配置幫助我們自動完成上述兩個例項的注入。
-->
<mvc:annotation-driven />
<!-- 檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 字首 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 字尾 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
5、建立Controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/h1")
public String hello(Model model){
model.addAttribute("msg","Hello,SpringMvcAnnotation");
return "helloSpringmvc";
}
}
@Controller是為了讓Spring IOC容器初始化時自動掃描到;
@RequestMapping是為了對映請求路徑,這裡因為類與方法上都有對映所以訪問時應該是/HelloController/hello;
方法中宣告Model型別的引數是為了把Action中的資料帶到檢視中;
方法返回的結果是檢視的名稱hello,加上配置檔案中的前後綴變成WEB-INF/jsp/hello.jsp。
6、建立檢視層
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC</title>
</head>
<body>
${msg}
</body>
</html>
7、配置Tomcat執行
小結
實現步驟其實非常的簡單:
1.新建一個web專案
2.匯入相關jar包
3.編寫web.xml , 註冊DispatcherServlet
4.編寫springmvc配置檔案
5.接下來就是去建立對應的控制類 , controller
6.最後完善前端檢視和controller之間的對應
7.測試執行除錯.
使用springMVC必須配置的三大件:
處理器對映器、處理器介面卡、檢視解析器
通常,我們只需要手動配置檢視解析器,而處理器對映器和處理器介面卡只需要開啟註解驅動即可,而省去了大段的xml配置