1.@Controller,任何java类都可以作为控制器,只要它被@Controller注解了
2.@RequestMapping
2.1@RequestMapping可以修饰java类
*则请求从原来的******/hello.do
*变成******/springmvc/hello.do
@Controller
@RequestMapping("/springmvc")
public class springMVCHelloWorld2 {
@RequestMapping("/hello2.do")
public String hello() {
System.out.println("进入请求方法类......");
return "success";
}
}
2.2@RequestMapping的四个属性,来精确映射:
value=“请求url”,如果一个注解,只有一个value属性,则value可以省略,如:@RequestMapping("/hello.do")
method=“method=RequestMethod.GET”或method=RequestMethod.POST....RequestMethod-是个枚举类
params={"id"} 请求参数里必须有一个参数名为id的参数!
params={"id=12","name!=Tom"}请求参数里必须有一个参数名为id=12,name!=Tom的.......
headers={"accept"}请求头中必须包含accept!
代码如下:
@RequestMapping(value="/testRequestMapping.do",method=RequestMethod.POST,params={"id"},headers={"accept"})
public String testRequestMapping() {
System.out.println("进入testRequestMapping请求方法类......");
return "success";
}
3.@RequestParam
==>@RequestParam(value="",required=true/false)
括号中value是参数名,required-是否是必须参数!
前端页面jsp:
<form action="testRequestParam.do" method="get" >
<input type="text" name="id" value="12"/>
<input type="text" name="name" value="xuxu"/>
......
</form>
后端请求方法:
@RequestMapping(value="/testRequestParam.do",method=RequestMethod.GET)
public String testPathVariable(@RequestParam(value="id",required=true) int id,@RequestParam(value="name",required=false) String name) {
//@RequestParam()括号中value是参数名,required-是否是必须参数!
return "success";
}
(如果形参名与请求参数名一致,则@RequestParam可以省略)
3.1@RequestParam延伸:===>POJO绑定请求参数
前端页面jsp:
<form action="testRequestParam.do" method="get" >
<input type="text" name="id" value="12"/>
<input type="text" name="name" value="xuxu"/>
......
</form>
后端请求方法:如果请求参数中id,name与POJO的属性一致,则自动绑定给User对象的属性
@RequestMapping(value="/testRequestParam.do",method=RequestMethod.GET)
public String testRequestParam(User) {
//如果请求参数中id,name与POJO的属性一致,则自动绑定给User对象的属性
return "success";
}
pojo类:User,有属性id,name,Address对象属性,set、get方法,toString()方法,构造函数.....
4.@PathVariable==>绑定带有占位符的url里的参数
前端页面jsp:<a href="/testPathVariable.do/12">.....</a><!--12是参数-->
后端请求方法:
@RequestMapping(value="/testPathVariable.do/{id}",method=RequestMethod.GET)//映射url中{id}是占位符,参数名id
public String testPathVariable(@PathVariable("id") int id) {
//@PathVariable("id")括号中是占位符的参数名,自动绑定到方法的形参id上
System.out.println("进入testPathVariable请求方法类......");
System.out.println(id);
return "success";
}

5.@RequestHeader
6.@CookieValue
7.@ModelAndView (pojo对象+视图)
===>作用域:仅这次请求requset内,${requestScope.模型数据参数名}
后台请求方法代码:
@RequestMapping(value="testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView mv = new ModelAndView();
//1.添加一个模型数据
mv.addObject("id",12);
//2.设置跳转视图
mv.setViewName("success");
//3.返回的是ModelAndView对象
return mv;
}
延伸:Map、Model、ModelMap ===>Springmvc自动给每次请求创建的模型数据(这三种)
同样作用域:requestScope
前台代码:
后台请求方法:
@RequestMapping(value="testMap")
public ModelAndView testMap(Map<String,Object> map) {
map.put("age",12);
return "success";
}
@RequestMapping(value="testModel")
public ModelAndView testModel(Model model) {
model.addAttribute("mail","123@123.com");
return "success";
}
8.@ModelAttribute==>作用:在做CRUD操作前,事先从DB读取绑定出模型数据。
则后台方法里绑定的形参user(只有:id=1,name=Jim,birthDay=null), update之后birthDay就是空的,就丢失了之前的数据。so,这时候@ModelAttribu
被@ModelAttribute修饰的方法,在所有映射方法(请求方法)之前,优先执行,做准备工作。
8.1-@ModelAttribute -- 修饰*无*返回值的方法:
优先执行@ModelAttribute修饰的方法:
@ModelAttribute
public void firstModelAttribute(Map<String, Object> map) {
//这里用new对象来-模拟从DB读取出User对象。。。。
User user = new User("1", "Tom", "2016-1-1");
//map.put("user", user);//不给绑定模型起别名,自动默认是POJO类的小写字母全称
map.put("abc", user);//给绑定模型起别名
}
再执行映射请求方法/testModelAttribute1.do:
@RequestMapping(value="/testModelAttribute1.do")
public String testModelAttribute(@ModelAttribute("abc")User user) {
//如果绑定模型没有起别名,则该映射方法的形参:User user,不加@ModelAttribute("别名")了
System.out.println(user);
return "success";
}
8.2-@ModelAttribute -- 修饰*有*返回值的方法:
@ModelAttribute(value="abc")//给绑定模型起别名,若不给绑定模型起别名,自动默认是POJO类的小写字母全称:user
public User firstModelAttribute2(Map<String, Object> map) {
//这里用new对象来-模拟从DB读取出User对象。。。。
User user = new User("1", "Tom", "2016-1-1");
return user;
}
@RequestMapping(value="/testModelAttribute2.do")
public String testModelAttribute2(@ModelAttribute("abc")User user) {
//如果绑定模型没有起别名,则该映射方法的形参:User user,不加@ModelAttribute("别名")了
System.out.println(user);
return "success";
}
9.@SessionAttributes ==>作用域:一次会话session。用来修饰整个控制器的类
package com.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.pojo.User;
@Controller
@SessionAttributes(value={"id","name"})
public class springMVCHelloWorld {
@RequestMapping(value="/test.do")
public String test(Map<String, Object> map) {
map.put("id",12);
return "success";
}
}
前台从${sessionScope.**}里取值
10.@Component===>自定义视图 及 解析器
注意事项:@Component
//单独@Component,则默认的bean-id(.xml中配置需要的)是java类的小写字母名myview
//若@Component(value="abc"),则逻辑视图名是abc
package com.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
@Component
//单独@Component,则默认的bean-id(.xml中配置需要的)是java类的小写字母名myview
//若@Component(value="abc"),则逻辑视图名是abc
public class MyView implements View{//实现view接口
@Override
public String getContentType() {
//告诉spring,内容格式contentType:
return "text/html";
}
@Override//渲染
public void render(Map<String, ?> map, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//这里是自定义的(物理)视图
response.getWriter().write("<h2>这是我的自定义视图View</h2>");
}
}
第2步:在springmvc.xml中配置自定义的视图解析器BeanNameViewResolver!(好好看xml中的备注!)
<!--
配置自定义视图的视图解析器:BeanNameViewResolver
-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
<!-- name="order"定义优先级,
InternalResourceViewResolver的默认视图解析器的优先级是无穷大,
所以自定义的BeanNameViewResolver随便取值100,的优先级排在InternalResourceViewResolver前
-->
</bean>
<mvc:view-controller path="/myview" view-name="myview"/>
<!-- 注意事项:spring中,
如果给某一映射配置了<mvc:view-controller>!
则其他控制器方法都会失效!!!
所以再行配置<mvc:annotation-driven>,使其他控制器方法有效!!
-->
<mvc:annotation-driven></mvc:annotation-driven>
注意事项:
<!-- 注意事项:springmvc.xml中,
如果给某一映射配置了<mvc:view-controller>!
则其他控制器方法都会失效!!!
所以再行配置<mvc:annotation-driven>,使其他控制器方法有效!!
-->
<mvc:view-controller path="/myview" view-name="myview"/>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:view-controller>、<mvc:annotation-driven>要成对出现!!