栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring MVC 学习 之

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC 学习 之

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

@Controller:

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

@Controller

public class UserAction{ } 

@RequestMapping

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

配置映射路径:

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

以上配置映射

http:/ @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; }

 
  @ResponseBody
  @RequestMapping(value = "/geturlant
  
  @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;
  } 
  
  @ResponseBody
  @RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
  public String UrlMethod(@RequestParam String id) {
    return "只能是Post请求,获取到的Id:" + id;
  } 
   
  @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 ;
  } 

  
  @RequestMapping("/getcookies.html")
  public ModelAndView getcookie(@cookievalue("key") String cookvalue) {
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", "cookies=" + cookvalue);
    return mode;
  } 
  
  @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;

  } 
  
  @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;
  } 

 实现页面跳转:

  
  @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":"王坤"}

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号