Lo + Leído
Eventos destacados
Artículos para tus primeros pasos
Si estás empezando a introducirte en el mundo de Groovy y Grails, no te pierdas nuestros artículos básicos:
- Introducción a Groovy, parte 1 (Andrés Almiray)
- Introducción a Groovy, parte 2 (Andrés Almiray)
- Introducción a Groovy, parte 3 (Andrés Almiray)
- Introducción a Grails (Material Seminario ImaginaWorks)
Los protagonistas te cuentan de qué van los proyectos más importantes del mundo Groovy:
Groovy: primeros pasos
Es posible utilizar un Date con segundos?
Gabriel - miércoles 15/07/2009He creado una simple clase con un campo Date startTime, pero cuando genero los Controllers y vistas, me genera un campo editable hasta los minutos. He podido crear un taglib para que me permita editar segundos tambien, pero cuando quiero grabar, no toma los segundos....
alguna idea? No se si debo cambiar tambien el controlador? En la vista he cambiado el tag que datePicker por el que yo he creado: datePickerSeconds.
Este es el codigo que tengo en el archivo del taglib: DatePickerSecondsTagLib.groovy
import org.springframework.web.servlet.support.RequestContextUtils as RCU;
import java.text.SimpleDateFormat;
class DatePickerSecondsTagLib {
def datePickerSeconds = {attrs ->
def xdefault = attrs['default']
if (xdefault == null) {
xdefault = new Date()
} else if (xdefault.toString() != 'none') {
if (xdefault instanceof String) {
xdefault = DateFormat.getInstance().parse(xdefault)
}else if(!(xdefault instanceof Date)){
throwTagError("Tag [datePickerSeconds] requires the default date to be a parseable String or a Date")
}
} else {
xdefault = null
}
def value = attrs['value']
if (value.toString() == 'none') {
value = null
} else if (!value) {
value = xdefault
}
def name = attrs['name']
def id = attrs['id'] ? attrs['id'] : name
def noSelection = attrs['noSelection']
if (noSelection != null)
{
noSelection = noSelection.entrySet().iterator().next()
}
def years = attrs['years']
final PRECISION_RANKINGS = ["year": 0, "month": 10, "day": 20, "hour": 30, "minute": 40, "second":50]
def precision = (attrs['precision'] ? PRECISION_RANKINGS[attrs['precision']] : PRECISION_RANKINGS["minute"])
def day
def month
def year
def hour
def minute
def second
def dfs = new java.text.DateFormatSymbols(RCU.getLocale(request))
def c = null
if (value instanceof Calendar) {
c = value
}
else if (value != null) {
c = new GregorianCalendar();
c.setTime(value)
}
if (c != null) {
day = c.get(GregorianCalendar.DAY_OF_MONTH)
month = c.get(GregorianCalendar.MONTH)
year = c.get(GregorianCalendar.YEAR)
hour = c.get(GregorianCalendar.HOUR_OF_DAY)
minute = c.get(GregorianCalendar.MINUTE)
second = c.get(GregorianCalendar.SECOND)
}
if (years == null) {
def tempyear
if (year == null) {
// If no year, we need to get current year to setup a default range ugly
def tempc = new GregorianCalendar()
tempc.setTime(new Date())
tempyear = tempc.get(GregorianCalendar.YEAR)
} else {
tempyear = year
}
years = (tempyear - 100)..(tempyear + 100)
}
out << "<input type=\"hidden\" name=\"${name}\" value=\"struct\" />"
// create day select
if (precision >= PRECISION_RANKINGS["day"]) {
out.println "Day: <select name=\"${name}_day\" id=\"${id}_day\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
for (i in 1..31) {
out.println "<option value=\"${i}\""
if (i == day) {
out.println " selected=\"selected\""
}
out.println ">${i}</option>"
}
out.println '</select>'
}
// create month select
if (precision >= PRECISION_RANKINGS["month"]) {
out.println "<select name=\"${name}_month\" id=\"${id}_month\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
dfs.months.eachWithIndex {m, i ->
if (m) {
def monthIndex = i + 1
out << "<option value=\"${monthIndex}\""
if (month == i) out << " selected=\"selected\""
out << '>'
out << m
out.println '</option>'
}
}
out.println '</select>'
}
// create year select
if (precision >= PRECISION_RANKINGS["year"]) {
out.println "<select name=\"${name}_year\" id=\"${id}_year\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
for (i in years) {
out.println "<option value=\"${i}\""
if (i == year) {
out.println " selected=\"selected\""
}
out.println ">${i}</option>"
}
out.println '</select>'
}
// do hour select
if (precision >= PRECISION_RANKINGS["hour"]) {
out.println "Hour: <select name=\"${name}_hour\" id=\"${id}_hour\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
for (i in 0..23) {
def h = '' + i
if (i < 10) h = '0' + h
out << "<option value=\"${h}\" "
if (hour == h.toInteger()) out << "selected=\"selected\""
out << '>' << h << '</option>'
out.println()
}
out.println '</select> :'
// If we're rendering the hour, but not the minutes, then display the minutes as 00 in read-only format
if (precision < PRECISION_RANKINGS["minute"]) {
out.println '00'
}
}
// do minute select
if (precision >= PRECISION_RANKINGS["minute"]) {
out.println "<select name=\"${name}_minute\" id=\"${id}_minute\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
for (i in 0..59) {
def m = '' + i
if (i < 10) m = '0' + m
out << "<option value=\"${m}\" "
if (minute == m.toInteger()) out << "selected=\"selected\""
out << '>' << m << '</option>'
out.println()
}
out.println '</select>'
}
// do minute select
if (precision >= PRECISION_RANKINGS["second"]) {
out.println ": <select name=\"${name}_second\" id=\"${id}_second\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, '')
out.println()
}
for (i in 0..59) {
def s = '' + i
if (i < 10) s = '0' + s
out << "<option value=\"${s}\" "
if (second == s.toInteger()) out << "selected=\"selected\""
out << '>' << s << '</option>'
out.println()
}
out.println '</select>'
}
}
}
Un momento...
Re: Es posible utilizar un Date con segundos?
idecasso - jueves 16/07/2009Si funciona correctamente tu taglib en el controlador debes obtener el valor del taglib y crear un objeto Date con los valores, ya sabes, es como en Java, lo mas facil es concatenar todo en una cadena y pasarselo al constructor del Date, habria que checar el formato que debe tener la cadena, no se, es lo que se me ocurre a mi.
En el controlador debes hacerlo en la accion que recibe el valor del taglib.
Ojala esto te sirva de algo.