1.1、Spring是IOC和AOP的容器框架,SpringMVC是基于Spring功能之上添加的Web框架,想用SpringMVC必须先依赖Spring。
1.2、SpringMVC是一个MVC模式的WEB开发框架;Spring是一个通用解决方案, 最大的用处就是通过Ioc/AOP解耦, 降低软件复杂性, 所以Spring可以结合SpringMVC等很多其他解决方案一起使用, 不仅仅只适用于WEB开发
junit
junit
4.11
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.2
org.aspectj
aspectjweaver
1.8.9
org.springframework
spring-aop
4.3.7.RELEASE
org.springframework
spring-aspects
4.3.7.RELEASE
org.springframework
spring-beans
4.3.7.RELEASE
org.springframework
spring-context
4.3.7.RELEASE
org.springframework
spring-core
4.3.7.RELEASE
org.springframework
spring-expression
4.3.7.RELEASE
org.springframework
spring-tx
4.3.7.RELEASE
org.springframework
spring-jdbc
4.3.7.RELEASE
org.springframework
spring-web
4.3.7.RELEASE
org.springframework
spring-webmvc
4.3.7.RELEASE
org.springframework
spring-test
4.3.7.RELEASE
test
org.mybatis
mybatis
3.4.4
org.mybatis
mybatis-spring
1.3.0
org.mybatis.generator
mybatis-generator-core
1.3.5
com.fasterxml.jackson.core
jackson-core
2.7.3
com.fasterxml.jackson.core
jackson-databind
2.7.3
com.fasterxml.jackson.core
jackson-annotations
2.7.3
javax.servlet
servlet-api
2.5
provided
commons-io
commons-io
2.5
1.2、web.xml
contextConfigLocation
classpath:spring/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
ce
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
ce
/*
ds
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:mvc/SpringMvc.xml
1
ds
/
1.3、SpringMvc.xml
/WEB-INF/jsp/
.jsp
1.4、Spring核心配置文件:applicationContext.xml
1.5、MyBatis核心配置文件
@Controller
@RequestMapping("con1")
public class TestController {
@RequestMapping("/test1")
public void test1(String name){//如果不使用@RequestParam,方法中的形参要和URL的参数名一致
System.out.println(name);
System.out.println("ssm整合1");
}
@RequestMapping("/test2")
public void test2(@RequestParam(name = "name")String a){
System.out.println(a);
System.out.println("ssm整合2");
}
@RequestMapping(value = "/testPost",method = RequestMethod.POST)//post方式提交
public void testPost(String name, int age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
System.out.println(name);
System.out.println(age);
System.out.println(date);
}
@RequestMapping(value = "/testGet",method = RequestMethod.GET)//get方式请求
public void testGet(@RequestParam Map map){
Set set = map.entrySet();
for (Map.Entry entry:set){
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
//restful风格的参数获取--url中只有参数值,参数名在注解中,路径变量的注解@pathVariable
@RequestMapping(value = "/test3/{name}/{age}/{birth}")
public void test3(@PathVariable String name,
@PathVariable Integer age,
@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birth){
System.out.println(name);
System.out.println(age);
System.out.println(birth);
}
}
2.2、解决乱码问题
ce
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
ce
/*
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/
8080
UTF-8
2.3、实现页面跳转
@RequestMapping("/test4")
public String test4(){//页面跳转--转发
return "test";
}
@RequestMapping("/test5")
public String test5(){//重定向
return "redirect:test4";
}
2.4、响应正文
//响应正文
//响应的一个字符串response.getWriter().writer("")
@RequestMapping("/test1")
@ResponseBody//响应正文
public String test1(){
return "hello!";
}
//响应的一个JavaBean使用json转换器
@RequestMapping(value = "/test2",produces = "text/html;charset=UTF-8")
@ResponseBody
public Object test2(){
User user = new User();
user.setName("韩冰");
user.setAge(18);
user.setBirth(new Date());
return user.toString();
}
//响应字节--图片
@RequestMapping(value = "/test3")
public void test3(HttpServletResponse response) throws Exception {
FileInputStream is = new FileInputStream("D:\\照片\\壁纸\\cat.jpg");
ServletOutputStream os = response.getOutputStream();
IOUtils.copy(is,os);
}
2.5、参数存储,存进request域对象,三种方式
@Controller
@RequestMapping("con3")
public class TestController3 {
//Model
@RequestMapping("/test1")
public String test1(Model model){
model.addAttribute("name","萧寒");
return "test";
}
//ModelMap
@RequestMapping("/test2")
public String test2(ModelMap map){
map.addAttribute("name","魏巍");
return "test";
}
//ModelAndView Model和jsp跳转的结合,返回值不是String
@RequestMapping("/test3")
public ModelAndView test3(ModelAndView mav){
mav.addObject("name","魏文");
mav.setViewName("test");
return mav;
}
}