JTraining Blog

A short description about your blog
Tags >> Ajax4JSF
svanhugten
svanhugten

When working with JSF 1.x you will eventually find out about some of its annoying features. Such as that it is all about HTTP POST and nothing but HTTP POST. So when working with forms with inputTextArea components you will notice that refreshing or navigating away and then back will leave them blank again. Some of customers I worked for had a problem understand this limitation, because dropdown boxes just worked fine and demanded that the text areas work the same.

I found a small piece of code that will remedy this shortcoming.   

 


Tagged in: Java EE 6 , Java EE , Ajax4JSF , Ajax
svanhugten
svanhugten

It's something that it very hard to find once some of your RichFaces pages are suddenly acting very weird, like sortable columns only sorting once and then ignoring all other requests. It turns out that the following configuration changes the way events and life cycle are handled, breaking AJAX.

<context-param> 

<param-name>facelets.BUILD_BEFORE_RESTORE</param-name> 


Tagged in: RichFaces , Java EE 6 , Facelets , Ajax4JSF
svanhugten
svanhugten

 

Het uploaden van bestanden is vaak een niet-triviale taak. Eerst moet je het bestand worden gevalideerd op type en grootte, naar een tijdelijke bestemming worden gestreamed en daarna gekopieerd, wat nu al simpeler klinkt dan het is als je het zelf moet doen.
Met RichFaces wordt het echter een stuk simpeler, een oplossing die ik jullie niet wilde onthouden.

De component:

Met de <rich:fileUpload> tag plaats je de component. In mijn geval ziet het er als volgt uit:

<rich:fileUpload maxFilesQuantity="1"
            immediateUpload="true"  onsizerejected=
"alert('Uw bestand mag niet groter dan 1000KB zijn');"
            ontyperejected=
"alert('Alleen *.jpg, *.gif, *.png en *.bmp bestanden zijn toegestaan als plaatje');"
            acceptedTypes=
"jpg, gif, png, bmp"
            fileUploadListener="#{bean.upload}">
</rich:fileUpload>


Dit creëert een upload-component dat één plaatje accepteert en het meteen upload na het selecteren. Als het groter is dan 1000KB (te configureren als maxRequestSize init-param bij het rich-faces filter in web.xml) of als niet voldoet aan de bestandtype-eis, dan geeft dit component netjes een melding. De listener verwijst naar een specifiek event in de achterliggende bean.

public void upload(UploadEvent event) throws IOException {
        UploadItem item = event.getUploadItem();
        String filePath = //path waar het bestand naar toe moet

        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        // In de web.xml is ook weer configureren of er  tijdelijke bestanden worden
        // gemaakt of dat alleen het geheugen wordt gebruikt. Standaard wordt er
        // gebruik gemaakt van tijdelijke bestanden.
        //<init-param>
        //   <param-name>createTempFiles</param-name>
        //   <param-value>true</param-value>
        //</init-param>   
        if (item.isTempFile()) {
            FileInputStream fis = new FileInputStream(item.getFile());
            IOUtils.write(IOUtils.toByteArray(fis), fos);
            fis.close();
        } else {
            IOUtils.write(item.getData(), fos);
        }
        fos.close();
}


Merk op dat ik bij deze code ook de IOUtils uit Apache Commons gebruik om de bestandsafhandeling aanzienlijk te vereenvoudigen.
Hiermee is het bestand geupload, inclusief nette afhandeling van eventuele fouten. Bedankt RichFaces!











































Tagged in: RichFaces , Java SE , Java EE , Ajax4JSF
svanhugten
svanhugten Van de week was ik weer eens bezig met een JSF pagina en er moest iets relatief simpels gebeuren; de gebruiker moest een ‘Weet u dit zeker'-schermpje krijgen als hij/zij iets probeerde te verwijderen. Geduldig zocht ik door alle componenten die ik tot mijn beschikking had en kwam tot mijn schrik tot de conclusie dat er geen confirmation dialog component was in alle component sets die ik gebruikte. JSF Core, MyFaces, RichFaces, Facelets, geen enkele verwijzing naar een confirmation box of iets dat er op leek.
Even googlen geeft wel wat opties zoals deze, maar ik wou alleen het bekende javascript-venstertje laten zien en niet een berg code in mijn pagina's op te nemen of een compleet nieuw component te maken. Het is ten slotte een gewoon stukje javascript, kan dat niet anders?
Natuurlijk wel! De oplossing is bijna te simpel voor woorden:

<h:commandButton action="#{bean.action}" onclick="if (!confirm('Weet u dit zeker?')) return false;" />

De oplossing zit hem in het ‘if (expression is false) return false'-gedeelte. Door de ‘return false' staakt het script zijn werking en wordt de action niet uitgevoerd. Dit stukje code is zelfs nog volledig aan te passen voor internationale toepassingen met i18n bundles.

<h:commandButton action="#{bean.action}" onclick="if (!confirm('#{labels['message.confirm']}')) return false;" />

Voordelen van deze aanpak is dat het zeer weinig code is, die bovendien ook nog voor elke action bruikbaar is. Nadelen hieraan is dat het gevaar op loer ligt dat, als je dit scriptje vaak gebruikt, de code rommelig kan worden door het copy-pasten (Ik weet niet zeker of de labels het ook nog doen als je de code externaliseert). Ook is de dialog box niet te skinnen, wat natuurlijk op te lossen valt door de code/het component van de eerder gelinkte website te gebruiken.
Veel succes ermee!

Tagged in: RichFaces , JSF , Java SE , Java EE , Ajax4JSF

Tags:

1.4 M1 2.1.1 6.10 2010 6.9.1 97 things Access Program advanced Agile Ajax Ajax4JSF Algorithms Applet applications Atlanta Atmosphere Audit available BDD beginner Behaviour beta blind BPM Build bus C# Cache Caching Certification Cloud Cloud computing code code to coast collection Computing Continuous Integration Database Databinding DC Debugging Develop developer development dienst download Driven Early Eclipse Editor EJB Enterprise event Events ExternalNews ExtJS Extreme Programming Facelets features Flex Flight recorder Framework Frameworks free GlassFish Grid Groovy Hat HD Hello World Hibernate HowTo ICT IDE IDEA installation Integration IntelliJ introduction iPhone Java Java EE Java EE 6 java road trip Java SE Java SE 6 JavaFX JavaFX Composer javascript JavaZone jaxws JBehave JBoss JDK 7 JetBrains JNI JPA jQuery JRebel JRebel 3.0 JRockit JRockit Mission Control JSF JSON JTraining jtraining.com JVM klant know lady Leak library license Lift Loom Lucene M1 Mark Maven Memory Milestone 1 milestones mkb music Netbeans NTNU oJava OOP Open Open Source Oracle ORM oslo patch 7 Performance PHP Plugin prizes programmer programming project project management Puzzles QA RAP Red Reflection Reinhold related Release Reuse Rich Ajax Platform RichFaces Road SaaS sample Scala Scaling Script Scrum Seam Server should soap software Software as a Service Source Spring Spring Web Flow SQL String Student Sun sweepstakes Swing Teaching Testing tools Tour Training Transaction Tutorial Update 21 validation video web Web applications web service Web Services Weblogic wiki XP

Sponsers