Projects Practice Steps
Projects Practice Steps
I need to redirect to a page only after clicking the submit button, but not directly
typing the url in the address bar. If it was directly typed in address bar, then it
should display the home page.
favorite
The method below is for the next page where the session declared above is used.
@RequestMapping(value = "/success", method = RequestMethod.GET)
public String showSuccess(ModelMap model, HttpServletRequest request)
{
try {
view = "success";
HttpSession session = request.getSession(false);
int vNumber = (int) session.getAttribute("vNumber");
System.out.println(vNumber);
if (vNumber != 0) {
request.getSession(false).removeAttribute("vNumber");
return view;
}
else
return "pay";
} catch (Exception e) {
}
return "redirect:pay";
Is there any other way to do this, as I have to do this for all the methods...
Whenever i have to do the post submit , I always use a intermediate page to capture POST
data and store the data in database and store the record key in the session and redirect it to
the display page where i check for the record id if its there then i retrieve data from DB and
display it if not display a error msg.
So even if somebody access your display page directly (Typed in url) it will display a error
msg , And in most cases people will not see the intermediate page url but even if they do
you can use random token for the your html FORM and store in session and validate it on
intermediate page.
favorite
I am trying to submit a spring form based on confirm condition using java script.
following is my Controller
@RequestMapping("/loadPage.do")
public ModelAndView loadPage()
{
ModelAndView modelAndView;
//some code
return modelAndView;
}
@RequestMapping(value="/submitAction.do" , method=RequestMethod.POST)
public String submitForm(@ModelAttribute Object form, Model m ){
//some code
return "page";
}
JSP
<script>function confirmForChanges (){
var r= confirm("Do you want to proceed");
if (r == true) {
document.getElementById('submitButton').action = "/root/submitAction.do";
document.getElementById('submitButton').submit();
alert("Your changes have been saved");
}if (r ==false){
alert("changes not saved")
}
}</script>
<form:form action="/submitAction.do" commandName="command"
method="post">
<input id=cancelButton type="button" value="Cancel" />
<input id=submitButton type="submit" value="Submit"
onclick="javascript:confirmForChanges();"/>;
</form:form>
The problem is even if i do a cancel, the form gets submitted :( I tried removing
the action="/submitAction.do" from the form in jsp but no luck.
Try using the event onsubmit="return confirmForChanges();"
function confirmForChanges() {
var r = confirm("Do you want to proceed");
if (r) {
alert("Your changes have been saved");
} else {
alert("changes not saved")
}
return r;//if true then submit else don't submit
}
<form:form action="/submitAction.do" commandName="command" method="post"
onsubmit="return confirmForChanges();">
<input id=cancelButton type="button" value="Cancel" />
<input id=submitButton type="submit" value="Submit" />
</form:form>