Am trying to design a side menu in jsp home page using json file with spring mvc below is my three files, jsp page contains a code that the way which i am looking to design side menu the json file contains menu and sub menu data, in the spring controller i read json file and validate the user after validation i have send the data to jsp which i am getting from json file and make its menu and sub menu two different menu as r1 and r2
here what i am facing the problem is how to json data from spring controller to jsp and make it as menu and sub menu
user
adduser
deleteuser
updateuser
job
add job
delete job
update job
jsp
<li>
<a href="#"><!-- <i class="fa fa-wrench fa-fw"></i> -->User<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="javascript:void(0)">add user</a>
</li>
<li>
<a href="javascript:void(0)">delete user</a>
</li>
<li>
<a href="javascript:void(0)">update user</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
<li>
<a href="#"><!-- <i class="fa fa-wrench fa-fw"></i> -->job<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="javascript:void(0)">add job</a>
</li>
<li>
<a href="javascript:void(0)">delete job</a>
</li>
<li>
<a href="javascript:void(0)">update job</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
Json file
{
"userid": "abc",
"roles": ["admin"],
"sidebar":
{
"job": ["addjob", "updatejob", "deletejob"]
,
"user": ["adduaser", "deleteuser", "updateuser"]
}
Controller
@RequestMapping(value = "/authenticateLogin", method = RequestMethod.POST)
public String loginAuthentication(HttpServletRequest request) {
String username=request.getParameter("userName");
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("d:\\test.json"));
JSONObject jsonObject = (JSONObject) obj;
String userID1 = (String) jsonObject.get("userid");
if (username.equals(userID1)) {
JSONArray rolesArray = (JSONArray) jsonObject.get("roles");
String role = (String) rolesArray.get(0);
if (role.equals("admin")) {
JSONObject sideContent = (JSONObject) jsonObject.get("sidebar");
JSONArray job = (JSONArray) sideContent.get("job");
JSONArray user = (JSONArray) sideContent.get("user");
ArrayList<String> jobArrayList = new ArrayList<String>();
ArrayList<String> userArrayList = new ArrayList<String>();
Iterator<String> iterator1 = job.iterator();
while (iterator1.hasNext()) {
jobArrayList.add(iterator1.next());
}
Iterator<String> iterator2 = user.iterator();
while (iterator2.hasNext()) {
userArrayList.add(iterator2.next());
}
return "";
}
if(role.equals("user"))
{
JSONObject sideContent = (JSONObject) jsonObject.get("sidebar");
JSONArray job = (JSONArray) sideContent.get("job");
ArrayList<String> jobArrayList = new ArrayList<String>();
Iterator<String> iterator1 = job.iterator();
while (iterator1.hasNext()) {
jobArrayList.add(iterator1.next());
}
return "";
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return "Error";
}