SpringMVC
m:model:模型,javabean
v:view:视图,html/jsp
c:controller:控制器:servlet
- 概述
(1) springMVC 简介
springMVC是spring framework的后续衍生产生,纯正的基于Servlet技术,基于MVC,基于注解开发的web框架技术。
(2) 特点:
a) 基于MVC的view层框架,主要作为请求处理和响应
b) 松耦合易拓展的技术框架
c) 可以应用RESTFul开发风格
d) 使用普通类处理请求(基于servlet环境)
- 使用
1)引入依赖
<!--springmvc依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2)web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3)controller
@Controller
public class LoginController {
@RequestMapping("lg")
public ModelAndView login(String uname,String upwd){
//方法的参数用于接收数据
System.out.println("uname:" + uname);
System.out.println("upwd:" + upwd);
System.out.println("hello springmvc");
//响应
ModelAndView mv = new ModelAndView();
mv.addObject("uname",uname);
mv.setViewName("success.jsp");
return mv;
}
}
4)jsp页面
<form action="lg">
<input type="text" name="uname"/><input type="password" name="upwd"/>
<input type="submit" value="登录">
</form>
5)spring核心配置文件
<context:component-scan base-package="com.qf.controller"></context:component-scan>
3.运行原理