2014-09-03 124 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

相关问题