【发布时间】:2017-10-18 21:40:19
【问题描述】:
大家好,我正在使用 JSP 在 Spring MVC 应用程序中工作,我的问题是在某个时刻我发出一个控制器请求以获取一段 JSP 代码以包含在一个 div 中。我从 ModelAndView 答案中获得 JSP 代码。我用 JQuery 把它放在我的 div 上。但是当我想使用我的 JSP 容器变量时,我做不到。我会展示我的代码。
JSP 容器(父级)
<div class="row" id="principalDiv">
<div id="menuZone">
<c:forEach items="${user.rol.permisos}" var="permisos">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 iconos methodDiv" id="${permisos.permisoMetodoMap}">
<div>
<center>
<i class="fa <c:out value="${permisos.permisoMetodoIcono}" /> fa-5x" id="icon_${permisos.permisoMetodoMap}"></i>
</center>
<br>
<center>
<div>
<c:out value="${permisos.permisoNombre}" />
</div>
</center>
</div>
</div>
</c:forEach>
</div>
<div id="loadZone">
</div>
</div>
我通过一个按钮向我的控制器发出请求,以获取“loadZone”的内容。
代码 JSP 请求
function loadAsp(element) {
$.get('getInclude', {
include : $(element).attr('id')
}, function(responseText) {
$('#loadZone').html(responseText);
});
}
控制器
@Controller
@RequestMapping(value = "/getInclude")
public class IncludesDispatcherController {
@Autowired
FormModelBo boConsultas;
@GetMapping
public ModelAndView getReportesView() {
ModelAndView mav = new ModelAndView("includes/reportes");
try {
mav.addObject("consultas", boConsultas.consultaGeneral());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mav;
}
}
我返回带有模型的视图以制作标题表,可以在上面的 JQuery 片段中看到我使用 html() 方法将视图放入 div 中。
要包含的 JSP(子级)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<div>
<center>
<legend>
<!-- //<img alt="" src="img/pen.png"> -->
Censo Métricas Triage ${user.nombre}<br /> <--- this is parent variable.
</legend>
</center>
</div>
<form action="${user.rol.rolMap}/createExcel" method="POST" role="form" id="">
<div class="form-group">
<!-- -------------------------------------------first section--------------------------- -->
<div class="row" style="padding-bottom: 22px !important;">
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
<div class="form-group">
<div class="form-group ">
<div class="input-group ">
<span class="input-group-addon">Fecha inicio</span> <input type="date" id="fecha_1" name="fecha_1" />
</div>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
<div class="form-group">
<div class="form-group ">
<div class="input-group ">
<span class="input-group-addon">Fecha final</span> <input type="date" id="fecha_2" name="fecha_2" />
</div>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
<div class="form-group">
<div class="input-group">
<button type="submit" class="btn btn-primary">Generar Reporte</button>
</div>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</form>
<div style="padding-bottom: 22px !important;">
<table class="table">
<thead>
<tr>
<th>Nombre de médico</th>
<th>Número de consultas</th>
</tr>
</thead>
<tbody>
<c:forEach items="${consultas}" var="consultasLista">
<tr>
<td><c:out value="${consultasLista[0]} ${consultasLista[1]} ${consultasLista[2]}" /></td>
<td><c:out value="${consultasLista[3]}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
我在 ModelAndView 中发送的模型变量正在工作,但我的父母没有。
我尝试使用其他方法,例如声明变量
<%
String variable = "test";
%>
或
request.setAttribute("attrName", myValue); (for catch it in my children JSP with request.getAttribute())
但也不起作用。我不知道是不是因为 JSP 父代码已经编译了。我需要访问我的父变量。
请帮助我并感谢您的时间。
【问题讨论】:
-
JSP 代码在服务器上运行。我不知道你从哪里得到“要包含的 JSP”,但是如果你通过 ajax-answer 将这段代码发送到浏览器,那么没有人可以做到
<c:forEach或<c:out
标签: javascript java jquery spring jsp