标签:fun 作用域 rip package insert tin jar包 作用 ice
@RequestMapping("/login") public String login(HttpServletRequest request){ String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); return "success"; }
前端请求的表单
<form action="${pageContext.request.contextPath}/getParam" method="post"> 用户名:<input type="text" name="name" > 密码:<input type="text" name="pwd" > <input type="submit" value="submit"> </form>
Controller代码
@RequestMapping(value="/getParam",method=RequestMethod.POST) public String getParam1(String name,String pwd){ System.out.println("用户名"+name); System.out.println("密码"+pwd); return "success"; }
package com.neuedu.bean; /* * 项目名称:SpringMVC-02 * @author:wzc * @date 创建时间:2017年8月22日 上午9:23:33 * @Description:定义学生类 * @parameter * */ public class Student { private String name; private String email; private int sex; private Address address; public Student() { super(); } public Student(String name, String email, int sex) { super(); this.name = name; this.email = email; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ", email=" + email + ", sex=" + sex + ", address=" + address + "]"; } }
Controller代码段
@RequestMapping("/getStudent") public String getStudent(Student student){ String name = student.getName(); String email = student.getEmail(); System.out.println("name:"+name); System.out.println("email:"+email); String studnetIn = student.toString(); System.out.println(studnetIn); return "success"; }
<a href="${pageContext.request.contextPath }/testUrl?id=5">走一个,测试URL</a><br>
@RequestMapping(value="/testUrl",method=RequestMethod.GET) public String testURL( @RequestParam(value="id") Integer id){ // service.getOrderById(id) System.out.println("---------"+id); return "success"; }
<a href="${pageContext.request.contextPath}/order/1">测试get</a><br>
@RequestMapping(value="/order/{id}") public String getorderbyId1(@PathVariable(value="id") Integer id){ // service.getOrderById(id) System.out.println("---------"+id); return SUCCESS; }
value:这个字段要与请求参数的name属性值一致!
required:布尔值,默认是true,当指定为false的时候,说明这个参数不是必须的,可以不带!
defaultValue:在我们不传值的时候,默认使用defaultValue的值,传递参数的时候,使用我们传递的参数值!
HttpServletRequest
HttpServletResponse
HttpSession
public String login(HttpServletRequest request,HttpServletResponse response ,HttpSession session){ }
如:request。setAttribute(“user”,new User("张三",1));
ModelAndView ,Map ,Model,ModelMap
–ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据
–Map 及 Model、ModelMap: 入参为 org.springframework.ui.Model、org.springframework.ui. ModelMap 或 java.uti.Map 时,
处理 方法返回时,Map 中的数据会自动添加到模型中。
@RequestMapping("/testMoudle") public String testMoudle(Map<String,Object> map){ map.put("user", "张三"); return "success"; } @RequestMapping("/testMoudle1") public ModelAndView testMoudle1(){ ModelAndView mAndView=new ModelAndView(); mAndView.addObject("user", "李四"); mAndView.setViewName("success"); return mAndView; } @RequestMapping("/testMoudle2") public String testMoudle(ModelMap modelMap){ modelMap.addAttribute("user", "王五"); return "success"; } @RequestMapping("/testMoudle3") public String testMoudle2(Model model){ model.addAttribute("user", "赵七"); return "success"; }
无论我们的返回值是String类型还是ModelAndView类型,SpringMVC框架执行目标Handler方法之后都会将返回值解析为ModelAView;
我们放入到Map或者Model、ModelMap中的数据都会放入ModelAndView对象中,作为MOdel使用!
在前端页面中都可以通过作用域将参数取出来
1.加入json的3个jar包
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
2. 编写目标方法,使其返回 JSON 对应的对象或集合
3. 在方法上添加 @ResponseBody 注解
<%@ 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>Insert title here</title> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ var url="${pageContext.request.contextPath}/testJson"; var data={}; function callback(date){ for(var i= 0;i < date.length; i++){ alert(date[i].name); } } $.post(url,data,callback); }); }); </script> </head> <body> <button name="OK" id="btn">测试JSON</button> </body> </html>
@RequestMapping(value="/testJson",method=RequestMethod.POST) @ResponseBody public List<Student> testJSON(){ List<Student> list=new ArrayList<>(); list.add(new Student("张三 ", 0)); list.add(new Student("李四 ", 1)); list.add(new Student("王五 ", 0)); return list; }
标签:fun 作用域 rip package insert tin jar包 作用 ice
原文地址:http://www.cnblogs.com/Actexpler-S/p/7425637.html