朝阳商城网站建设,wordpress 弹出层,做网站有生意吗,网站开发环境是什么记录一些很基本的使用方法。
一、GET请求传参方法#xff1a;
1.方法一#xff1a;把参数传到#xff1f;之后 使用注解RequestParam
// 假如传值了current和limit /students?current1limit20
RequestMapping(value /students, method RequestMetho…记录一些很基本的使用方法。
一、GET请求传参方法
1.方法一把参数传到之后 使用注解RequestParam
// 假如传值了current和limit /students?current1limit20
RequestMapping(value /students, method RequestMethod.GET)
ResponseBody
// name代表传的参数名require代表不传这个值也行defaultvalue代表默认值
// 默认current和limit参数是int类型
public String getStudents(RequestParam(name current, required false, defaultValue 1) int current,RequestParam(name limit, required false, defaultValue 1) int limit) {return some students current limit;
}
2.方法二传到路径当中 使用注解PathVariable指定
// /student/123 查找一个学生
RequestMapping(value /student/{id}, method RequestMethod.GET)
ResponseBody
public String getStudent(PathVariable(id) int id) {return a student id;
}
二、POST请求传参方法默认使用表单提交获取表单中字段的name属性
// 这里表单传入了一个学生的姓名和年龄
RequestMapping(value /student, method RequestMethod.POST)
ResponseBody
public String saveString(String name, int age) {System.out.println(name: name);return success name age;
} 三、响应HTML数据分别使用ModelAndView 和Model
使用ModelAndView
// 这里是model和view都封装在一个对象中
// 不加ResponseBody 返回的就是html 加了就返回其他对象比如json
RequestMapping(value /teacher, method RequestMethod.GET)
public ModelAndView getTeacher(ModelAndView modelAndView) {modelAndView.addObject(name,ym);modelAndView.addObject(age, 21);// 指定一下模板目录 会默认在templates目录下 下面的/view也就是view.htmlmodelAndView.setViewName(/demo/view);return modelAndView;
}
使用Model
// model与view分开
RequestMapping(value /school, method RequestMethod.GET)
public String getSchool(Model model) {model.addAttribute(name, 北京大学);model.addAttribute(age, 100);return /demo/view;
}
四、响应JSON数据主要针对异步请求ajax等等
// Java对象返回给浏览器解析为JS对象就用JSON完成前面的操作 java-json-js对象
RequestMapping(value /emp, method RequestMethod.GET)
ResponseBody
public MapString, Object getEmp() {MapString, Object emp new HashMap();emp.put(name, ljm);emp.put(salary, 18000);emp.put(age, 21);return emp;
}
五、cookie示例
分别编写了设置cookie和获取cookie的方法
GetMapping(/cookie/set)
ResponseBody
// 把cookie放在response响应里
public String setCookie(HttpServletResponse response) {// 创建cookie 这里使用uuid随机创建值Cookie cookie new Cookie(code, CommunityUtil.generateUUID());// 设置cookie生效的范围 也就是说只在访问下面这个链接时生效cookie.setPath(/community/alpha);// 默认关闭浏览器就失效 但可以设置时间保证cookie的存活时间cookie.setMaxAge(60 * 10);// 发送cookieresponse.addCookie(cookie);return set cookie;
}RequestMapping(/cookie/get)
ResponseBody
// “code”是上面设置的值
public String getCookie(CookieValue(code) String code) {System.out.println(code: code);return get cookie;
} 六、session示例
RequestMapping(/session/set)
ResponseBody
public String setSession(HttpSession session) {session.setAttribute(id, 1);session.setAttribute(name, Test);return set session;
}
RequestMapping(/session/get)
ResponseBody
public String getSession(HttpSession session) {System.out.println(session.getAttribute(id));System.out.println(session.getAttribute(name));return get session;
}