2012-07-25 26 views
0

在我的web應用程序中,我使用Spring MVC。我有彈簧MVC使用的默認登錄頁面。在我的登錄頁面中,我需要一個複選框。登錄時,用戶必須啓用複選框,然後單擊「登錄」按鈕。
如果他沒有選中'複選框',應該顯示一個彈出窗口(帶有一些錯誤信息)。春天 - 登錄頁面有條款和條件頁面(使用Javascript)

我想實現這個使用JavaScript。

任何人都可以請告訴如何實現這個?

這是我的JSPX文件。對不起是大

<div xmlns:spring="http://www.springframework.org/tags" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
    <jsp:directive.page contentType="text/html;charset=UTF-8" /> 
    <jsp:output omit-xml-declaration="yes" /> 
    <spring:message code="security_login_title" var="title" htmlEscape="false" /> 
    <util:panel id="title" title="${title}"> 
    <c:if test="${not empty param.login_error}"> 
     <div class="errors"> 
     <p> 
      <spring:message code="security_login_unsuccessful" /> 
      <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /> 
      . 
     </p> 
     </div> 
    </c:if> 
    <c:if test="${empty param.login_error}"> 
     <p> 
     <spring:message code="security_login_message" /> 
     </p> 
    </c:if> 
    <spring:url value="/resources/j_spring_security_check" var="form_url" /> 
    <form name="f" action="${fn:escapeXml(form_url)}" method="POST"> 
     <div> 
     <label for="j_username"> 
      <spring:message code="security_login_form_name" /> 
     </label> 
     <input id="j_username" type='text' name='j_username' style="width:150px" /> 
     <spring:message code="security_login_form_name_message" var="name_msg" htmlEscape="false" /> 
     <script type="text/javascript"> 
      <c:set var="sec_name_msg"> 
      <spring:escapeBody javaScriptEscape="true">${name_msg}</spring:escapeBody> 
      </c:set> 
      Spring.addDecoration(new Spring.ElementDecoration({elementId : "j_username", widgetType : "dijit.form.ValidationTextBox", widgetAttrs : {promptMessage: "${sec_name_msg}", required : true}})); 
     </script> 
     </div> 
     <br /> 
     <div> 
     <label for="j_password"> 
      <spring:message code="security_login_form_password" /> 
     </label> 
     <input id="j_password" type='password' name='j_password' style="width:150px" /> 
     <spring:message code="security_login_form_password_message" var="pwd_msg" htmlEscape="false" /> 
     <script type="text/javascript"> 
      <c:set var="sec_pwd_msg"> 
      <spring:escapeBody javaScriptEscape="true">${pwd_msg}</spring:escapeBody> 
      </c:set> 
      Spring.addDecoration(new Spring.ElementDecoration({elementId : "j_password", widgetType : "dijit.form.ValidationTextBox", widgetAttrs : {promptMessage: "${sec_pwd_msg}", required : true}})); 
     </script> 
     </div> 
     <br /> 
     <div class="submit"> 
     <script type="text/javascript">Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));</script> 
     <spring:message code="button_submit" var="submit_label" htmlEscape="false" /> 
     <input id="proceed" type="submit" value="${fn:escapeXml(submit_label)}" /> 
     <spring:message code="button_reset" var="reset_label" htmlEscape="false" /> 
     <input id="reset" type="reset" value="${fn:escapeXml(reset_label)}" /> 
     </div> 
    </form> 
    </util:panel> 
</div> 

我有以下input.tagx文件

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
    <jsp:output omit-xml-declaration="yes" /> 

    <jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)" /> 
    <jsp:directive.attribute name="field" type="java.lang.String" required="true" rtexprvalue="true" description="The field exposed from the form backing object" /> 
    <jsp:directive.attribute name="label" type="java.lang.String" required="false" rtexprvalue="true" description="The label used for this field, will default to a message bundle if not supplied" /> 
    <jsp:directive.attribute name="labelCode" type="java.lang.String" required="false" rtexprvalue="true" description="Key for label message bundle if label is not supplied" /> 
    <jsp:directive.attribute name="required" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicates if this field is required (default false)" /> 
    <jsp:directive.attribute name="disabled" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Specify if this field should be enabled" /> 
    <jsp:directive.attribute name="validationRegex" type="java.lang.String" required="false" rtexprvalue="true" description="Specify regular expression to be used for the validation of the input contents" /> 
    <jsp:directive.attribute name="validationMessageCode" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the message (message property code) to be displayed if the regular expression validation fails" /> 
    <jsp:directive.attribute name="validationMessage" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the message to be displayed if the regular expression validation fails" /> 
    <jsp:directive.attribute name="min" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the minimum length of the input contents" /> 
    <jsp:directive.attribute name="max" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the maximum length of the input contents" /> 
    <jsp:directive.attribute name="decimalMin" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the minimum size of the input contents" /> 
    <jsp:directive.attribute name="decimalMax" type="java.lang.String" required="false" rtexprvalue="true" description="Specify the maximum size of the input contents" /> 
    <jsp:directive.attribute name="disableFormBinding" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Set to true to disable Spring form binding" /> 
    <jsp:directive.attribute name="type" type="java.lang.String" required="false" rtexprvalue="true" description="Set field type (default 'text', or 'password')" /> 
    <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" /> 
    <jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)" /> 

    <c:if test="${empty render or render}"> 

    <c:if test="${empty type}"> 
     <c:set value="text" var="type" /> 
    </c:if> 

    <c:if test="${empty disabled}"> 
     <c:set value="false" var="disabled" /> 
    </c:if> 

    <c:if test="${empty label}"> 
     <c:if test="${empty labelCode}"> 
     <c:set var="labelCode" value="${fn:substringAfter(id,'_')}" /> 
     </c:if> 
     <spring:message code="label_${fn:toLowerCase(labelCode)}" var="label" htmlEscape="false" /> 
    </c:if> 

    <c:if test="${empty validationMessage}"> 
     <c:choose> 
     <c:when test="${empty validationMessageCode}"> 
      <spring:message arguments="${fn:escapeXml(label)}" code="field_invalid" var="field_invalid" htmlEscape="false" /> 
     </c:when> 
     <c:otherwise> 
      <spring:message arguments="${fn:escapeXml(label)}" code="${validationMessageCode}" var="field_invalid" htmlEscape="false" /> 
     </c:otherwise> 
     </c:choose> 
    </c:if> 

    <c:if test="${empty required}"> 
     <c:set value="false" var="required" /> 
    </c:if> 

    <c:set var="sec_field"> 
     <spring:escapeBody javaScriptEscape="true" >${field}</spring:escapeBody> 
    </c:set> 

    <div id="_${fn:escapeXml(id)}_id"> 
     <label for="_${sec_field}_id"> 
     <c:out value="${fn:escapeXml(label)}" /> 
     : 
     </label> 
     <c:choose> 
     <c:when test="${disableFormBinding}"> 
      <input id="_${sec_field}_id" name="${sec_field}" type="${fn:escapeXml(type)}" /> 
     </c:when> 
     <c:otherwise> 
      <c:choose> 
      <c:when test="${type eq 'password'}"> 
       <form:password id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" /> 
      </c:when> 
      <c:otherwise> 
       <form:input id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" /> 
      </c:otherwise> 
      </c:choose> 
      <br /> 
      <form:errors cssClass="errors" id="_${sec_field}_error_id" path="${sec_field}" /> 
     </c:otherwise> 
     </c:choose> 
     <c:choose> 
     <c:when test="${required}"> 
      <spring:message code="field_required" var="field_required" htmlEscape="false" /> 
      <spring:message argumentSeparator="," arguments="${label},(${field_required})" code="field_simple_validation" var="field_validation" htmlEscape="false" /> 
     </c:when> 
     <c:otherwise> 
      <spring:message argumentSeparator="," arguments="${label}, " code="field_simple_validation" var="field_validation" htmlEscape="false" /> 
     </c:otherwise> 
     </c:choose> 
     <c:set var="sec_field_validation"> 
     <spring:escapeBody javaScriptEscape="true">${field_validation}</spring:escapeBody> 
     </c:set> 
     <c:set var="sec_field_invalid"> 
     <spring:escapeBody javaScriptEscape="true" htmlEscape="true">${field_invalid}</spring:escapeBody> 
     </c:set> 
     <c:set var="sec_field_required"> 
     <spring:escapeBody javaScriptEscape="true">${field_required}</spring:escapeBody> 
     </c:set> 
     <c:set var="sec_validation_regex" value="" /> 
     <c:if test="${!empty validationRegex}"> 
     <c:set var="sec_validation_regex" value="regExp : '${validationRegex}', " /> 
     </c:if> 
     <script type="text/javascript"> 
     Spring.addDecoration(new Spring.ElementDecoration({elementId : '_${sec_field}_id', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: '${sec_field_validation}', invalidMessage: '${sec_field_invalid}', required : ${required}, ${sec_validation_regex} missingMessage : '${sec_field_required}' }})); 
     </script> 
    </div> 
    <br /> 
    </c:if> 
</jsp:root> 

我有以下checkbox.tagx文件

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
    <jsp:output omit-xml-declaration="yes" /> 

    <jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)" /> 
    <jsp:directive.attribute name="field" type="java.lang.String" required="true" rtexprvalue="true" description="The field exposed from the form backing object" /> 
    <jsp:directive.attribute name="label" type="java.lang.String" required="false" rtexprvalue="true" description="The label used for this field, will default to a message bundle if not supplied" /> 
    <jsp:directive.attribute name="disableFormBinding" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Set to true to disable Spring form binding" /> 
    <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" /> 
    <jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)" /> 

    <c:if test="${empty render or render}"> 
    <c:if test="${empty label}"> 
     <spring:message code="label_${fn:toLowerCase(fn:substringAfter(id,'_'))}" var="label" htmlEscape="false" /> 
    </c:if> 
    <c:set var="sec_field"> 
     <spring:escapeBody javaScriptEscape="true">${field}</spring:escapeBody> 
    </c:set> 

    <script type="text/javascript">dojo.require("dijit.form.CheckBox");</script> 
    <div id="_${sec_id}_id"> 
     <label for="_${sec_field}_id"> 
     <c:out value="${fn:escapeXml(label)}" />: 
     </label> 
     <c:choose> 
     <c:when test="${disableFormBinding}"> 
      <input id="_${sec_field}_id" name="${sec_field}" type="checkbox"/> 
     </c:when> 
     <c:otherwise> 
      <form:checkbox id="_${sec_field}_id" path="${sec_field}"/> 
     </c:otherwise> 
     </c:choose> 
     <script type="text/javascript"> 
     Spring.addDecoration(new Spring.ElementDecoration({elementId : '_${sec_field}_id', widgetType : 'dijit.form.CheckBox', widgetAttrs : {}})); 
     </script> 
    </div> 
    <br /> 
    </c:if> 
</jsp:root> 
+0

我只想讓用戶選中複選框。我不需要在後端做任何驗證。 – user1514499 2012-07-25 15:25:02

+1

如果用戶在其瀏覽器中禁用了JavaScript,會發生什麼情況?這意味着他們可以在不接受條款和條件的情況下使用該應用程序? – 2012-07-26 02:39:02

回答

1

也許this sample幫助。

在您的代碼:

<!-- add the id attribute to your form, to get it with JS --> 
<form id="f" name="f" action="${fn:escapeXml(form_url)}" method="POST"> 
    ... 
    <!-- add an input checkbox --> 
    <input type="checkbox" id="terms" /> 
    .... 
    <!-- in your input button add onclick attribute calling the JS function --> 
    <input id="proceed" onclick="doSubmit()" type="submit" value="${fn:escapeXml(submit_label)}" /> 
    ... 
</form> 

這是你需要的JS功能:

function doSubmit() { 
    var checkbox = document.getElementById("terms"); 
    if (!checkbox.checked) { 
     alert("error message here!"); 
     return; 
    } 
    document.getElementById("f").submit(); 
}​ 

希望它能幫助。

+0

嗨Jelies,我剛剛添加了我需要實現複選框的代碼。你能檢查一下,讓我知道你能幫忙嗎? – user1514499 2012-07-25 15:43:26

+0

剛剛編輯過,請看看;) – jelies 2012-07-25 16:01:22

+0

這是不正確的,因爲你沒有調用'doSubmit()'這是必要的! – jelies 2012-07-26 06:11:42