jar包:
链接:https://pan.baidu.com/s/14-E9oVfcZHJz1b4r1Kej9A
提取码:17vm
创建项目;
HelloController.java类:
@Controller
//@RequestMapping("/hello")
@SessionAttributes("msg")
public class HelloController {
@RequestMapping("/fun1")
public ModelAndView fun1(){
ModelAndView mode=new ModelAndView();
mode.addObject("msg", "modeandview");
mode.setViewName("index");
return mode;
}
@RequestMapping("fun2")
public String fun2(Map<String,Object> map){
System.out.println("=========");
map.put("msg", "map msp……");
return "index";
}
@RequestMapping("fun3")
public String fun3(Model m){
System.out.println("=========");
m.addAttribute("msg", "model 数据");
return "index";
}
@RequestMapping("fun4")
public String fun4(ModelMap mm){
System.out.println("=========");
// mm.put("msg", "modelmap msp……");
mm.addAttribute("msg", "modelmap 数据");
return "index";
}
}
Spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.ytl.controller"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/"/>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>执行了Spring-MVC</h1>
<h1>返回数据:${msg}</h1>
<--测是数据用的那种方式传输-->
<h2>request:${requestScope.msg}</h2>
<h2>session:${sessionScope.msg}</h2>
<h2>application:${application.Scope.msg}</h2>
</body>
</html>
这里就只测试fun1了!