如何将get方法中的id传递给spring mvc中的post方法?

时间:2016-11-22 03:06:29

标签: spring-mvc

我的控制器:

       @RequestMapping(value = "/BankEdit", method = RequestMethod.GET) public ModelAndView BankEdit(HttpServletRequest request, HttpServletResponse response,BankBean bankBean) 
        {       
        ModelAndView model= null;
        model = new ModelAndView("accounts/company/manage_bank_edit");  
        long bName=Long.parseLong(request.getParameter("bName"));
        System.out.println("Banme get "+request.getParameter("bName"));


        return model;
        }

我在get方法中获取bName值...我在post方法中需要相同的值... getting null value

POst方法:

      @RequestMapping(value = "/BankEdit", method = RequestMethod.POST) public ModelAndView BankEditPost(HttpServletRequest request, HttpServletResponse response,BankBean bankBean) throws Exception
        {       
        ModelAndView model= null;
        model = new ModelAndView("accounts/company/manage_bank");
        long session_id=(Long) request.getSession().getAttribute("sessionId");
         long sessionBId=(Long) request.getSession().getAttribute("sessionBId");

         System.out.println("B_name==="+request.getParameter("bName"));

         long bName=Long.parseLong(request.getParameter("bName"));

            bankBean = accDao.editBank(bankBean,sessionBId,session_id,bName);


        return model;
        }       

1 个答案:

答案 0 :(得分:0)

在post方法中,您尝试检索参数值bName。就像get方法一样。

对于GET,请求值将作为参数发送,如~/BankEdit?name1=value1&name2=value2
所以request.getParameter("bName")你得到了那个价值。

对于POST方法值,通过消息体发送不发送参数,因此您得到null request.getParameter("bName"))因为您尝试从参数请求URL中提取。

对于收到的POST值,您需要在方法参数上声明参数对象,并从消息体中获取值。
如果bName属于BankBean,那么您将从BankBean.bName对象中退出 如果没有,那么在你的方法论证中声明并获得你的价值。

如果bName是BankBean的对象

@RequestMapping(value = "/BankEdit", method = RequestMethod.POST)
public ModelAndView BankEditPost(HttpServletRequest request, HttpServletResponse response,BankBean bankBean) throws Exception{       
    ModelAndView model= null;
    model = new ModelAndView("accounts/company/manage_bank");
    long session_id=(Long) request.getSession().getAttribute("sessionId");
    long sessionBId=(Long) request.getSession().getAttribute("sessionBId");

    System.out.println("B_name=== "+bankBean.bName);

    long bName=Long.parseLong(bankBean.bName);

    bankBean = accDao.editBank(bankBean,sessionBId,session_id,bName);

    return model;
}

以其他方式收到字符串

@RequestMapping(value = "/BankEdit", method = RequestMethod.POST)
public ModelAndView BankEditPost(HttpServletRequest request, HttpServletResponse response,BankBean bankBean, String stringValue) throws Exception{       
    ModelAndView model= null;
    model = new ModelAndView("accounts/company/manage_bank");
    long session_id=(Long) request.getSession().getAttribute("sessionId");
    long sessionBId=(Long) request.getSession().getAttribute("sessionBId");

    System.out.println("B_name=== "+stringValue);

    long bName=Long.parseLong(stringValue);

    bankBean = accDao.editBank(bankBean,sessionBId,session_id,bName);

    return model;
}