简体   繁体   中英

Spring MVC - Passing Value from jsp to Controller - HTTP Status 400 - The request sent by the client was syntactically incorrect

If you can help me with this I will accept the best answer and upvote the rest that are correct.

I have been trying to solve this issue for a very long time yet I have no luck.

When I hit the 'Add Current Issue' button, a page with HTTP Status 400 saying 'The request sent by the client was syntactically incorrect.'

What I am trying to do is to pass a value through a form in jsp to the Controller and the controller will process that value and write it into a CSV file, and grab all the data from the CSV file and display it on the webpage.

I have both the add and delete button to add or delete data in my CSV file. The delete function is working properly, yet the add function is not.

Below are my codes:

Controller:

@RequestMapping(value = "/c_Outage", method = RequestMethod.POST)
    public String deleteCurrentInputPage(@RequestParam String CurrentDelete_InputPage, @RequestParam(value="DeleteOn", required = false) String DeleteOn, String date, String type, String system, String description, String commit, Locale locale, HttpServletRequest req, Model model) throws Exception{

        String currentString = "";
        String csvFilename1 = "C:/temp/csv/curdata.csv";
        String eachdatarow = "";

        int entry = 0;
        logger.info("This is commit " + commit, locale);        
        logger.info("DeleteOn is " + DeleteOn, locale);
        logger.info("This is CurrentDelete_InputPage" + CurrentDelete_InputPage, locale);
        if (commit == null ){
            commit = "";
        }
        if (DeleteOn == null){
            DeleteOn = "";
        }

        //logger.info("This is CurrentDelete_InputPage" + CurrentDelete_InputPage, locale);
        /////////////////////////////// Writing Input Begin ///////////////////////////////////////////////
        if(commit.equals("Add Current Issue")){
            logger.info("DeleteOn inside if statement is " + DeleteOn, locale);

        CSVWriter writer1 = new CSVWriter(new FileWriter(csvFilename1, true));

        String [] record1 = {date,type,system,description};

        writer1.writeNext(record1);         

        writer1.close();

        }       else{}

/////////////////////////////// Writing Input End ///////////////////////////////////////////////
        if(DeleteOn.equals("DeleteOn")){
            ArrayList<String> INPUTTOCSV = new ArrayList<String>();
            CSVReader reader1 = new CSVReader(new FileReader(csvFilename1));
            String [] CurnextLineD;
            while ((CurnextLineD = reader1.readNext()) != null) {
                eachdatarow = CurnextLineD[0] + " " + CurnextLineD[1] + " " + CurnextLineD[2] + " " + CurnextLineD[3] ;
                eachdatarow = eachdatarow.replaceAll("\\s+","");
                CurrentDelete_InputPage = CurrentDelete_InputPage.replaceAll("\\s+", "");           
                //logger.info("This is eachdatarow " + eachdatarow, locale);
                //logger.info("This is CurrentDelete_InputPage " + CurrentDelete_InputPage, locale);
                if(eachdatarow.equals(CurrentDelete_InputPage)){
                    //logger.info("Success eachdatarow equals CurrentDelete", locale);
                    continue;
                }           
                    INPUTTOCSV.add(CurnextLineD[0]);
                    INPUTTOCSV.add(CurnextLineD[1]);
                    INPUTTOCSV.add(CurnextLineD[2]);
                    INPUTTOCSV.add(CurnextLineD[3]);

                    entry++;                    
            }
            reader1.close();
            //logger.info("This is entry " + entry , locale);
            //logger.info("This is INPUTTOCSV " + INPUTTOCSV, locale);

            int entrytimes4 = entry * 4;
            if ( entrytimes4 == 0){
                //logger.info("this is entrytimes4 " + entrytimes4, locale);
                CSVWriter writer1 = new CSVWriter(new FileWriter(csvFilename1, false));     
                writer1.writeNext(null);
                writer1.close();
            }


            for(int i = 0; i < entrytimes4; i+=4){          
                String[] individual = {INPUTTOCSV.get(i), INPUTTOCSV.get(i+1), INPUTTOCSV.get(i+2), INPUTTOCSV.get(i+3)};
                //logger.info("This is individual " + individual, locale);
                if(i == 0){
                  CSVWriter writer1 = new CSVWriter(new FileWriter(csvFilename1, false));
                  writer1.writeNext(individual);     
                  writer1.close();
                }
                else{
                  CSVWriter writer1 = new CSVWriter(new FileWriter(csvFilename1, true));
                  writer1.writeNext(individual);     
                  writer1.close();
                }        
            }

}else{

        }



            CSVReader reader3 = new CSVReader(new FileReader(csvFilename1));     

            while ((CurnextLine_InputPage = reader3.readNext()) != null) {
                if (CurnextLine_InputPage[0] != null){
                  currentString += CurnextLine_InputPage[0] + "\t" + CurnextLine_InputPage[1] + "\t" + CurnextLine_InputPage[2] + "\t" + CurnextLine_InputPage[3] + "\t" + "|"; //appending each data each time the while loop loops
                }
            }
            model.addAttribute("currentString", currentString); //This is the long appended string which is to be split in the jsp page by the delimiter '|'
            reader3.close();



        return "currentO";
    }

currentO.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page session="true" %>
<html>
<head>
    <title>Home</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <!-- Calender Style Sheet Begin -->
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script>
    $(function() {
        for (i = 0; i < 100000; i++) {
            $( "#datepicker"+i ).datepicker({ dateFormat: "mm/dd/yy", firstDay: 1, changeYear: true });
        }
    });
    </script>
</head>
<body>
<%-- <form:form method="post" action="" commandName="currentOutage"> --%>
<form:form method="post"  modelAttribute="currentOutage">
<tr><td><h2>Input Current Outage</h2></td><td></td></tr>
<tr><td>&nbsp;</td><td></td></tr>
  <tr><td>Date: </td><td><input type="text" name="date" id="datepicker1" style="width: 80px;"></td></tr>
  <tr><td>Type: </td><td>
    <select name="type">
        <option value="">Select...</option> 
        <option value="Planned">Planned</option>
        <option value="Unplanned">Unplanned</option>
        <option value="Emergency">Emergency</option>  
    </select>
  </td></tr>
  <tr><td>System: </td><td><input type="text" name="system" ></td></tr>
  <tr><td>Description: </td><td><input type="text" name="description" style="width: 250px;" ></td></tr>
  <p class="submit"><input type="submit" name="commit" value="Add Current Issue"></p>

</form:form>

<h2>Current Outages</h2>
<table>
<h3><td> Date </td> <td> Type </td> <td> System </td> <td> Description </td></h3>
<!-- <tr><td>   ${ curdate }   </td><td>   ${ curtype }   </td><td>   ${ cursys }   </td><td>   ${ curdes }   </td></tr>-->

 <c:set var="currentinput_InputPage" value="${currentString}" />
  <c:forEach var="splitcurrentinput_InputPage" items="${fn:split(currentinput_InputPage, '|')}" >
      <tr><td><c:out value="${splitcurrentinput_InputPage}" /><tr><td>      
      <c:if test="${not empty splitcurrentinput_InputPage}">
        <form:form method="post"  modelAttribute="currentOutageDelete_InputPage" name="form1_InputPage">    
            <input type="hidden" value="${splitcurrentinput_InputPage}" name="CurrentDelete_InputPage">
            <input type="hidden" value="DeleteOn" name = "DeleteOn">
            <input type="submit" value="Delete" />
        </form:form>      
      </c:if>      
  </c:forEach> 
</table>

</body>
</html>

The Maven dependency for CSV file:

<dependency>
    <groupId>net.sf.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>2.3</version>
</dependency>

Specifically I want the:

String [] record1 = {date,type,system,description};

to contain the field values date, type, system, and description from the jsp.

To pass all request parameters to your Controller's method use following:

@RequestParam Map<String,String> params

as a method parameter. Then, in that method you can simply get each parameter with this code (eg):

String onDelete = params.get("OnDelete"); //or any other parameter name as argument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM