我正在使用Spring3MVC,我有下面的类。
外部系统将使用以下URL调用我的应用程序:
http://somehost/root/param1/param2/param3我有一个spring控制器方法,如下所示:
public ModelAndView showPage(@PathVariable("param1") String paramOne, @PathVariable("param2") String paramTwo, @PathVariable("param3") String paramThree, HttpServletResponse response) {
SomeModel model = new SomeModel(paramOne, paramTwo, paramThree);
return new ModelAndView("SomeJsp", "model", model);
} SomeModel.java
public class SomeModel{
private String paramOne;
private String paramTwo;
private String paramThree;
//constructor
//setters and getters
}SomeJsp.jsp
//In this Jsp i have a div with few elements. Div is not visible by default.
//This jsp has externalJavascript included.
//I enable div and set the data into div elements using jquery.externalJs.js
$(document).ready(function() {
//Here i need the model returned using ModelAndView
//I can get data from model and set into div elements.
});在外部java脚本文件中,是否有可能获得模型内容?
谢谢!
发布于 2014-03-18 14:55:07
在jsp页面中,您可以定义全局js变量并从外部js访问这些变量,如下所示:
jsp文件
<script>
myModel=[];
myModel.paramOne='${model.paramOne}';
myModel.paramTwo='${model.paramTwo}';
myModel.paramThree='${model.paramThree}';
</script>所有服务器端脚本都在服务器端呈现,这些值一旦呈现就会被硬编码。在浏览器中加载页面后,将使用呈现的硬编码值创建全局变量。我的myModel也可以访问外部文件。
编辑:这是someJsp.jsp文件
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE>
<html>
<head>
<script>
myModel=[];
myModel.paramOne='${model.paramOne}';
myModel.paramTwo='${model.paramTwo}';
myModel.paramThree='${model.paramThree}';
</script>
<script src="externalJs.js"></script>
</head>
<body>
<!--content-->
</body>和externalJs.js
$(document).ready(function() {
// myModel will be accessible from here because it's globally declared
alert(myModel);
});https://stackoverflow.com/questions/22480475
复制相似问题