- -
1.Controller配置总结
1.通过URL对应Bean
<!--配置handlerMapping-->
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerrMapping"/>
<!--配置请求的处理器-->
<bean name = "/hello.do" class = "cn.limbo.controller.HelloController"/>
以上配置,访问/hello.do就会自动寻找ID为/hello.do的bean,此类方法仅使用小型系统
2 . 为URL分配Bean
使用一个统一配置集合,对各个URL对应的Controller做关系映射,此类配置还可以使用通配符
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--key对应url名字,value对面处理bean的id -->
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<!-- 配置请求和处理器 -->
<bean id="helloController" class="hello.Hello"></bean>
4 . 注解(最常用,最省事)
需要添加扫描包,Controller代码中要写相应的注解
<context:component-scan base-package="cn.sxt.annotation"/>
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp){
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "hello Spring mvc annotation");
mv.setViewName("hello");
return mv;
}
}
**
2.结果跳转方式
**
1.在注解的方式中
1.1通过HttpServletResponse的API直接输出(不需要配置渲染器)
@Controller
public class RequestController{
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.getWriter().println("hello HttpServletResponse");
}
1.2 使用HttpServletResponse 重定向到另一个视图(其他不变 )
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.sendRedirect("index.jsp");
}
}
1.3 使用HttpServletRequest 转发(默认访问/下的index.jsp页面 不受渲染器的影响)
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
req.setAttribute("message","it's forword ");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
1.4直接返回jsp页面的名称(无渲染器)
@RequestMapping("/nice")
public String hello1(){
//转发方式1
return "home.jsp";
//转发方式2
return "forward:index.jsp";
//重定向方式
return "redirect:index.jsp";
}
1.5当有渲染器指定
@RequestMapping("/nice")
public String hello1(){
//转发方式1
return "home";
//转发方式2
return "forward:index";
//重定向方式 hello指的是requsrmapping
return "redirect:hello";
}
2 使用view
2.1 使用modelandview
需要视图解析器 能指定跳转页面
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//封装要显示到视图的数据
mv.addObject("msg","hello myfirst mvc");
//视图名
mv.setViewName("hello");
return mv;
}
}
2.处理模型数据
1.1. ModelAndView
/**
* SpringMVC 使用 ModelAndView 来解析视图和数据
* 如果返回的是字符串 执行方法后 还是会包装成一个 ModelAndView
* 最后会遍历 ModelMap将键值对存入request中
* @return
*/
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(){
ModelAndView mv = new ModelAndView();
mv.addObject("name", "bigsea");
mv.addObject("age", 23);
mv.setViewName("helloworld");
return mv;
}
1.2. Map 和Model
Spring MVC 在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。
– 如果方法的入参为 Map 或 Model 类型,Spring MVC 会将隐含模型的引用传
给这些入参。在方法体内,开发者可以通过这个入参对象访问到模型中的所有数
据,也可以向模型中添加新的属性数据
/**
* 当参数为Map时
* SpirngMVC 会传入 一个BindingAwareModelMap
* 往BindingAwareModelMap里面存入的值 会在后面存入request域中
* @param map
* @return
*/
@RequestMapping("/map")
public String map (Map<String, Object> map){
System.out.println(map.getClass().getName());
map.put("name", "bigsea");
map.put("age", 23);
return "helloworld";
}
1.3. @SessionAttributes
若希望在多个请求之间共用某个模型属性数据,则可以在控制器类上标注一个 @SessionAttributes,SpringMVC将在模型中对应的属性暂存到 HttpSession 中。
• @SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中
– @SessionAttributes(types=User.class) 会将隐含模型中所有类型为 User.class 的属性添加到会话中。
– @SessionAttributes(value={“user”,”admin”})
– @SessionAttributes(types={User.class, Department.class})
– @SessionAttributes(value={“user”,”admin”}, types={User.class})
/**
* 通过 @SessionAttributes 注解 在@SessionAttributes 中 设置 type 和 value
* type: 通过类型 来判断是否 将 属性存入 会话中
* value : 通过 ModelAndView 中 ModelMap 的 key 来判断 key 是否和 value 相等 如果相等 存入
* @return
*/
@RequestMapping("/sessionAttributes")
public ModelAndView sessionAttributes(){
User user = new User();
user.setName("bigsea");
user.setAge(23);
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
mv.addObject("user", user);
return mv;
}
1.4. @ModelAttribute
在方法定义上使用 @ModelAttribute 注解:Spring MVC在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法。
• 在方法的入参前使用 @ModelAttribute 注解:
– 可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数绑定到对象中,再传入入参
– 将方法入参对象添加到模型中
/**
* 当标注了@ModelAttribute
* SpirngMVC就会在调用目标方法前去执行该方法
* 将结果传入implicitMode中
* SpringMVC 中 大量的使用implicitMode
* @return
*/
@ModelAttribute("user")
public User modelAttribute(){
User user = new User();
user.setName("bigsea");
user.setAge(23);
return user;
}