0

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 :)

2
  • Your question is unclear. Few things we need to know. What is the value, you need to pass it into controller (mapEntry.value)? and how the URL looks in browser? Does it rendered properly?? I hope probably not, since you are wrongly combining string with c:out value. Commented Sep 3, 2014 at 17:05
  • what is your controller? Commented Sep 5, 2014 at 2:48

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.