Spring MVC 学习 之 - URL参数传递

原文资料:  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;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值