一、Multiaction Controller

package cn.framelife.mvc.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import cn.framelife.mvc.entity.User; @Controller
@RequestMapping("/user")
public class UserControl { /**
* 这种方法接收/user/create.abc请求
*/
@RequestMapping("create")
public ModelAndView createUser(User user){
System.out.println("createUser"); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
} /**
* 这种方法接收/user/update.abc请求
*/
@RequestMapping("update")
public ModelAndView update(){
System.out.println("updateUser"); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}
}

二、 ModelAndView

Controller处理方法的返回值为ModelAndView,既包括视图信息,也包括模型数据信息。

1、ModelAndView中的方法

能够使用下面方法加入模型数据:

    addObject(Object modelObject)
addObject(String modelName, Object modelObject)
addAllObjects(Map modelMap)

能够通过下面方法设置视图:

    setViewName(String viewName)
setView(View view)

2、重定向:

这里的重定向仅仅能重定向到其他的处理方法,不能重定向到页面。

假设想重定向到页面,那么在另外的的处理方法中跳转就是。

    @RequestMapping(value="/add",method = RequestMethod.GET)
public ModelAndView initForm(){
User user = new User();
return new ModelAndView("/add").addObject(user);
} @RequestMapping("create")
public ModelAndView createUser(){
ModelAndView view = new ModelAndView(); //这里会重定向到add.abc的处理方法中,再由add.abc的处理方法作页面跳转
RedirectView redirectView = new RedirectView("add.abc");
view.setView(redirectView); return view;
}

三、Controler处理方法获取值

1、获取页面值

页面Form表单:

<form action="user/create.abc" method="post">
用户名:<input type="text" name="username"><br/>
密 码:<input type="text" name="password"><br/>
其他:<input type="text" name="other"><br/>
<input type="submit">
</form>

A、 绑定到同名參数中

    /*
* @RequestParam能够用来提取名为“username”的String类型的參数。并将之作为输入參数传入
*/
@RequestMapping("create")
public ModelAndView createUser(@RequestParam("username") String username,String password,String other){
System.out.println(username+"-"+password+"-"+other);
ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}

B、 绑定到实体类模型数据中

User实体类:

public class User implements java.io.Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

Controller中的方法:

    @RequestMapping("create")
public ModelAndView createUser(User user){
System.out.println(user.getUsername()+"-"+user.getPassword());
ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}

C、既有模型又有同名參数

    /**
* 页面表单值传输时,假设User类中有同名的属性。那就绑定到User对象中
* 假设User类中没有的属性,能够使用同名參数接收
* 假设User类中也有,而我们又有同名參数。两个都能够接收
*/
@RequestMapping("create")
public ModelAndView createUser(User user,String username,String other){
System.out.println(user.getUsername()+"-"+user.getPassword()+"-"+username+"-"+other);
ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}

2、获取Servlet API

Controller中的处理方法:

    @RequestMapping("create")
public ModelAndView createUser(HttpServletRequest request,HttpServletResponse response,HttpSession session){
String username = request.getParameter("username");
System.out.println(username); request.setAttribute("requestName", "aaaaaaaaaaaa");
session.setAttribute("sessionName", "bbbbbbbbbbbb"); Cookie cookie = new Cookie("cookieName", "ccccccccccc");
response.addCookie(cookie); ModelAndView view = new ModelAndView();
view.setViewName("/success"); return view;
}

Success.jsp页面显示:

<body>
${requestName}<br/>
${sessionName}<br/>
${cookie.cookieName.value}<br/>
</body>

最新文章

  1. [大数据之Spark]——快速入门
  2. 使用Fragment创建灵活的用户界面
  3. python numpy包
  4. 什么办法可以替代distinct
  5. 五个有用的jquery小技巧
  6. 关于dom ready事件
  7. html之input系列标签
  8. spring mvc 数据绑定总结
  9. UVa839 Not so Mobile
  10. ccr test
  11. &lt;一&gt; MVC - HtmlHelper
  12. Axiom3D学习日记 2.介绍SceneManager,SceneNode,Entity
  13. UP UP UP!(dp)
  14. hdu 1698 Just a Hook(线段树基础)
  15. Chapter 1 First Sight——4
  16. 可能是迄今为止最好的GitHub代码浏览插件--赞
  17. 学习创建spring boot
  18. Java的date和string互转
  19. getdate — 取得日期/时间信息-----参数是一个 integer 的 Unix 时间戳
  20. Js正则校验身份证号码

热门文章

  1. (转)淘淘商城系列——SSM框架整合之逆向工程
  2. CAD设置超链接(网页版)
  3. NET使用SuperSocket完成TCP/IP通信
  4. Python 文件修改-函数介绍
  5. ceph集群
  6. jz2440开发板烧写裸板
  7. Oracle on Azure
  8. 全文搜索(A-4)-推荐系统架构
  9. MYSQL Sorting result 把队列堆满了,该怎么办?
  10. noip模拟赛 区间