SpringMVC 概述

  Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一
  Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架
  Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口。
  支持 REST 风格 URL 请求
  采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性

HelloWorld

步骤:
  –加入 jar 包
  –在 web.xml 中配置 DispatcherServlet

		<!-- 
			实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
			默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
		-->

  –加入 Spring MVC 的配置文件

	<!-- 配置自定扫描的包 -->
	<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

	<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

  –编写处理请求的处理器,并标识为处理器

 1 @Controller
 2 public class HelloWorld {
 3     /**
 4      * 1. 使用 @RequestMapping 注解来映射请求的 URL
 5      * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
 6      * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
 7      * /WEB-INF/views/success.jsp
 8      */
 9     @RequestMapping("/helloworld")
10     public String hello(){
11         System.out.println("hello world");
12         return "success";
13     }
14 }
helloworld.java

相关文章:

  • 2021-05-29
  • 2022-12-23
  • 2021-06-21
  • 2021-08-29
  • 2022-12-23
  • 2021-11-21
  • 2021-12-16
  • 2021-05-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2021-05-09
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案