0

I have many submit buttons in my plain HTML . The one not working is as below:- the other are as same as below

< form:submit cssClass="action-button" name="excelBTNX" value="Excel" id="excelBTNX"  /> 

The function of the above button in the controller is to create a excel sheet and put in session(I can download it from cookies ) and returns back .

The defination of the corrosponding method in Controller is as same as for other buttons which are working fine .

The problem with this is ,it works only at even count hit .When I click for the first time the page gets refreshed . When I click for the second time , control passes to the controller and my excel comes up as cookies.

I tried to track whether the submit is working or not with javaScript code as

     $(‘form’).submit(function(){
     alert("event getting fired");
});

and it gives the alert for both the cases.

I have done the validation part from the controller(manually), so local inbuilt validators are not used . So I believe they are not the case.

How do I fix it ?

Controller codes:-

 @RequestMapping(value = "execute.action", method = RequestMethod.POST, params = "excelBTNX")
public String excelOut(HttpServletRequest request, HttpServletResponse response,
        @ModelAttribute("mymodel") myModel model, BindingResult bindingResult, ModelMap modelmap) {

    scr14(request).initializeSomeCalculation(model);// some innercalss called to manupulate model
    HttpSession session = request.getSession(false);

    if(1=1){//CRUD condition here true in READ mode.
        model= new myModel ();
    }


    byte[] excel = createExcelS14(model,request);
    String fileName = getExcelName() + ".xls";
    String filepath = myFrameWorkUtils.createTempFile(excel, fileName);
    if (session != null) {
        session.setAttribute(fileDownload, filepath);
    }
  scr14(request).initializeSomeCalculation(model);
    model.setDate(somedate);
    return "myPanel";}

2
  • I would first see whether this issue is related to your Excel processing or whether it is something with your Controller. Just comment out the Excel functionality in the Controller and just forward to a new page if the Controller method for the button is Please post the Controller code the is invoked with your button.
    – MarcFasel
    Jan 3, 2013 at 4:15
  • Ok ! I will try commenting the codes @MarcFasel , and post my code.
    – Narayan
    Jan 3, 2013 at 4:32

1 Answer 1

0

Here are some steps:

  • Check whether this issue is related to your Excel processing or whether it is something with your Controller. I assume you have something like
@RequestMapping(..., params = "excelBTNX")
public ModelAndView next(...) 
{ <EXCEL FUNCTIONALITY> }

Just comment out the in the Controller and verify that the method is called every time. Please test this a let us know whether this is working.

  • What happens that makes you think the Controller is only called at the second click? Maybe the signs that you are looking at don't really mean that the controller is only called every second click. Please explain.

  • Fix if (1=1) code. = in Java is the assignment operator, == is the comparison operator. I assume you want to do a comparison. It also seems like you simplified this part of the code, but it may actually be the problem. Please post the actual code here.

  • I don't see anything about cookies here. It looks to me like you are creating a temporary Excel file, and setting the name of the file in the session.

session.setAttribute(fileDownload, filepath) cannot work, since the key of the session attribute map is of type String. It should probably be session.setAttribute("fileDownload", filepath).

  • Can you see whether there is a new temp Excel file generated with each click? You should be able to tell by the timestamp.

This may still not point to the problem, but it will certainly get us closer.

8
  • You r right about the model defination and the implementation. I need excel in cookies because I modify my model in controller and use the data of the model to create excel sheet . I have edited the Question with controller codes.
    – Narayan
    Jan 3, 2013 at 4:40
  • Yes! You are right about almost everything in your answer !Thanks . I am much sure the control is not passing to the controller : by debugging the code and commenting it. And no excel is being created for the first click , I checked it by the timestamp. I have come up to a hint by now :- The button gets focus at first click and works from the second .just a wild guess.
    – Narayan
    Jan 4, 2013 at 6:20
  • If your JavaScript code fires every time then your Controller method should be called every time. It is also odd that other buttons work fine, but this one behaves differently. I would try to isolate now if this is a front-end/ JavaScript issue or a back-end/Spring-MVC issue that there is a discrepancy between the registered clicks in the front-end and the calls to the controller. Good luck!
    – MarcFasel
    Jan 4, 2013 at 7:31
  • @Narayan Can you please verify whether this is the solution and accept answer if that is the case? thx.
    – MarcFasel
    Jan 6, 2013 at 23:20
  • The isolation of the problem from JavaScript spring MVC has extended itslef to HTML.Let me state some of my test here 1> I removed all the screens other components except Submit buttons . the button worked fine . I checked the html it seems ok . 2> reverted the changes of 1 . The code is not going to the controller 3> created another hidden submit button and redirected the its click() function with the click event of excelBTN in java script , still the page gets to the controller only once . Checked it with the debugger .
    – Narayan
    Jan 7, 2013 at 4:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.