日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

三、入门实例----基于注解

發布時間:2025/3/21 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 三、入门实例----基于注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目結構為:

  

?

回到頂部

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?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 是自己創建的SpringMVC全局配置文件,用contextConfigLocation作為參數名來加載 ????????如果不配置 contextConfigLocation,那么默認加載的是/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結尾的或者以.action結尾的URL都由前端控制器DispatcherServlet來解析 ????????第二種配置:/,所有訪問的 URL 都由DispatcherServlet來解析,但是這里最好配置靜態文件不由DispatcherServlet來解析 ????????錯誤配置:/*,注意這里是不能這樣配置的,應為如果這樣寫,最后轉發到 jsp 頁面的時候,仍然會由DispatcherServlet進行解析, ????????????????????而這時候會找不到對應的Handler,從而報錯!!! ??????--> ????<url-pattern>/</url-pattern> ??</servlet-mapping> </web-app>

  

?

回到頂部

2、在 springmvc.xml 文件中配置處理器映射器,處理器適配器,視圖解析器

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 <?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可以代替上面的映射器和適配器 ????????這里面會默認加載很多參數綁定方法,比如json轉換解析器就默認加載,所以優先使用下面的配置 ??????--> ????<!-- <mvc:annotation-driven></mvc:annotation-driven> --> ????<!--單個配置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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.ys.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; //使用@Controller注解表示這個類是一個Handler @Controller public class HelloController { ????//@RequestMapping注解括號里面的表示訪問的URL ????@RequestMapping("hello") ????public ModelAndView hello(){ ????????ModelAndView modelView = new ModelAndView(); ????????//類似于 request.setAttribute() ????????modelView.addObject("name","張三"); ????????//配置返回的視圖名,由于我們在springmvc.xml中配置了前綴和后綴,這里直接寫視圖名就好 ????????modelView.setViewName("index"); ????????//modelView.setViewName("/WEB-INF/view/index.jsp"); ????????return modelView; ????} }

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

?

回到頂部

4、編寫 視圖 index.jsp

1 2 3 4 5 6 7 8 9 10 11 12 <%@ 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

?

轉載于:https://www.cnblogs.com/zhoanghua/p/9292237.html

總結

以上是生活随笔為你收集整理的三、入门实例----基于注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。