2017-10-18 58 views
0

大家好我正在使用JSP的Spring MVC应用程序中工作,我的问题是在某个时刻,我发出一个控制器请求来获取一个JSP代码以包含在div中。我从ModelAndView答案中获取JSP代码。我把它放在JQuery的div上。但是当我想使用我的JSP容器变量时,我无法做到这一点。我会显示我的代码。如何在JSP中使用父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> 

白衣一个按钮,我做了一个reques到我的控制器,以获取 “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&eacute;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正在发送模型变量,但我的父母没有。

我尝试用其他方法,如申报variblaes

<% 
String variable = "test"; 
%> 

request.setAttribute("attrName", myValue); (for catch it in my children JSP with request.getAttribute()) 

但没有也可以。我不知道是否会导致JSP父代码被编译。我需要访问我的父变量。

请帮助我,谢谢你的时间。

+0

JSP代码运行在服务器上。我不知道你从哪里得到'JSP来包含',但是如果你通过ajax-answer向浏览器发送这段代码,没有人可以做' Holger

回答

0

如果您需要变量IncludesDispatcherController $ {user.nombre}您需要将其注入到此控制器,然后注入视图,否则它将为空。

无论如何,就像有人已经提到JSP是服务器端语言,并且您与javascript和Ajax的交互并不十分完美。如果你不直接发送HTML,而是例如只使用JSON发送JavaScript所需的数据,那么它会更好。

之后,它会好得多,如果您使用的数据,你已经在你的父 - “$ {} user.rol.permisos”。你可以把它放在“data-”标签中,然后通过JQuery轻松阅读。在你的循环,你可以做这样的事情:

<a href="#" id="icon_1" data-nombre="${permisos.permisoNombre}"> 

而且这个阅读:

$("a#icon_1").data("nombre");