2016-11-22 63 views
0

我的控制器:如何將id從get方法傳遞到彈簧mvc中的post方法?

 @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價值...我需要在後method..getting空值相同的值

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; 
     }  
+0

你在帖子正文發送'bName'? – Blank

回答

0

在post方法您嘗試檢索參數值bName。就像get方法。

對於GET請求值將被髮送的參數一樣~/BankEdit?name1=value1&name2=value2
所以request.getParameter("bName")你得到的價值。

對於POST方法值通過消息體發送不發送參數,所以你得到空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; 
} 
相關問題