2014-09-03 145 views
0

我是Spring的新手,我一直在將數據從jsp傳遞到控制器 我在這裏列出的是允許用戶單擊主鍵的列表頁面,主鍵應該傳遞給控制器​​,它檢測到用戶的傳入請求查看該頁面在Spring中將數據從jsp傳遞到控制器mvc

我有我的房源JSP如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
<style> 
.tableimp { 
    margin-top: 150px; 
} 
</style> 
</head> 
<body> 
    <table align="center" border="1" class="tableimp"> 
     <tr> 
      <th bgcolor="#2E64FE">USERNAME</th> 
      <!--   <td>&nbsp;&nbsp;</td> --> 
      <th bgcolor="#2E64FE">FIRSTNAME</th> 
      <!--   <td>&nbsp;&nbsp;</td> --> 
      <th bgcolor="#2E64FE">LASTNAME</th> 
      <!--   <td>&nbsp;&nbsp;</td> --> 
      <th bgcolor="#2E64FE">EMAIL</th> 
      <!--   <td>&nbsp;&nbsp;</td> --> 
      <th bgcolor="#2E64FE">USERID</th> 
     </tr> 

     <c:forEach items="${stkList}" var="maps"> 
      <tr> 
       <td colspan="5">&nbsp;&nbsp;</td> 
      </tr> 
      <tr> 
       <c:forEach items="${maps}" var="mapEntry"> 
        <td align="center"> <c:choose> 
          <c:when test="${mapEntry.key eq 'userId'}"> 
           <c:url var="userView" value="userView.do" /> 
           <a href="<c:out value='userView.do?userId=${mapEntry.value}' />">${mapEntry.value}</a> 
          </c:when> 
          <c:otherwise> 
          ${mapEntry.value} 
         </c:otherwise> 
         </c:choose></td> 
       </c:forEach> 
      </tr> 
     </c:forEach> 
    </table> 
</body> 
</html> 

我的控制器

@RequestMapping(value="/userView.do*", method=RequestMethod.GET) 
public ModelAndView viewUser(HttpServletRequest request) 
{ 
    try{ 
     //I need that userId Here somehow 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return new ModelAndView("form","",null); 
} 

}

任何人都可以請給我一個片斷如何實現任務

我的URL中獲取附加到瀏覽器,因爲我想:本地主機:8080 /用SpringMVC/userView.do用戶id = 1

我需要在我的控制器此用戶id值完蛋了:)

+0

你的問題還不清楚。幾件事我們需要知道。什麼是價值,你需要將它傳遞到控制器(mapEntry.value)?以及該網址在瀏覽器中的外觀?它是否正確渲染?我希望可能不會,因爲你錯誤地將字符串與'c:out'值組合。 – 2014-09-03 17:05:04

+0

你的控制器是什麼? – user23123412 2014-09-05 02:48:49

回答

1

你可以得到

Integer.parseInt(request.getParameter("userId")); 

當然喲的用戶ID請求PARAM ü將需要額外的錯誤解析(不丟空指針,NumberFormat的...)

在我看來,你應該使用@RequestParam註解來替代

@RequestMapping(value="/userView.do*", method=RequestMethod.GET) 
public ModelAndView viewUser(@RequestParam int userId) 

那麼你得到的將是用戶id自動轉換成int對於你而言,如果Spring不存在,Spring將返回HTTP BAD REQUEST

+0

工作就像魅力....非常感謝你 – 2014-09-06 16:37:59

相關問題