Spring MVC的简单使用方法
2024-08-24 21:02:05
一、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>
最新文章
- [大数据之Spark]——快速入门
- 使用Fragment创建灵活的用户界面
- python numpy包
- 什么办法可以替代distinct
- 五个有用的jquery小技巧
- 关于dom ready事件
- html之input系列标签
- spring mvc 数据绑定总结
- UVa839 Not so Mobile
- ccr test
- <;一>; MVC - HtmlHelper
- Axiom3D学习日记 2.介绍SceneManager,SceneNode,Entity
- UP UP UP!(dp)
- hdu 1698 Just a Hook(线段树基础)
- Chapter 1 First Sight——4
- 可能是迄今为止最好的GitHub代码浏览插件--赞
- 学习创建spring boot
- Java的date和string互转
- getdate — 取得日期/时间信息-----参数是一个 integer 的 Unix 时间戳
- Js正则校验身份证号码