我需要你的帮助。我在互联网上尝试了所有我研究的东西,但我不能这样做。
我有一个jsp文件和js文件。我把js文件包含到jsp中。我想在javascript代码上处理spring mvc controller的返回值但是我失败了。
这是我的控制器:
package com.sample.spring.controller;
import com.sample.hibernate.model.employee.Employee;
import com.sample.hibernate.service.employeeservice.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@RequestMapping(value = {"/","/list"},method = RequestMethod.GET)
public ModelAndView lisEmployees(){
List<Employee> employees = employeeService.findAllEmployees();
return new ModelAndView("allemployees","allEmployees",employees);
}
}
这是我的allemployees.jsp文件:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page pageEncoding="UTF-8" language="java" %>
<html>
<head>
<script src="<c:url value="/resources/js/sample.js" />"></script>
<link href="<c:url value="/resources/css/bootstrap.css" />" rel="stylesheet"/>
<link href="<c:url value="/resources/css/bootstrap-theme.css"/>" rel="stylesheet"/>
</head>
<c:import url="/resources/templates/allemployees.html" charEncoding="UTF-8" />
这是我的sample.js文件:
var modelList = "${allEmployees}";
var lengthStr = modelList.length;
console.log(lengthStr);
我在firebug上调试过。我尝试了$ {model},&#34; $ {model}&#34;,$ {allEmployees},&#34; $ {allEmployees}&#34;但是所有这些试验我都失败了。
你能帮我吗?