package net.biancheng.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributeController { // 方法无返回值 @ModelAttribute public void myModel(@RequestParam(required = false) String name, Model model) { model.addAttribute("name", name); } @RequestMapping(value = "/model") public String model() { return "index"; } }创建 index.jsp 页面,代码如下。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>编程帮</title> </head> <body> ${name } </body> </html>访问地址:http://localhost:8080/springmvcDemo2/model?name=%E7%BC%96%E7%A8%8B%E5%B8%AE,运行结果如图 1 所示。
图 1:运行结果
@RequestMapping(value = "/model") public String model(@RequestParam(required = false) String name, Model model) { model.addAttribute("name", name); return "index"; }
package net.biancheng.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributeController { // 方法有返回值 @ModelAttribute("name") public String myModel(@RequestParam(required = false) String name) { return name; } @RequestMapping(value = "/model") public String model() { return "index"; } }修改 index.jsp,代码如下。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>编程帮</title> </head> <body> ${string } </body> </html>访问地址和运行结果与示例 1 相同。
model.addAttribute("string", name);
。// 方法有返回值 @ModelAttribute("name") public String myModel(@RequestParam(required = false) String name) { return name; }等同于
model.addAttribute("name", name);
@RequestMapping("/register") public String register(@ModelAttribute("user") UserForm user) { if ("zhangsan".equals(uname) && "123456".equals(upass)) { logger.info("成功"); return "login"; } else { logger.info("失败"); return "register"; }上述代码中“@ModelAttribute("user") UserForm user”语句的功能有两个:
package net.biancheng.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributeController { // @ModelAttribute和@RequestMapping同时放在方法上 @RequestMapping(value = "/index") @ModelAttribute("name") public String model(@RequestParam(required = false) String name) { return name; } }index.jsp 代码如下。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>编程帮</title> </head> <body> ${name } </body> </html>访问地址:http://localhost:8080/springmvcDemo2/index?name=%E7%BC%96%E7%A8%8B%E5%B8%AE,运行结果如图 1 所示。
package net.biancheng.controller; import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.ModelAttribute; public class BaseController { @ModelAttribute public void isLogin(HttpSession session) throws Exception { if (session.getAttribute("user") == null) { throw new Exception("没有权限"); } } }创建 ModelAttributeController ,代码如下所示:
package net.biancheng.controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/admin") public class ModelAttributeController extends BaseController { @RequestMapping("/add") public String add() { return "addSuccess"; } @RequestMapping("/update") public String update() { return "updateSuccess"; } @RequestMapping("/delete") public String delete() { return "deleteSuccess"; } }在上述 ModelAttributeController 类中的 add、update、delete 请求处理方法执行时,首先执行父类 BaseController 中的 isLogin 方法判断登录权限,可以通过地址“http://localhost:8080/springMVCDemo2/admin/add”测试登录权限。
本文链接:http://task.lmcjl.com/news/19111.html