SpringMVC原理:
MVC模型:是一种架构的新模式,本身不引入新的功能,只是帮助我们将开发的结构组织的更加合理。使展示与模型分离,流程逻辑控制、业务逻辑调用与展示逻辑分离。
- model(模型):数据模型,包含要展示的数据和业务。
- View(视图):用户界面,在界面上展示模型数据。
- Controller(控制器):起调度作用,接收用户请求,调用业务处理请求,共享数据模型并跳转界面。
SpringMVC执行流程
SpringMVC入门程序
1、添加jar包
2、配置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_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--过滤器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
3、配置applicationContext.xml文件,添加视图解析器
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--自动扫描上下文包-->
<context:component-scan base-package="cn.su.*"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4、定义controller实现方法
@Controller
public class HelloController {
@RequestMapping("/hello.form")
public String hello(){
System.out.println("Hello");
return "index";
}
}
结果如下:
控制器和视图层的数据传递
一、视图层向控制器传递View to Controller
1、普通参数传递
@RequestMapping("/hello.form")
public String hello(String name){
System.out.println(name);
return "index";
}
2、@RequestParam注解
此时必须进行参数传递,否则报错
@RequestMapping("/hello.form")
public String hello(@RequestParam String name){
System.out.println(name);
return "index";
}
3、@RequestParam注解(可不进行参数传递)
此时定义了required = false,参数传递成为了非必须
@RequestMapping("/hello.form")
public String hello(@RequestParam(value = "name",required = false) String name){
System.out.println(name);
return "index";
}
4、method = RequestMethod.GET方法进行参数传递
@RequestMapping(value = "/hello.form",method = RequestMethod.GET,params = "name")
public String hello(String name){
System.out.println(name);
return "index";
}
二、控制器向视图层传递Controller to View
1、ModelAndView
@RequestMapping("/welcome.form")
public ModelAndView welcome(){
ModelAndView mv = new ModelAndView();
mv.addObject("name","苏"); // 添加模型数据
mv.setViewName("index"); // 设置视图
return mv;
}
2、Model
@RequestMapping("/welcome.form")
public String welcome(Model model){
model.addAttribute("username","小蚂蚁");
model.addAttribute("汤姆猫");
return "index";
}
3、Map集合
@RequestMapping("/welcome.form")
public String welcome(Map<String,Object> map){
map.put("password","123456");
return "index";
}