當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC学习03之使用注解开发SpringMVC
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC学习03之使用注解开发SpringMVC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
復習
Spring MVC的特點:
一、使用注解開發
1.創建普通Maven項目后,添加web框架
2.配置web.xml配置文件,改為最新的4.0版本
<?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"> </web-app>3.在web.xml配置注冊DispatcherServlet
<!--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>4.配置springmvc-servlet.xml配置文件,名稱: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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 --><context:component-scan base-package="com.kuang.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
package com.shan.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@Controller //會被Spring托管 @RequestMapping("/HelloController") public class HelloController {//真實訪問地址 : 項目名/HelloController/hello@RequestMapping("/hello")public String sayHello(Model model){//向模型中添加屬性msg與值,可以在JSP頁面中取出并渲染model.addAttribute("msg","hello,SpringMVCAnnotation");//WEB-INF/jsp/hello.jspreturn "hello";}}@Controller是讓Spring IOC容器初始化時自動掃描到;
@RequestMapping是映射請求路徑
方法中聲明Model類型的參數是為了把數據帶到視圖中;
方法返回的結果是視圖的名稱hello,加上配置文件中的前后綴變成WEB-INF/jsp/hello.jsp。
6.創建視圖層
為了取出msg這個數據的信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>springmvc</title> </head> <body> ${msg} </body> </html>7.配置Tomcat運行
記得將依賴包導入到tomcat中,否則會出現找不到依賴包的錯誤!!!
二、小結
實現步驟:
作者有話說
博客創作不易,希望看到這里的讀者動動你的小手點個贊,如果喜歡的小伙伴可以一鍵三連,作者大大在這里給大家謝謝了。
總結
以上是生活随笔為你收集整理的SpringMVC学习03之使用注解开发SpringMVC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML学习04之内联框架和表单
- 下一篇: gradle idea java ssm