我试图使用JSON文件,下面的Spring MVC是我的三个文件来设计JSP主页的侧边菜单中,JSP页面包含的,是我在找边设计菜单的方式,JSON文件包含菜单和子菜单数据的代码,在春天控制器我读JSON文件和验证验证后的用户,我有数据发送到JSP对此我从JSON文件中获取,并使其菜单和子菜单中有两个不同的菜单,r1和r2
在这里我所面临的问题是如何从春季控制器JSP JSON数据并使其为菜单和子菜单
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文件
{
"userid": "abc",
"roles": ["admin"],
"sidebar":
{
"job": ["addjob", "updatejob", "deletejob"]
,
"user": ["adduaser", "deleteuser", "updateuser"]
}
调节器
@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";
}