Spring MVC 学习 之 - URL参数传递
2015-07-13 11:01
706 查看
原文资料: Spring
MVC 学习 之 - URL参数传递
//控制器类
package com.springmvc.controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;
import com.springmvc.entity.UserEntity;
/*
* @Controller 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。
* @RequestMapping 指定URL映射路径,如果在控制器上配置 RequestMapping ,具体请求方法也配置路径则映射的路径为两者路径的叠加
* @ResponseBody 将注解方法对应的字符串直接返回
* @RequestParam 自动映射URL对应的参数到Action上面的数值,RequestParam 默认为必填参数。
* @PathVariable 获取@RequestMapping 配置指定格式的URL映射参数
*/
@Controller
@RequestMapping("urlinfo")
public class TController {
private static final String ShowMsg = null;
/*
* 直接输出 HTML,或JSON 字符串 请求路径: /web1/urlinfo/getcontent.html?key=rhythmk
* /web1/urlinfo/getcontent.json?key=rhythmk
*/
@ResponseBody
@RequestMapping(value = "/getcontent.**")
public String GetContent(
@RequestParam("key") String key,
@RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
System.out.println("getcontent 被调用");
String result = "直接返回内容 - key:" + key + ",key2:" + key2;
System.out.println(result);
return result;
}
/*
* RequestMapping 支持 Ant 风格的URL配置 : 请求路径:
* /urlinfo/geturlant/config.html?key=adddd
*/
@ResponseBody
@RequestMapping(value = "/geturlant/**.html")
public String getUrlAnt(HttpServletRequest request) {
String result = "?后面的参数为:" + request.getQueryString();
return result;
}
/*
* 配置指定格式的URL,映射到对应的参数 请求路径:/web1/urlinfo/geturlparam/12_123.html
*/
@ResponseBody
@RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
public ModelAndView getUrlParam(@PathVariable("id") String id,
@PathVariable("menuId") String menuId) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "获取到的Id:" + id + ",menuId:" + menuId);
return mode;
}
/*
* 只接收Post 请求 请求路径:/web1/urlinfo/posturl.html
*/
@ResponseBody
@RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
public String UrlMethod(
@RequestParam(value = "keyid", required = true, defaultValue = "defaultValue") String id) {
return "只能是Post请求,获取到的Id:" + id;
}
/*
* 写入 cookie 请求路径:/web1/urlinfo/writecookies.html?value=1
*/
@ResponseBody
@RequestMapping("/writecookies.html")
public ModelAndView writeCookies(@RequestParam String value,
HttpServletResponse response) {
response.addCookie(new Cookie("key", value));
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies 写入成功");
return mode;
}
/*
* 通过 @CookieValue 获取对应的key的值 请求路径:/web1/urlinfo/getcookies.html
*/
@ResponseBody
@RequestMapping("/getcookies.html")
public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies=" + cookvalue);
return mode;
}
/*
* 将 Servlet Api 作为参数传入 可以在action中直接使用
* HttpServletResponse,HttpServletRequest
*/
@ResponseBody
@RequestMapping("/servlet.html")
public String Servlet1(HttpServletResponse response,
HttpServletRequest request) {
Boolean result = (request != null && response != null);
ModelAndView mode = new ModelAndView();
mode.addObject("msg", "result=" + result.toString());
return ShowMsg;
}
/*
* 根据URL传入的参数实例化对象
*
* 如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?username=asf&age=22 */
@ResponseBody
@RequestMapping("getobject.html")
public ModelAndView getObject(UserEntity user) {
String result = "用户名称:" + user.getUsername().toString() + ",用户年龄:"
+ user.getAge().toString();
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "result=" + result.toString());
return mode;
}
/*
* 实现页面跳转 /web1/urlinfo/redirectpage.html
*/
@RequestMapping("/redirectpage.html")
public String RedirectPage() {
return "redirect:getcookies.html?r=10";
}
@ResponseBody
@RequestMapping("/getuser.json")
public UserEntity GetUser() {
System.out.println("getuser");
UserEntity model = new UserEntity();
model.setAge("21");
model.setUsername("王坤");
return model;
}
}
//模型类
package com.springmvc.entity;
public class UserEntity {
private String username;// 用户名
private String password;// 密码
private String nickname;// 昵称
private String gender;// 性别
private String age;// 年龄
public void setUsername(String value) {
this.username = value;
}
public void setPassword(String value) {
this.password = value;
}
public void setNickname(String value) {
this.nickname = value;
}
public void setGender(String value) {
this.gender = value;
}
public void setAge(String value) {
this.age = value;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getNickname() {
return this.nickname;
}
public String getGender() {
return this.gender;
}
public String getAge() {
return this.age;
}
}
MVC 学习 之 - URL参数传递
//控制器类
package com.springmvc.controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;
import com.springmvc.entity.UserEntity;
/*
* @Controller 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。
* @RequestMapping 指定URL映射路径,如果在控制器上配置 RequestMapping ,具体请求方法也配置路径则映射的路径为两者路径的叠加
* @ResponseBody 将注解方法对应的字符串直接返回
* @RequestParam 自动映射URL对应的参数到Action上面的数值,RequestParam 默认为必填参数。
* @PathVariable 获取@RequestMapping 配置指定格式的URL映射参数
*/
@Controller
@RequestMapping("urlinfo")
public class TController {
private static final String ShowMsg = null;
/*
* 直接输出 HTML,或JSON 字符串 请求路径: /web1/urlinfo/getcontent.html?key=rhythmk
* /web1/urlinfo/getcontent.json?key=rhythmk
*/
@ResponseBody
@RequestMapping(value = "/getcontent.**")
public String GetContent(
@RequestParam("key") String key,
@RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
System.out.println("getcontent 被调用");
String result = "直接返回内容 - key:" + key + ",key2:" + key2;
System.out.println(result);
return result;
}
/*
* RequestMapping 支持 Ant 风格的URL配置 : 请求路径:
* /urlinfo/geturlant/config.html?key=adddd
*/
@ResponseBody
@RequestMapping(value = "/geturlant/**.html")
public String getUrlAnt(HttpServletRequest request) {
String result = "?后面的参数为:" + request.getQueryString();
return result;
}
/*
* 配置指定格式的URL,映射到对应的参数 请求路径:/web1/urlinfo/geturlparam/12_123.html
*/
@ResponseBody
@RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
public ModelAndView getUrlParam(@PathVariable("id") String id,
@PathVariable("menuId") String menuId) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "获取到的Id:" + id + ",menuId:" + menuId);
return mode;
}
/*
* 只接收Post 请求 请求路径:/web1/urlinfo/posturl.html
*/
@ResponseBody
@RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
public String UrlMethod(
@RequestParam(value = "keyid", required = true, defaultValue = "defaultValue") String id) {
return "只能是Post请求,获取到的Id:" + id;
}
/*
* 写入 cookie 请求路径:/web1/urlinfo/writecookies.html?value=1
*/
@ResponseBody
@RequestMapping("/writecookies.html")
public ModelAndView writeCookies(@RequestParam String value,
HttpServletResponse response) {
response.addCookie(new Cookie("key", value));
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies 写入成功");
return mode;
}
/*
* 通过 @CookieValue 获取对应的key的值 请求路径:/web1/urlinfo/getcookies.html
*/
@ResponseBody
@RequestMapping("/getcookies.html")
public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies=" + cookvalue);
return mode;
}
/*
* 将 Servlet Api 作为参数传入 可以在action中直接使用
* HttpServletResponse,HttpServletRequest
*/
@ResponseBody
@RequestMapping("/servlet.html")
public String Servlet1(HttpServletResponse response,
HttpServletRequest request) {
Boolean result = (request != null && response != null);
ModelAndView mode = new ModelAndView();
mode.addObject("msg", "result=" + result.toString());
return ShowMsg;
}
/*
* 根据URL传入的参数实例化对象
*
* 如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?username=asf&age=22 */
@ResponseBody
@RequestMapping("getobject.html")
public ModelAndView getObject(UserEntity user) {
String result = "用户名称:" + user.getUsername().toString() + ",用户年龄:"
+ user.getAge().toString();
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "result=" + result.toString());
return mode;
}
/*
* 实现页面跳转 /web1/urlinfo/redirectpage.html
*/
@RequestMapping("/redirectpage.html")
public String RedirectPage() {
return "redirect:getcookies.html?r=10";
}
@ResponseBody
@RequestMapping("/getuser.json")
public UserEntity GetUser() {
System.out.println("getuser");
UserEntity model = new UserEntity();
model.setAge("21");
model.setUsername("王坤");
return model;
}
}
//模型类
package com.springmvc.entity;
public class UserEntity {
private String username;// 用户名
private String password;// 密码
private String nickname;// 昵称
private String gender;// 性别
private String age;// 年龄
public void setUsername(String value) {
this.username = value;
}
public void setPassword(String value) {
this.password = value;
}
public void setNickname(String value) {
this.nickname = value;
}
public void setGender(String value) {
this.gender = value;
}
public void setAge(String value) {
this.age = value;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getNickname() {
return this.nickname;
}
public String getGender() {
return this.gender;
}
public String getAge() {
return this.age;
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统