欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC注解的入門實(shí)例詳解

 更新時(shí)間:2022年01月19日 17:11:19   作者:YSOcean  
這篇文章主要為大家介紹了SpringMVC注解的入門實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1、在 web.xml 文件中配置前端處理器

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_01</display-name>
  <!-- 配置前端控制器DispatcherServlet -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--springmvc.xml 是自己創(chuàng)建的SpringMVC全局配置文件,用contextConfigLocation作為參數(shù)名來加載
        如果不配置 contextConfigLocation,那么默認(rèn)加載的是/WEB-INF/servlet名稱-servlet.xml,在這里也就是 springmvc-servlet.xml
      -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--第一種配置:*.do,還可以寫*.action等等,表示以.do結(jié)尾的或者以.action結(jié)尾的URL都由前端控制器DispatcherServlet來解析
        第二種配置:/,所有訪問的 URL 都由DispatcherServlet來解析,但是這里最好配置靜態(tài)文件不由DispatcherServlet來解析
        錯(cuò)誤配置:/*,注意這里是不能這樣配置的,應(yīng)為如果這樣寫,最后轉(zhuǎn)發(fā)到 jsp 頁面的時(shí)候,仍然會(huì)由DispatcherServlet進(jìn)行解析,
                    而這時(shí)候會(huì)找不到對(duì)應(yīng)的Handler,從而報(bào)錯(cuò)?。?!
      -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

2、在 springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--注解處理器映射器  -->   
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    <!--注解處理器適配器  -->   
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>  
    <!--使用mvc:annotation-driven可以代替上面的映射器和適配器
        這里面會(huì)默認(rèn)加載很多參數(shù)綁定方法,比如json轉(zhuǎn)換解析器就默認(rèn)加載,所以優(yōu)先使用下面的配置
      -->
    <!-- <mvc:annotation-driven></mvc:annotation-driven> -->
    <!--單個(gè)配置Handler  -->
    <!-- <bean class="com.ys.controller.HelloController"></bean> -->
    <!--批量配置Handler,指定掃描的包全稱  -->
    <context:component-scan base-package="com.ys.controller"></context:component-scan>
    <!--配置視圖解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 返回視圖頁面的前綴 -->
        <property name="prefix" value="/WEB-INF/view/"></property>
        <!-- 返回頁面的后綴 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

3、編寫 Handler

package com.ys.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//使用@Controller注解表示這個(gè)類是一個(gè)Handler
@Controller
public class HelloController {
    //@RequestMapping注解括號(hào)里面的表示訪問的URL
    @RequestMapping("hello")
    public ModelAndView hello(){
        ModelAndView modelView = new ModelAndView();
        //類似于 request.setAttribute()
        modelView.addObject("name","張三");
        //配置返回的視圖名,由于我們?cè)趕pringmvc.xml中配置了前綴和后綴,這里直接寫視圖名就好
        modelView.setViewName("index");
        //modelView.setViewName("/WEB-INF/view/index.jsp");
        return modelView;
    }
}

注意@Controller注解和@RequestMapping注解的用法

4、編寫 視圖 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello:${name}
</body>
</html>

5、在瀏覽器中輸入:http://localhost:8080/SpringMVC_03/hello

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • java 異常被catch后 將會(huì)繼續(xù)執(zhí)行的操作

    java 異常被catch后 將會(huì)繼續(xù)執(zhí)行的操作

    這篇文章主要介紹了java 異常被catch后 將會(huì)繼續(xù)執(zhí)行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 一文了解Java中record和lombok的使用對(duì)比

    一文了解Java中record和lombok的使用對(duì)比

    Java的 record 關(guān)鍵字是Java 14中引入的一個(gè)新的語義特性。Lombok 是一個(gè)Java庫,可以自動(dòng)生成一些已知的模式為Java字節(jié)碼。本文我們將探討各種使用情況,包括java record 的一些限制。對(duì)于每個(gè)例子,我們將看到Lombok如何派上用場,并比較這兩種解決方案
    2022-07-07
  • Java用棧實(shí)現(xiàn)綜合計(jì)算器

    Java用棧實(shí)現(xiàn)綜合計(jì)算器

    棧(stack)又名堆棧,它是一種運(yùn)算受限的線性表,下面看一下如何在Java中,利用數(shù)組實(shí)現(xiàn)模擬一個(gè)棧,感興趣的朋友跟隨小編一起看看吧
    2022-06-06
  • java serialVersionUID解決序列化類版本不一致問題面試精講

    java serialVersionUID解決序列化類版本不一致問題面試精講

    這篇文章主要為大家介紹了serialVersionUID解決序列化類版本不一致問題的面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • SpringBoot整合RabbitMQ實(shí)現(xiàn)延遲隊(duì)列的示例詳解

    SpringBoot整合RabbitMQ實(shí)現(xiàn)延遲隊(duì)列的示例詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合RabbitMQ實(shí)現(xiàn)延遲隊(duì)列,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下
    2023-04-04
  • maven項(xiàng)目在svn中的上傳與檢出的方法

    maven項(xiàng)目在svn中的上傳與檢出的方法

    企業(yè)開發(fā)中經(jīng)常使用svn來為我們控制代碼版本,也經(jīng)常使用maven來管理項(xiàng)目。下面將介紹一下如何將maven項(xiàng)目上傳到svn中,如何將項(xiàng)目從svn中檢出,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Spring實(shí)現(xiàn)對(duì)象注入的三種方法詳解

    Spring實(shí)現(xiàn)對(duì)象注入的三種方法詳解

    這篇文章主要為大家學(xué)習(xí)介紹了Spring中實(shí)現(xiàn)對(duì)象注入的三種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下
    2023-07-07
  • idea啟動(dòng)項(xiàng)目提示端口占用的問題解決

    idea啟動(dòng)項(xiàng)目提示端口占用的問題解決

    有時(shí)候當(dāng)我們使用Tomcat啟動(dòng)web項(xiàng)目時(shí),會(huì)提示端口占用,導(dǎo)致啟動(dòng)失敗,本文就來介紹一下端口占用的解決方法,具有一定的參考價(jià)值,感興趣的可以了解下
    2023-08-08
  • SpringBoot整合Liquibase的示例代碼

    SpringBoot整合Liquibase的示例代碼

    本篇文章給大家介紹SpringBoot整合Liquibase的兩種情況分析,看似整合問題很簡單,但是很容易出錯(cuò),下面小編給大家介紹下整合步驟,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Spring中BeanFactoryPostProcessors是如何執(zhí)行的

    Spring中BeanFactoryPostProcessors是如何執(zhí)行的

    BeanFactoryPostProcessor是Spring容器提供的一個(gè)擴(kuò)展機(jī)制,它允許開發(fā)者在Bean的實(shí)例化和初始化之前對(duì)BeanDefinition進(jìn)行修改和處理,這篇文章主要介紹了你知道Spring中BeanFactoryPostProcessors是如何執(zhí)行的嗎,需要的朋友可以參考下
    2023-11-11

最新評(píng)論