在Groovy/Grails中格式化电话号码

在Groovy/Grails中格式化电话号码,grails,Grails,我一直在尝试使用以下标记库在grails应用程序中格式化电话号码 package rewards class MasksTagLib { static defaultEncodeAs = [taglib:'html'] //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']] def phone334 = { attrs

我一直在尝试使用以下标记库在grails应用程序中格式化电话号码

package rewards

class MasksTagLib {
    static defaultEncodeAs = [taglib:'html']
     //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName:            
    [taglib:'none']]    
  def phone334 = { attrs ->
    String phone = attrs.phone
    def formatted = "("+phone.substring(0,3)+") "+pho ne.substring(3,6)+"-"+phone.substring(6)
    out << formatted
  }
 }

如果要自定义索引视图,在这种情况下最好停止使用脚手架页面,是的。(有替代方案,但在这种情况下,它们过于复杂,并且不能为您提供对结果视图的几乎同样多的控制。)


一般来说,从长远来看,将内容分为半个框架不是一个好主意。我的意思是脚手架控制器或脚手架视图,但不是另一侧。从长远来看,一个或另一个很可能会改变,你的页面将被打破

我们能看到普惠制不起作用的地方吗?另外,我猜上面的taglib中有一个拼写错误,单词phone中有空格?“此外,我在视图中使用了import语句来使用taglib”-在视图中使用taglib不需要import语句。我可以向您展示控制器。它使用索引来显示客户列表。我如何将格式应用于显示,因此上面的gsp是
profile.gsp
,在添加taglib调用后,它可以正常工作,但是对于索引,您仍然在为gsp使用scaffolding?taglib文件中的空间是由于我在代码块中复制了代码,并且在查看代码时添加了一些其他注释:查看作为您现在呈现错误的替代方法,因为您可能会发现它更干净。您可以删除taglib导入。相当多的控制器动作看起来仍然是标准的脚手架动作,所以如果你也不想看视图,你可以把它们拿出来,让脚手架来完成工作,但我知道细节经常被拿出来张贴在这里,所以可能不是“真的”。祝你一切顺利!
<%@ page import="rewards.Customer" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'customer.label',           default: 'Customer')}" />
    <title>Customer Profile</title>
</head>
<body>
    <div id="edit-customer" class="content scaffold-edit" role="main">
        <h1>Customer Profile</h1>
        <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
        </g:if>
        <g:hasErrors bean="${customerInstance}">
        <ul class="errors" role="alert">
            <g:eachError bean="${customerInstance}" var="error">
            <li <g:if test="${error in   org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
            </g:eachError>
        </ul>
        </g:hasErrors>
        <g:form url="[resource:customerInstance, action:'updateProfile']" method="PUT" >
            <g:hiddenField name="version" value="${customerInstance?.version}" />
            <fieldset class="buttons">
                <g:actionSubmit class="save" action="updateProfile" value="${message(code: 'default.button.update.label', default: 'Update')}" />
            </fieldset>
            <fieldset class="form">
                <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'firstName', 'error')} ">
                    <label for="firstName">
                        <g:message code="customer.firstName.label" default="First Name" />

                    </label>
                    <g:textField name="firstName" value="${customerInstance?.firstName}"/>
                </div>

                <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'lastName', 'error')} ">
                    <label for="lastName">
                        <g:message code="customer.lastName.label" default="Last Name" />

                    </label>
                    <g:textField name="lastName" value="${customerInstance?.lastName}"/>
                </div>

                <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'phone', 'error')} required">
                    <span id="phone-label" class="property-label"><g:message code="customer.phone.label" default="Phone" /></span>
                    <span class="property-value" aria-labelledby="phone-label"><g:phone334 phone="${customerInstance?.phone}"/></span>
                </div>

                <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'email', 'error')} ">
                    <label for="email">
                        <g:message code="customer.email.label" default="Email" />

                    </label>
                    <g:textField name="email" value="${customerInstance?.email}"/>
                </div>

                <div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'totalPoints', 'error')} required">
                    <span id="totalPoints-label" class="property-label"><g:message code="customer.totalPoints.label" default="Total Points" /></span>
                    <span class="property-value" aria-labelledby="totalPoints-label"><g:fieldValue bean="${customerInstance}" field="totalPoints"/></span>

                </div>
            </fieldset>

        </g:form>
    </div>

    <div id="list-award" class="content scaffold-list" role="main">
        <g:if test="${flash.message}">
            <div class="message" role="status">${flash.message}</div>
        </g:if>
        <table>
        <thead>
                <tr>

                    <g:sortableColumn property="type" title="${message(code: 'award.type.label', default: 'Type')}" />

                    <g:sortableColumn property="awardDate" title="${message(code: 'award.checkinDate.label', default: 'Award Date')}" />

                    <th><g:message code="award.customer.label" default="Phone" /></th>

                    <g:sortableColumn property="points" title="${message(code: 'award.points.label', default: 'Points')}" />

                </tr>
            </thead>
            <tbody>
            <g:each in="${customerInstance.awards}" status="i" var="checkinInstance">
                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                    <td>${fieldValue(bean: checkinInstance, field: "type")}</td>

                    <td>${fieldValue(bean: checkinInstance, field: "awardDate")}</td>

                    <td><g:phone334 phone="${customerInstance?.phone}"/></td>

                    <td>${fieldValue(bean: checkinInstance, field: "points")}</td>
                </tr>
            </g:each>
            </tbody>
        </table>

    </div>
</body>
    package rewards

class CustomerController {
static scaffold = true

def calculationsService

def lookup() {
    // def customerInstance = Customer.list(sort: "lastName", order:       
   "desc", max: 5, offset: 5)
    // dynamic queries
    // def customerInstance = Customer.findAllByLastName("Foster")
    // def customerInstance = Customer.findAllByTotalPoints(5, [sort: "lastName", order: "desc"])
    // def customerInstance = Customer.findAllByPhone(params.id)  // for one row return findBy if rows > 1 only first
    // def customerInstance = Customer.findAllByLastNameLike("H%")  // Case Sensitive
    // def customerInstance = Customer.findAllByLastNameIlike("b%")  // Case Insensitive
    // def customerInstance = Customer.findAllByTotalPointsGreaterThanEquals(3, [sort: "totalPoints"])
    // def customerInstance = Customer.findAllByTotalPointsBetween(2, 4, [sort: "totalPoints"])
    def customerInstance = Customer.findAllByFirstNameIlikeAndTotalPointsGreaterThanEquals("b%", 3)
    [customerInstanceList: customerInstance]
}

def customerLookup(Customer lookupInstance) {
    // Query customer by Phone number - service
    // If no result, - controller
    // create a new customer - service
    // create welcome message - service
    // add award reward - service
    // save customer - service
    // send welcome to kiosk - controller
    // if customer found - controller
    // calculate total ponts - service
    // create welcome message - service
    // add award reward - service
    // save customer - controller
    // send welcome to kiosk - controller
    def (customerInstance, welcomeMessage) = calculationsService.processCheckin(lookupInstance)
    render(view: "checkin", model:[customerInstance: customerInstance, welcomeMessage: welcomeMessage])
}
def index() {
    params.max = 10
    [customerInstanceList: Customer.list(params), customerInstanceCount: Customer.count()]
}

def create() {
    [customerInstance: new Customer()]
}

def save(Customer customerInstance) {
    customerInstance.save()
    redirect(action: "show", id: customerInstance.id)
}

def show(Long id) {
    def customerInstance = Customer.get(id)
    customerInstance = calculationsService.getTotalPoints(customerInstance)
    [customerInstance: customerInstance]
}

def edit(Long id) {
    def customerInstance = Customer.get(id)
    [customerInstance: customerInstance]
}

def update(Long id) {
    def customerInstance = Customer.get(id)
    customerInstance.properties = params
    customerInstance.save()
    redirect(action: "show", id: customerInstance.id)
}

def delete(Long id) {
    def customerInstance = Customer.get(id)
    customerInstance.delete()
    redirect(action: "index")
}

def profile() {
    def customerInstance = Customer.findByPhone(params.id)
    [customerInstance: customerInstance]
}

def updateProfile(Customer customerInstance) {
    customerInstance.save()
    render(view: "profile", model:[customerInstance: customerInstance])
}

def checkin() {}

}