1.编写web.xml注册DispatcherServlet
<?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" >
< servlet>
< servlet-name> springmvc</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< 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>
<?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" >
< context: component-scan base-package = " com.yf.controller" />
< mvc: default-servlet-handler/>
< 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>
@Controller
public class HelloController {
@RequestMapping ( "/hello" )
public String hello ( Model model) {
model. addAttribute ( "msg" , "helloAnnotation" ) ;
return "hello" ;
}
}
< % --
Created by IntelliJ IDEA .
User : Lenovo
Date : 2022 / 3 / 31
Time : 15 : 53
To change this template use File | Settings | File Templates.
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title< / title>
< / head>
< body>
${ msg}
< / body>
< / html>