如何使用Spring MVC使jQuery函数可执行?

时间:2015-07-30 10:47:34

标签: javascript jquery html ajax spring-mvc

我有一个Spring项目,它使用jQuery函数从一个区间获取值,但它不起作用。我是jQuery和Ajax的新手,我不知道如何修复它。

这是我的Controller类:

@Controller
public class ChooseForm {

    @RequestMapping(value = "/choose", method = RequestMethod.GET)
    public ModelAndView selectValue() {

        try{

            File file = new File("C:/Program Files/Apache Software Foundation/Tomcat 8.0/tmpFiles/Canon.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Canon.class);      
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Canon canon = (Canon) jaxbUnmarshaller.unmarshal(file);

            List<Table> table = canon.getTable();

            for(Table tables: table){

                List<String> row = tables.getRow();

                List<String> first = new ArrayList<String>();
                List<String> second = new ArrayList<String>();
                List<String> third = new ArrayList<String>();
                List<String> fourth = new ArrayList<String>();
                List<String> fifth = new ArrayList<String>();
                List<String> sixth = new ArrayList<String>();
                List<String> seventh = new ArrayList<String>();
                List<String> eight = new ArrayList<String>();
                List<String> ninth = new ArrayList<String>();
                List<String> tenth = new ArrayList<String>();

                String values = null;
                String values2 = null;

                String value1 = null;
                String value2 = null;
                String value3 = null;
                String value4 = null;
                String value5 = null;
                String value6 = null;
                String value7 = null;
                String value8 = null;
                String value9 = null;
                String value10 = null;

                String value11 = null;
                String value22 = null;
                String value33 = null;
                String value44 = null;
                String value55 = null;
                String value66 = null;
                String value77 = null;
                String value88 = null;
                String value99 = null;
                String value1010 = null;

                List<String> rowss = row.subList(0, 12600);
                for(int i=0; i<rowss.size(); i++){
                    values = row.get(i);

                    String[] parts = values.split(" +");

                    String part0 = parts[1];     
                    String part1 = parts[2];
                    String part2 = parts[3];
                    String part3 = parts[4];
                    String part4 = parts[5];
                    String part5 = parts[6];
                    String part6 = parts[7];
                    String part8 = parts[8];
                    String part9 = parts[9];
                    String part10 = parts[10];

                    value1 = parts[1];
                    value2 = parts[2];
                    value3 = parts[3];
                    value4 = parts[4];
                    value5 = parts[5];
                    value6 = parts[6];
                    value7 = parts[7];
                    value8 = parts[8];
                    value9 = parts[9];
                    value10 = parts[10];

                    first.add(value1);
                    second.add(value2);
                    third.add(value3);
                    fourth.add(value4);
                    fifth.add(value5);
                    sixth.add(value6);
                    seventh.add(value7);
                    eight.add(value8);
                    ninth.add(value9);
                    tenth.add(value10);
                }   

                List<String> row1 = tables.getRow();
                List<String> rows = row1.subList(12600, row1.size());
                for(int j=0; j<rows.size(); j++){
                    values2 = rows.get(j);

                    String[] parts2 = values2.split(" +");

                    String part11 = parts2[0];   
                    String part22 = parts2[1];
                    String part33 = parts2[2];
                    String part44 = parts2[3];
                    String part55 = parts2[4];
                    String part66 = parts2[5];
                    String part77 = parts2[6];
                    String part88 = parts2[7];
                    String part99 = parts2[8];
                    String part1010 = parts2[9];

                    value11 = parts2[0];
                    value22 = parts2[1];
                    value33 = parts2[2];
                    value44 = parts2[3];
                    value55 = parts2[4];
                    value66 = parts2[5];
                    value77 = parts2[6];
                    value88 = parts2[7];
                    value99 = parts2[8];
                    value1010 = parts2[9];


                    first.add(value11);
                    second.add(value22);
                    third.add(value33);
                    fourth.add(value44);
                    fifth.add(value55);
                    sixth.add(value66);
                    seventh.add(value77);
                    eight.add(value88);
                    ninth.add(value99);
                    tenth.add(value1010);
                }

                ModelAndView model = new ModelAndView("chooseForm");                    

                model.addObject("first", first);
                model.addObject("second", second);
                model.addObject("third", third);
                model.addObject("fourth", fourth);
                model.addObject("fifth", fifth);
                model.addObject("sixth", sixth);
                model.addObject("seventh", seventh);
                model.addObject("eight", eight);
                model.addObject("ninth", ninth);
                model.addObject("tenth", tenth);

                return model;

            }

        }

        catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我的jsp函数的jsp文件是:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<html>
<head>
<title>Filter Form</title>
</head>
<body> 
<center>
<form>
<p>First value:</p>
<form:label path="first" id="firstvalue" />
<input type="text" id="first" />

<br/>Last value:
<br/>

<form:label path="first" id="lastvalue" />
<input type="text" id="second" />

<br/>

<input type="submit" id="btnSubmit" value="Submit" />
</form>
</center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
$(document).ready(function () {
$('#btnSubmit').on("click", function () { 
    var array = ${first};
    var newArray = [];
    var firstVal = $('#first').val();
    var lastVal = $('#second').val();

    for (var i=0; i<array.length;i++) {
        if (array[i] >= firstVal && array[i] <= lastVal) { 
            newArray.push(array[i]); // Add to new array            
        }
    }   

    // loop through new array and display values
    for (var i=0; i<newArray.length;i++) {        
        alert(newArray[i]);
    }
});
})
</script>
</body>
</html>

在这里,我想仅从第一个List<String>获取值{$ first},但是当我输入值并单击提交按钮时,它什么都没有给我。你能帮帮我吗?

0 个答案:

没有答案