Complicated
Complicated

Reputation: 85

Unable to Access Request Parameter in SpringMVC Controller while redirecting to URL

I am having a form which submits new articles to my controller.

JSP Page:

<form class="form-signin" method="post"
    action="/articleViewer">
<div class="control-group" style="margin-top: -5px;">
    <label class="control-label text-info" for="commentContent"><strong>Post
            Comment</strong></label>
    <div class="controls">
        <textarea class="FormElement" name="area2" id="commentContent"
        style="width: 100%;"></textarea>
    </div>
</div>
<button type="submit" class="btn btn-primary" style="margin-left: 90%;">Post</button>
</form>

Controller Method:

@RequestMapping(value="/articleViewer", method = RequestMethod.POST)
public String saveArticleComment (HttpServletRequest request, HttpServletResponse response,Principal principal, ModelMap map) throws ServletException, IOException{
    //processing request
    System.out.println("Link : "+Path.Jsp.ARTICLE_VIEWER_PAGE);
    return Path.Jsp.ARTICLE_VIEWER_PAGE; //this ARTICLE_VIEWER_PAGE = /articleViewer
}

Now from the above controller method I wanna redirect to another method where I want to pass currently saved article id as http://<myurl>/article?articleId="xyz".

Here is my get method code for handling the redirect.

@RequestMapping(value="/articleViewer", method= RequestMethod.GET)
public String articleViewer(HttpServletRequest request, Principal principal,
        ModelMap map, HttpServletResponse response)
        throws DatabaseException {
        //I wanna access article id here.
    return Path.Jsp.ARTICLE_VIEWER_PAGE;
} 

I wanna know how could I access that passed request parameter in above method?

Upvotes: 2

Views: 2675

Answers (3)

Complicated
Complicated

Reputation: 85

I Resolved it by using Redirect attribute in return ...

return "redirect:/articleViewer?varArticleID="+getVarArticleID();

Upvotes: 1

Niju
Niju

Reputation: 487

So if you need the page like as after submitting change the action value of form like

<form class="form-signin" method="post"
    action="/articleViewer?varArticleID={yourArticleid}">

Also after submit you need

return Path.Jsp.ARTICLE_VIEWER_PAGE+"?varArticleID="+request.getParameter("varArticleID");

Upvotes: 0

Roman C
Roman C

Reputation: 1

If you submit the action url without parameter, or use hidden field for this purpose then you should return that parameter back as a result. So, you don't get it lost, or redirect to the page where the parameter is not needed anymore. To pass parameter in the url use

<form class="form-signin" method="post" action="/articleViewer?varArticleID=94"> 

Upvotes: 1

Related Questions