Bootstrap 3模态窗口不适用于spring mvc

时间:2014-01-30 13:02:54

标签: java spring jsp spring-mvc twitter-bootstrap-3

我正在尝试让Modals与我的页面一起工作,我使用spring-mvc 3.2和bootstrap 3.0.3。 单击“添加用户”按钮无效,当我将模态视图设置为显示时,更多功能无法使用关闭按钮关闭。

我该如何解决?

以下是jsp页面代码:

<!doctype html>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
  <head>
    <meta charset="utf-8">
    <titlTest modals</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="<c:url value="/resources/css/bootstrap.css" />" rel="stylesheet"  type="text/css" />

    <script type="text/javascript" src='<c:url value="/resources/js/bootstrap.js" />'></script>
    <script type="text/javascript" src='<c:url value="/resources/js/jquery-1.9.1.js" />'></script>
</head>

<body>
    <div class="container">
        <button class="btn btn-danger btn-mini" data-toggle="modal" data-target="#editUserModal">
            Add user
        </button>
    </div>
    <div class="modal fade" id="editUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <form:form method="post" action="/admin/users/update" commandName="user" role="form">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Modify user</h4>
                    </div>
                    <div class="modal-body">
                        <form:hidden path="id" placeholder="Id" value="${user.id}"/>
                        <div class="form-group">
                            <form:label path="firstName">First Name:</form:label>
                            <form:input path="firstName" class="form-control" placeholder="First Name" value="${user.firstName}"/>
                        </div>
                        <div class="form-group">
                            <form:label path="lastName">Last Name:</form:label>
                            <form:input path="lastName" class="form-control" placeholder="Last Name" value="${user.lastName}"/>
                        </div>
                        <div class="form-group">
                            <form:label path="email">Email:</form:label>
                            <form:input path="email" class="form-control" placeholder="Email" value="${user.email}"/>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div><!-- /.modal-content -->
            </form:form>
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</body>

生成的HTML代码:

<html>
<head>
    <meta charset="utf-8">
    <title>Test modals</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="/resources/css/bootstrap.css" rel="stylesheet"  type="text/css" />

    <script type="text/javascript" src='/resources/js/bootstrap.js'></script>
    <script type="text/javascript" src='/resources/js/jquery-1.9.1.js'></script>
</head>

<body>
    <div class="container">
            <button class="btn btn-danger btn-mini" data-toggle="modal" data-target="#editUserModal">
                Add user
            </button>
    </div>
    <div class="modal fade" id="editUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <form id="user" role="form" action="/admin/users/update" method="post">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Modify user</h4>
                    </div>
                    <div class="modal-body">
                        <input id="id" name="id" placeholder="Id" value="0" type="hidden" value="0"/>
                        <div class="form-group">
                            <label for="firstName">First Name:</label>
                            <input id="firstName" name="firstName" placeholder="First Name" class="form-control" type="text" value=""/>
                        </div>
                        <div class="form-group">
                            <label for="lastName">Last Name:</label>
                            <input id="lastName" name="lastName" placeholder="Last Name" class="form-control" type="text" value=""/>
                        </div>
                        <div class="form-group">
                            <label for="email">Email:</label>
                            <input id="email" name="email" placeholder="Email" class="form-control" type="text" value=""/>
                        </div>
                        <div class="form-group">
                            <label for="password">Password:</label>
                            <input id="password" name="password" placeholder="Password" class="form-control" type="text" value=""/>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Save changes</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div><!-- /.modal-content -->
            </form>
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</body>

3 个答案:

答案 0 :(得分:1)

你需要包含jquery然后引导,因为bootstrap需要jquery才能工作。

所以,切换这两行代码

 <script type="text/javascript" src='<c:url value="/resources/js/jquery-1.9.1.js" />'></script>
 <script type="text/javascript" src='<c:url value="/resources/js/bootstrap.js" />'></script>

答案 1 :(得分:0)

我认为

<form:form method="post" action="/admin/users/update" commandName="user" role="form">

应该在里面

<div class="modal-body">

答案 2 :(得分:0)

Bootstrap.js是使用jquery构建的。所以它必须包含在jquery之后

只需交换两个包含行,它应该像doc一样工作!

<script type="text/javascript" src='<c:url value="/resources/js/jquery-1.9.1.js" />'></script>
<script type="text/javascript" src='<c:url value="/resources/js/bootstrap.js" />'></script>