搭建和使用Spring MVC框架的方法如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- 配置Spring MVC -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
@Controller
@RequestMapping("/example")
public class ExampleController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello Spring MVC!");
return "hello";
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
以上是使用Spring MVC框架搭建和使用的基本方法。根据具体需求,还可以使用其他功能和特性,如数据绑定、表单验证、拦截器等。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:springmvc框架搭建的流程是什么