jeudi 22 février 2018

changing data on jsp page using Spring MVC

Basically, i need show on 1 jsp page 10 unsorted arrays, and then after clicking on button, sort them and display 10 sorted arrays, so on 1 page have to be 20 arrays(ten sorted and ten unsorted). Hopefully i describe it clear for you. Im using Spring MVC/JSP. Here is my home.jsp page

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
        <title>Home</title>
</head>
<body>

<P>   current arrays: <br>
        <c:forEach items="${list}" var="arr">
       <c:forEach items="${arr}" var="elem">
          ${elem} 
       </c:forEach> <br>
    </c:forEach>
  </P>
  
  <br>
    sorted arrays:
  <form:form action = "sort">
  <input type="submit" value="Sort" />
        <c:forEach items="${sortedList2}" var="arr">
      <c:forEach items="${arr}" var="elem">
          ${elem} 
      </c:forEach> <br>
    </c:forEach>

  </form:form>
</body>
</html>

Here is my controller:

@Controller
public class HomeController {


private AlgorithmModel algo = new AlgorithmModel();

private List<Integer[]> list = algo.getList();

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);


@ModelAttribute("list")
public List<Integer[]> getList() {
return list;
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(ModelMap model) {
return "home";
}

@RequestMapping(value = "/sort", method = RequestMethod.POST)
public String sort(@ModelAttribute("list") ArrayList<Integer[]> list, ModelMap model) {
List<Integer[]> list2 = new ArrayList<Integer[]>(list);
algo.sortList(list2);
model.put("sortedList2", list2);
return "home";
}

}

And finally here is my AlgorithmModel, to perform sorting and generate List:

public class AlgorithmModel {
private Random random = new Random();
private List<Integer[]> list = new ArrayList<Integer[]>();

private List<Integer[]> randomGenerate() {
List<Integer[]> list = new ArrayList<Integer[]>();
int size = 5;
for (int i = 0; i < 10; i++) {
    list.add(generateArr(size));
    size++;
}
return list;
}

private Integer[] generateArr(int n) {
Integer[] arr = new Integer[n];
for (int i = 0; i < arr.length; i++) {
    arr[i] = random.nextInt(101);
}
return arr;
}

public AlgorithmModel() {
list = randomGenerate();
}

public List<Integer[]> getList() {
return list;
}

public void sortList(List<Integer[]> list) {
for (Integer[] integers : list) {
    sort(integers);
}
}

private void sort(Integer[] arr) {
  //some sort algorithm
}
}

So i think it should work like this: -In controller we generate list with 10 random arrays -using @ModelAtribute and pull this list on the page -click button and sort our list inside sort method

but as the result i get: picture

both lists get sorted, but first one shoudnt. How can i make this work? P.S. im new in Spring MVC, and ive wasted 4 hours already, so sry for wasting your time.




Aucun commentaire:

Enregistrer un commentaire