SpringMVC使用註解開發
- 新建一個Web項目
- 導入相關的jar包
- 編寫web.xml,註冊DispatcherSerlet
- 編寫Springmvc配置文件
- 創建對應的控制類,controller
- 完善前端視圖和contriller之間的對應
- 測試
必須配置:處理映射器、處理適配器、視圖解析器,我們只需要配置解析器
編寫web.xml,註冊DispatcherSerlet
<?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.註冊DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--關聯一個springmvc的配置文件:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--啓動級別-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的請求;(不包括.jsp)-->
<!--/* 匹配所有的請求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
編寫Springmvc配置文件
<?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="cn.ar99guoke.controller"/>
<!-- 讓Spring MVC不處理靜態資源 .css .js .html .mp3 .mp-->
<mvc:default-servlet-handler/>
<!-- 自動開啓DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter-->
<mvc:annotation-driven/>
<!--視圖解析器:DispatcherServlet給他的ModelAndView 並且自動注入-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前綴-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--後綴-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
創建對應的控制類,controller
package cn.ar99guoke.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Controller爲了讓IOC容器掃描到
* @RequestMapping是爲了映射請求路徑,這裏因爲類與方法上都有映射所以訪問時應該是/HelloConttroller/hello
* Model類型是爲了把Action中的數據帶到視圖中;
* 方法返回結果是視圖的名稱hello,加上配置文件中的前後綴變成web-inf/jsp/hello.jsp
*/
@Controller
@RequestMapping("/HelloController")
public class HelloController {
//真實訪問地址:項目名/HelloController/hello
@RequestMapping("/hello")
public String hello(Model model){
//封裝數據 向模型中添加屬性值,可以在jsp頁面中取出並渲染
model.addAttribute("msg","HellpController");
//web-inf/jsp/hello.jsp
return "hello";
}
}
對應的jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>