2.基於註解使用springMVC
阿新 • • 發佈:2021-06-10
web.xml的配置
<?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"> <!--配置dispatchServlet:這個是mvc的核心:請求分發器,前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--繫結mvc的配置檔案--> <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> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring的配置檔案
<?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="com.wu.controller"/> <!--讓springmvc不處理靜態資源 css等--> <mvc:default-servlet-handler/> <!--支援mvc註解驅動 在spring中一般採用@RequestMapping 註解來完成對映關係 想使用這個註解必須註冊DefaultAnnotationHandlerMapping和一個AnnotationMethodHandlerAdapt例項 這兩個例項分別在類級別和方法級別處理 而annotation-driver配置幫助我們自動完成上述兩個例項的注入 --> <mvc:annotation-driven/> <!--處理器對映器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--處理器介面卡--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--處理器解析器--> <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 com.wu.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * @author wuyimin * @create 2021-06-10-12:11 * @description */ @Controller @RequestMapping("/hello")//第一級別的路徑 public class HelloController { @RequestMapping("/h1")//真實訪問地址 專案名/HelloController/hello/h1 public String hello(Model model){ //封裝資料 model.addAttribute("msg" ,"Hello,Annotation"); return "hello";//會被檢視解析器處理 //web-inf/jsp/hello.jsp } @RequestMapping("/h2")//真實訪問地址 專案名/HelloController/hello/h1 public String hello2(Model model){ //封裝資料 model.addAttribute("msg" ,"Hello2,Annotation"); return "hello";//會被檢視解析器處理 //web-inf/jsp/hello.jsp } }
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>