One of the requirements in the project is to display the month and year in two lines:
Java Background Code
To create a demo, use the map List to split the month:
Package demo;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.HashMap;
Import Java.util.Map;
public class Test {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String S1 = "2016-10";
String s2 = "2016-11";
String s3 = "2016-12";
String S4 = "2017-1";
list<string> sList = new arraylist<string> ();
Slist.add (S1);
Slist.add (S2);
Slist.add (S3);
Slist.add (S4);
SYSTEM.OUT.PRINTLN ("Initial month SList:" + sList);
map<string,integer> yearsdic = Getyearsandcount (sList);
System.out.println ("Number of months under each year Yearsdic:" + yearsdic);
Set up year and month collections
list<string> yearlist = new arraylist<string> ();
list<string> monthlist = new arraylist<string> ();
for (int i=0; i< slist.size (); i++)
{
String year = Slist.get (i). Split ("-") [0];
String month = Slist.get (i). Split ("-") [1];
if (!yearlist.contains (year))
{
Yearlist.add (year);
}
Monthlist.add (month);
}
System.out.println ("Year set Yearlist:" + yearlist);
SYSTEM.OUT.PRINTLN ("Month set Monthlist:" + monthlist);
}
/* Returns the year collection, the number of columns per year */
private static map<string, integer> Getyearsandcount (list<string> sList)
{
map<string, integer> yearcount = new hashmap<string, integer> ();
for (int i = 0; i < slist.size (); i++)
{
String year = Slist.get (i). Split ("-") [0];
if (Yearcount.containskey (year))
{
Integer count = Yearcount.get (year);
Yearcount.put (year, count+1);//Add Column
}else {
Yearcount.put (year, 1);//Initial value 1
}
}
return yearcount;
}
}
Console output:
The project is spring4.0 and added to the view:
/**list *@paramPage *@throwsException*/@RequestMapping (Value= "/list") PublicModelandview List (Page page)throwsexception{Modelandview MV= This. Getmodelandview (); Mv.addobject ("Months", monthlist); Mv.addobject ("Yearscount", Yearsdic); Mv.addobject ("Yearlist", yearslist); returnMV; }
Put it in the JSP display:
Use of the Java Map List