gikoha’s blog

個人的メモがわり

ファイルのアップロード

list.gspとshow.gspの div class="nav"内に追加

<span class="menuButton"><g:link url="${resource(dir:'abook', file:'upload.gsp')}">
<g:message code="default.vcfload.label" args="[entityName]" />
</g:link> </span>

AbookController.groovyに追加

	def vcfload =
	{
		def file=request.getFile("file")
		if(!file.empty)
		{
			String vcfstr = new String(file.getBytes())
			VCardEngine vcardParser = new VCardEngine();
			VCard vcard = vcardParser.parse(vcfstr)
			String name = vcard.getFormattedName().getFormattedName()
			String phone = vcard.getTelephoneNumbers().next().getTelephone()
			String addr = vcard.getAddresses().next().getStreetAddress()
			String post = vcard.getAddresses().next().getPostalCode()
			//println("Name:"+name+" Addr:〒 "+post+" "+addr+" Phone:"+phone)
			def abookInstance = new Abook()
			abookInstance.name = name
			abookInstance.address = addr
			abookInstance.phone = phone
			abookInstance.postalnum = post
			abookInstance.save(flush: true)

			flash.message = "${message(code: 'default.created.message', args: [message(code: 'abook.label', default: 'Abook'), abookInstance.id])}"
			redirect(action: "show", id: abookInstance.id)
		}
		else
		{
			flash.message = "${message(code: 'default.file.blank.message')}"
			render(view:'upload')
		}
	}

upload.gspを作成(新規>ビューの作成を選ぶ)

<%@ page import="gbook.Abook"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<g:set var="entityName"
	value="${message(code: 'abook.label', default: 'Abook')}" />
<title><g:message code="default.show.label" args="[entityName]" />
</title>
</head>
<body>
	<div class="nav">
		<span class="menuButton"><a class="home"
			href="${createLink(uri: '/')}"><g:message
					code="default.home.label" /> </a> </span> <span class="menuButton"><g:link
				class="list" action="list">
				<g:message code="default.list.label" args="[entityName]" />
			</g:link> </span> <span class="menuButton"><g:link class="create"
				action="create">
				<g:message code="default.new.label" args="[entityName]" />
			</g:link> </span>
	</div>
	<div class="body">
		<h1>
			<g:message code="default.vcfload.label" args="[entityName]" />
		</h1>
		<g:if test="${flash.message}">
			<div class="message">
				${flash.message}
			</div>
		</g:if>
		<div class="dialog">
		<g:form controller="abook"  method="POST"  action="vcfload" enctype="multipart/form-data">
		<input type="file" name="file"/>
		<input type="submit"/>
		</g:form>
		</div>
	</div>
</body>
</html>

無事vcfアップロード・ダウンロードできるアプリケーションができましたとさ
つーか、明らかに Roo (pure java)よりは楽だな...