2

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?

9
  • This means you have lost one important parameter in the url. Commented Jun 30, 2013 at 13:28
  • Yess After submiting the form I lost paramenter and it shows that page bloank bcz it takes ArticleID as input param Commented Jun 30, 2013 at 13:35
  • Yeah, you didn't post the jsp, impossible to answer. Commented Jun 30, 2013 at 13:44
  • 1
    That's quite expected. Post parameters are sent in the request body, and not in the query string. You should save the comment and then redirect. See en.wikipedia.org/wiki/Post/Redirect/Get Commented Jun 30, 2013 at 13:47
  • Don't post code in comments. Edit your question. Commented Jun 30, 2013 at 13:47

3 Answers 3

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"> 
Sign up to request clarification or add additional context in comments.

2 Comments

By using this I got in URL /articleViewer?varArticleID=79 but its not Calling my Get method code
I mean after this url also my form is Blank no article no comments
1

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

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

Comments

0

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");

7 Comments

My Method i.e saveArticleComment is mapped to this method How should i map it to /articleViewer?articleId=94
after returning this it gives ERror 404
Its shows Error 404 when i use return Path.Jsp.ARTICLE_VIEWER_PAGE+"?varArticleID="+request.getParameter("varArticleID");
When i use only <form class="form-signin" method="post" action="/articleViewer?varArticleID={yourArticleid}"> and URL localhost:8888/articleViewer?varArticleID=79 after posting it blank bcz it is calling post method not get it should call get method with that URL
Ok Iam not strong in spring methods.I think the method for getting the data's for above url should write in GET method where you handling the reuest.
|

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.