在学习 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://***: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":"王坤"}

Spring MVC中页面向后台传值的几种方式的更多相关文章

  1. ASP中页面之间传递值的几种方式

    ASP.NET页面之间传递值的几种方式 页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryS ...

  2. 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式

    一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...

  3. ASP.NET MVC 下拉框的传值的两种方式

    以前使用WebForm变成时,下拉框传值只需直接在后台绑定代码就可以了.现在我们来看看在MVC中DropDownList是如果和接受从Controller传过来的值的. 第一种:使用DropDownL ...

  4. asp.net 页面跳转传值的几种方式

    参考地址: http://blog.csdn.net/zzzzzzzert/article/details/8486143 protected void Button1_Click(object se ...

  5. 记录初学Spring boot中使用GraphQL编写API的几种方式

    Spring boot+graphql 一.使用graphql-java-tools方式 <dependency> <groupId>com.graphql-java-kick ...

  6. Spring框架中获取连接池常用的四种方式

    1:DBCP数据源 DBCP类包位于 /lib/jakarta-commons/commons-dbcp.jar,DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池,所 ...

  7. spring mvc 通过配置xml访问控制器的三种方式

    (一)通过 name 来一一映射(默认) (二)通过简单url 来指定映射,key 表示访问url value 是bean的ID (三)通过控制类的类名控制器,访问时类名首字母需要小写 <!-- ...

  8. ASP.NET MVC 下拉框的传值的两种方式(第二种方式未完成)

    控制器代码: public ActionResult Index() { List<SelectListItem> sli = new List<SelectListItem> ...

  9. MVC中控制器向视图传值的四种方式

    MVC中的控制器向视图传值有四种方式分别是 1 ViewDate  2.ViewBag   3.TempDate  4.Model 下面分别介绍四种传值方式 首先先显示出控制器中的代码 using S ...

随机推荐

  1. STL非变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1394600460.html 原创:ST ...

  2. 关于Dictionary字典和List列表

    命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T> ...

  3. [Usaco2007 Dec]Building Roads 修建道路[最小生成树]

    Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...

  4. namespace 的作用

    在写CPP的时候,常常要写using namespace std;这么一句话,到底有什么用呢? #include <iostream> namespace first { ; } name ...

  5. 自定义radio图标

    问题: 默认的radio控件不是很好看,我们能否自定义一个radio图标? 解决: 1.radio有input和lable两个标签. 2.<input>是前面的图标,选中后图标变化. 3. ...

  6. ecexl操作

    /// <summary> /// 写入ecexl /// </summary> /// <param name="dt"></param ...

  7. 关于MEF

    MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要的库,Visual Studio 2010 Code Editor的扩展支持也是基 ...

  8. winform程序中为无边框窗体手动添加窗体拖动代码

            Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置        Point oldFormPoint;//记录开始移动窗口前窗体位置        // ...

  9. JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法

    在JAVASE5 中的java.util.concurrent.BlockingQueue支持,BlockingQueue是一个接口但是我们通常可以使用LinkedBlockingQueue,它是一个 ...

  10. QuickSwitchSVNClient,快速完成SVN Switch的工具

    [开源]QuickSwitchSVNClient,快速完成SVN Switch的工具 在实际的开发中,我们一般使用SVN工具进行源代码的管理.在实际的产品开发中,根据项目的一些定制要求,往往需要对某一 ...