1. 程式人生 > 實用技巧 >使用註解開發SpringMVC

使用註解開發SpringMVC

使用註解開發SpringMVC

1、在maven中匯入spring-webmvc的依賴

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

2、配置web.xml,註冊DispatcherServlet

<web-app>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 關聯一個springmvc的配置檔案:springMVC-context.xml -->
        <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>

    <!-- / 匹配所有請求:(不包括.jsp) -->
    <!-- /* 匹配所有請求:(包括.jsp) -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3、配置springMVC-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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--自動掃描包,讓指定包下的註解生效, 由IOC容器統一管理 -->
    <context:component-scan base-package="com.qilu.controller"/>
    <!-- 讓Spring MVC不處理靜態資源-->
    <mvc:default-servlet-handler />

    <!--支援mvc註解驅動
		在spring中一般 @RequestMapping註解來完成對映關係
		要想使@RequestMapping註解生效
		必須向上下文中註冊DefaultAnnotationHandlerMapping
		和一個AnnotationMethodHandlerAdapter例項
		這兩個例項分別在類級別和方法級別處理。
		而 annotation-driven 配置幫助我們自動完成上述著兩個例項的注入。
	-->
    <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>

4、編寫業務類HelloController,並用註解自動裝配和完成請求路徑的配置

@Controller
public class HelloController {
	@RequestMapping("/hello")
	public String hello(Model model){
           //封裝資料
           model.addAttribute("msg", "Hello, SpringMVCAnnotation!");
           return "hello"; //會被檢視解析器處理:
	}
}

5、編寫hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Title</title> 
</head>
<body>
${msg}
</body>
</html>