Vous êtes sur la page 1sur 4

how to go to next page only after clicking submit button in

java using spring


3down
vote
1

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

Iam using session.getAttribute to do the above process. I am thinking if there is


any altrnative for this, as I need to do this for every post method...
The below method is for first page, where I created a session attribute which is
used in the next page.
@RequestMapping(value = "/payment", method = RequestMethod.POST)
public String submitForPayment(@ModelAttribute("deposit") Deposit deposit,
ModelMap model, HttpServletRequest request) throws IOException
{
try {
HttpSession sessionForNextPage = request.getSession(true);
sessionForNextPage.setAttribute("vNumber",
deposit.getValidityNumber());
return "redirect:success";

} catch (NullPointerException exception) {


return "redirect:payment";
}

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.

Spring Form Submit using Java script


0down
vote

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>

How does autowiring work in Spring?


http://stackoverflow.com/questions/3153546/how-does-autowiring-work-in-spring?
rq=1

Submit form without refreshing (SPRING MVC 3)


http://stackoverflow.com/questions/11037399/submit-form-without-refreshingspring-mvc-3?rq=1

Spring MVC - Form submit without binding object


http://stackoverflow.com/questions/14375085/spring-mvc-form-submit-withoutbinding-object?rq=1

Objects binding in Spring MVC form


http://stackoverflow.com/questions/10138715/objects-binding-in-spring-mvc-form?
rq=1

Spring MVC SessionAttributes with ModelAttribute usage


http://stackoverflow.com/questions/16373109/spring-mvc-sessionattributes-withmodelattribute-usage?rq=1

Spring MVC @ModelAttribute @SessionAttributes - Why


does a model attribute need a @ModelAttribute annotated
method?
http://stackoverflow.com/questions/19890198/spring-mvc-modelattributesessionattributes-why-does-a-model-attribute-need?rq=1

Spring @ModelAttribute and @SessionAttribute behaviour


http://stackoverflow.com/questions/16133498/spring-modelattribute-andsessionattribute-behaviour?rq=1

Spring MVC form not backed by a model object


http://stackoverflow.com/questions/26852509/spring-mvc-form-not-backed-by-amodel-object?rq=1

Spring-MVC form validation with MultiActionController


http://stackoverflow.com/questions/7261478/spring-mvc-form-validation-withmultiactioncontroller?rq=1

how to pass a java object from jsp to spring controller on


clicking a link
http://stackoverflow.com/questions/27462811/how-to-pass-a-java-object-from-jsp-tospring-controller-on-clicking-a-link?rq=1

Spring MVC submit only one element of render List in


page
http://stackoverflow.com/questions/23953863/spring-mvc-submit-only-one-elementof-render-list-in-page?rq=1

Vous aimerez peut-être aussi