如何在我的主视图Spring MVC中包含jsp视图?

时间:2017-10-19 16:40:17

标签: javascript java spring jsp spring-security

大家好昨天我问了一些关于Spring MVC的问题,关于从jsp注入JavaScript的jsp孩子中读取变量的可能性。

现在,我不知道如何制作这样的流程,我正在开发一个网络应用程序,下一个流程。

使用安全性登录,根据白皮书重新定向到用户

  protected String determineTargetUrl(Authentication authentication) {
      boolean isUser = false;
      boolean isAdmin = false;
      boolean isSupervisor = false;

      Collection<? extends GrantedAuthority> authorities
       = authentication.getAuthorities();
      for (GrantedAuthority grantedAuthority : authorities) {
          if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
              isUser = true;
              break;
          } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
              isAdmin = true;
              break;
          } else if (grantedAuthority.getAuthority().equals("ROLE_SUPERVISOR")) {
              isSupervisor = true;
              break;
          }
      }

      if (isUser) {
          return "/user";
      } else if (isAdmin) {
          return "/admin";
      } else if (isSupervisor) {
          return "/supervisor";
      } else {
          throw new IllegalStateException();
      }
  }

我会专注于我的管理员用户(主管有相同的主页,用户有一个特殊的页面)

控制器 我的控制器是我的视图,并使用ModelAndView发送一些值。

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    FormModelBo boFormModel;
    @Autowired
    UserBo boUser;
    @Autowired
    FormModelBo bo;
    @Autowired
    RolBo boRol;

@GetMapping
    public ModelAndView showRegistrationForm() {
        ModelAndView mav = new ModelAndView("adminDashboard");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            System.out.println(currentUserName);
            try {
                User user_complete = boUser.consultaIndividual(currentUserName);
                mav.addObject("user",user_complete);
                mav.addObject("permisos", boRol.consultaIndividual(user_complete.getRol().getRolId()));
                mav.addObject("version", VersionReader.getVersion());                   
            } catch (SQLException e) {
                e.printStackTrace();
            } 
        }else{
            System.out.println("No user");
        } 
        return mav;
    }
}

JSP adminDashboard

通过用户信息,我获得了rol信息,并且每个rol都有模块,并打印在div&#34; menuZone&#34;所有元素都有一个onclick In Jquery。

        <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>

JQuery的

它使控制器请求将视图部分包含在loadZone div中。

$(document).ready(function() {
    $('.methodDiv').click(function(event) {
        loadAsp(this);
        $('#menuZone').hide(1000);
        $('#menu').addClass("fa fa-arrow-circle-left fa-3x");

    });

    $('#menu').click(function(event) {
        $("#loadZone").hide(1000);
        $("#menuZone").show(1000);
    })

    function loadAsp(element) {
        $.get('getInclude', {
            include : $(element).attr('id')
        }, function(responseText) {
            //alert(responseText);
            $('#loadZone').html(responseText);
            $("#loadZone").show(1000);
        });
    }

});

调度视图的控制器

@Controller
@RequestMapping(value = "/getInclude")
public class IncludesDispatcherController {

    @Autowired
    FormModelBo boConsultas;
    @GetMapping
    public ModelAndView giveIncludeByName(@RequestParam(value = "include", required = false) String include) {

        ModelAndView mav;

            if(include.equals("reportes")){
                System.out.println(include);        
                return getReportesView();
            }else{
                System.out.println("not "+include);
                return null;
            }
    }

    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;

    }

包含在loadZone div中的视图

<%@ 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 <br />  --%>
                </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  ${myLogged}</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>

在html()时刻加载的模型变量(&#34; ${consultas}&#34;)效果很好,但我无法访问我父母的变量(&#34; {$user}&# 34)。我知道JSP是服务器lenguaje。

所以我不知道如何做这个过程来保持它现在的灵活性。我不能分别加载视图和数据信息,首先执行一个控制器请求,另一个加载数据,然后用JQuery填充我的jsp。但是对于菜单中的所有新选项,我需要添加用于获取和填充信息的代码。

如何以良好的方式完成此过程?

0 个答案:

没有答案