首页 技术 正文
技术 2022年11月15日
0 收藏 354 点赞 4,397 浏览 3966 个字

在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:

   @Controller:

在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。

12345 @Controllerpublic class UserAction{    }

  @RequestMapping

指定URL映射路径,如果在控制器上配置 RequestMapping  ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:RequestMapping(“url.html”)

配置映射路径:

@Controller
public class UserAction
{
@RequestMapping(value = "/get_alluser.html")
public ModelAndView GetAllUser(String Id)
{
}
}

以上配置映射

    http://***:8080:web1/get_alluser.html:

如在 @Controller添加 @RequestMapping(value = “/user”),则映射路径变成

http://***:8080:web1/user/get_alluser.html

   @ResponseBody

将注解方法对应的字符串直接返回

   @RequestParam

自动映射URL对应的参数到Action上面的数值,RequestParam 默认为必填参数

   @PathVariable

获取@RequestMapping 配置指定格式的URL映射参数

     /*
* 直接输出 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
*
* */ @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 请求
*/
@ResponseBody
@RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
public String UrlMethod(@RequestParam String id) {
return "只能是Post请求,获取到的Id:" + id;
}
    /*
* 写入 cookie
* */
@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的值
* */
@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
* */
@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?UserId=1&UserName=ad
* */
@RequestMapping("getobject.html")
public ModelAndView getObject(UserInfo user) {
String result = "用户ID:" + user.getUserId().toString() + ",用户名:"
+ user.getUserName().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"; }

直接回传JSON

    请求的URL地址一定是以.json结尾,否则异常

Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request “accept” headers () 

回传实体:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class UserInfo { private Integer UserId;
public Integer getUserId() {
return UserId;
}
public void setUserId(Integer userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
private String UserName;}

回传 action

@ResponseBody
@RequestMapping("/getuser.json")
public UserInfo GetUser()
{
System.out.println("getuser");
UserInfo model=new UserInfo();
model.setUserId(100);
model.setUserName("王坤");
return model;
}

请求:

/web1/urlinfo/getuser.json

输出:

{"userId":100,"userName":"张三"}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905