Jagjeet Singh
Jagjeet Singh

Reputation: 27

Passing Data from jsp to controller in Spring mvc

I am new to Springs and I am stuck at passing data from jsp to controller What i have here is a listing page where user is allowed to click on the primary key and then that primary key should pass to controller for it to detect the incoming request of the user to view the page

I have my listing jsp as follows

    <%@ 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>

My Controller

@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);
}

}

Could anyone please give me a snippet how to achieve the task

My url gets append to browser as i want : localhost:8080/SpringMVC/userView.do?userId=1

I need this userId value in my controller thats it :)

Upvotes: 0

Views: 9065

Answers (1)

SzaboAdam
SzaboAdam

Reputation: 324

You can get the userId request param with

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

Of course you would need additional error parsing (to not throw nullpointer, numberformat...)

In my opinion, you should use @RequestParam annotation instead

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

Then the userId you get will be automatically converted to int for you, and Spring returns with HTTP BAD REQUEST if it is not present

Upvotes: 1

Related Questions